Implement user-defined type guard method#1501
Draft
tk0miya wants to merge 1 commit intosoutaro:masterfrom
Draft
Conversation
tk0miya
commented
Feb 19, 2025
05bd3c7 to
1e9f080
Compare
tk0miya
commented
Feb 20, 2025
Comment on lines
+752
to
+757
| def context_from(type_name) | ||
| if type_name.namespace == RBS::Namespace.root | ||
| [nil, type_name] | ||
| else | ||
| parent = context_from(type_name.namespace.to_type_name) | ||
| [parent, type_name] | ||
| end | ||
| end |
Contributor
Author
There was a problem hiding this comment.
I could not find such a helper method in Steep and RBS.
I'm not sure where the best place is.
d43291c to
6821365
Compare
tk0miya
commented
Feb 24, 2025
835b944 to
c1a708e
Compare
This implements the minimal version of user-defined type guard method.
A user-defined type guard methos is a method defined by user that is
able to guarantee the type of the objects (receiver or arguments).
At present, Steep supports type guard methods provided by ruby-core (ex.
`#is_a?`, `#nil?`, and so on). But we also have many kinds of
user-defined methods that are able to check the type of the objects.
Therefore user-defined type guard will help checking the type of these
applications by narrowing types.
This implementation uses an annotation to declare user-defined type
guard method.
```
class Example < Integer
%a{guard:self is Integer}
def integer?: () -> bool
end
```
For example, the above method `Example#integer?` is a user-defined
type guard method that narrows the Example object itself to an Integer
if the conditional branch passed.
```
example = Example.new
if example.integer?
example #=> Integer
end
```
In this PR, the predicate of type guards only supports "self is TYPE"
statement. I have a plan to extend it:
* `%a{guard:self is arg}`
* `%a{guard:self is_a arg}`
* `%a{guard:self is TYPE_PARAM}`
* `%a{guard:arg is TYPE}`
Note: The compatibility of RBS syntax is the large reason of using
annotations. I'm afraid that adding a new syntax to define it will
bring breaking change to the RBS, and difficult to use it on common
repository or generators (ex. gem_rbs_collection and rbs_rails).
c1a708e to
2988b4e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This implements the minimal version of user-defined type guard method. A user-defined type guard methos is a method defined by user that is able to guarantee the type of the objects (receiver or arguments).
At present, Steep supports type guard methods provided by ruby-core (ex.
#is_a?,#nil?, and so on). But we also have many kinds of user-defined methods that are able to check the type of the objects.Therefore user-defined type guard will help checking the type of these applications by narrowing types.
This implementation uses an annotation to declare user-defined type guard method.
For example, the following method
Example#integer?is a user-defined type guard method that narrows the Example object itself to an Integer if the conditional branch passed.In this PR, the predicate of type guards only supports "self is TYPE" statement. I have a plan to extend it:
%a{guard:self is arg}%a{guard:self is_a arg}%a{guard:self is TYPE_PARAM}%a{guard:arg is TYPE}Note: The compatibility of RBS syntax is the large reason of using annotations. I'm afraid that adding a new syntax to define it will bring breaking change to the RBS, and difficult to use it on common repository or generators (ex. gem_rbs_collection and rbs_rails).
refs: