-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[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
base: main
Are you sure you want to change the base?
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_3 I reckon this has something to do with the Otherwise, this is ready for review. |
7826639
to
5d89050
Compare
Summary
Redo of #16314, part of #117.
TypeIs[]
is a special form that allows users to define their own narrowing functions. Despite the syntax,TypeIs
is 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, whereT
is called theTypeIs
return type.A
TypeIs[T]
may or may not be bound to a symbol. Only bound types have narrowing effect:A
TypeIs[T]
type:T
is fully static.True
andFalse
.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
.TypeIs
is invariant with respect to theTypeIs
return type:TypeIs[int]
is neither a subtype nor a supertype ofTypeIs[bool]
. When ty sees a function marked as returningTypeIs[T]
, itsreturn
s will be checked againstbool
instead. 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.