-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Type variables of generic returned callables not resolved/leaking #1703
Comments
You're encountering a limitation of the Python type system. Each type variable has a defined scope, as defined by the PEP 484 scoping rules. The type variable needs to be resolved within the context of its own scope. If the type variables are not resolved (i.e. replaced with an actual type) within a function call, they cannot be resolved later because they are now "out of scope". PEP 484 doesn't indicate what should happen with these variables. Pyright and mypy tend to "leak" them — that is, retain their identity — so you can tell where they came from and fix the problem. One could argue that they should be forcibly resolved to "Any", but that would mask the problem, so that has downsides. One workaround is to take advantage of the fact that generic type aliases (in addition to generic functions and generic classes) can provide the scope for a type variable. Pyright has special logic to handle type generic type aliases that include MyCallable = Callable[[X], X]
def id_f(x: MyCallable[X]) -> MyCallable[X]:
return x I'm going to give this more thought. Perhaps the special-case logic applied to |
I think it makes sense to include the same special-case logic to This change will be included in the next release. |
This is included in pyright 1.1.127, which I just published. It will also be included in the next release of pylance. |
Awesome! |
While trying to implement a typesafe
@curry
decorator which would enable partial application without writing any additional function-specific wrapper-code or other syntactic noise, I came across the problem of representing function-transformers using the python type system.E.g.
Given two typed functions:
I expect
transform(id)
to be typed and typesafe as well.The problem arises with Generics and can be reproduced by typechecking the following snippet:
Same as
mypy
,pyright
cannot infer the type ofX@id_generic
to beint
orLiteral[4]
.This issue might be related to #1639, with the exception that here we have a transformer that produces a generic function, like python/mypy#1317.
The text was updated successfully, but these errors were encountered: