-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Add partial support for TypeIs
#18294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
Regarding the three problems outlined here:
This PR itself has two new issues:
|
|
Sorry, didn't realize there was a comment here looking for feedback. (In general I don't see draft PRs in my notifications, and I assume draft PRs are not ready for review, unless I'm pinged by name for comment.)
I think this is a feature, not a bug. The point of
Happy to dig into this tomorrow, if this is the only issue blocking this PR. |
|
@carljm Actually, I think I got them:
The only blocking problem left is that the fuzzer says this PR causes new panics. Here's the generated snippet: for name_3 in something:
class name_5[**name_4](name_3):
pass
else:
class name_3[name_5](b'', name_5=name_1):
pass
name_5()
for name_2 in something:
pass
else:
@name_3
def name_3():
pass
(name_3 := f'') and (name_1 := name_2)
assert name_3I reckon this has something to do with the Otherwise, this is ready for review. |
7826639 to
5d89050
Compare
|
Sorry I didn't get to this sooner; been focused on getting the big attribute-assignment-narrowing PR landed; it's now landed! It looks like this currently has a build issue and needs conflict resolution with main. @InSyncWithFoo let me know if you're able to get it back into shape; if so I can look into the new panic right away. |
carljm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some initial review comments. Thanks for your work on this!
crates/ty_python_semantic/resources/mdtest/type_properties/is_subtype_of.md
Show resolved
Hide resolved
| /// The ID of the scope to which the symbol belongs, | ||
| /// the ID of the symbol itself within that scope, | ||
| /// and the symbol's name. | ||
| symbol_info: Option<(ScopeId<'db>, ScopedSymbolId, String)>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the scope and the symbol ID, we should be able to look up the symbol name? Also, I'm not sure we need to include the symbol name in the display of a bound TypeIs anyway. So I would remove the String here.
Also, I think this information will be insufficient to properly handle the case where the symbol may be re-bound. I think we will need to store the visible definitions of the symbol here to support that.
| Some(_) => None, | ||
| }; | ||
|
|
||
| // TODO: Handle unions/intersections |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of handling would we expect here? I don't think it's valid to expect narrowing from returning a union where one of the elements is a TypeIs...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. As for intersections, TypeIs[a, str] & TypeIs[b, int] should have narrowing effect, but I'm not sure if that type is constructable from Python code.
|
@carljm Thanks for the heads up. I probably won't be able to work on this earlier than Sunday (June 8th). Also, the reason for the panic was that the same |
|
This PR is unsalvageable; I submitted #18589 as replacement. |
Summary
Redo of #16314, part of #117.
TypeIs[]is a special form that allows users to define their own narrowing functions. Despite the syntax,TypeIsis not a generic and, on its own, it is meaningless as a type. Officially, a function annotated as returning aTypeIs[T]is a type narrowing function, whereTis called theTypeIsreturn type.A
TypeIs[T]may or may not be bound to a symbol. Only bound types have narrowing effect:A
TypeIs[T]type:Tis fully static.TrueandFalse.In other words, an unbound type have ambiguous truthiness.
It is possible to infer more precise truthiness for bound types; however, that is not part of this change.
TypeIs[T]is a subtype of or otherwise assignable tobool.TypeIsis invariant with respect to theTypeIsreturn type:TypeIs[int]is neither a subtype nor a supertype ofTypeIs[bool]. When ty sees a function marked as returningTypeIs[T], itsreturns will be checked againstboolinstead. ty will also report such functions if they don't accept a positional argument. Addtionally, a type narrowing function call with no positional arguments (e.g.,f()in the example above) will be considered invalid.Test Plan
Markdown tests.