-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Explicit type arguments to override type inference #3096
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
Comments
For people like me who have trouble understanding what's going on here (correct me if I'm wrong): this would be a way to explicitly specify the type argument to a generic function. Normally, if you write |
of course type_args would be:
|
This is something that used to be supported until mypy switched to Python syntax. I don't like the proposed syntax, though. |
@JukkaL how was it supported before? |
Maybe splitting things up would be nicer: typed_args(func)[A, B](x, y) or abusing slice syntax (my personal favorite): typed_args[myfunc: A, B, C](a, b, c) |
@kirbyfan64 I really like |
Can't you use a cast()? Seems a pretty unusual use case. |
@gvanrossum providing explicit type args to a generic function is type safe, cast is not. |
Maybe you can find a way to use braces too? :-) |
@gvanrossum sure, you can use |
@graingert The earliest versions of mypy translated to Python and it used |
@graingert I think the reason you're running into trouble here is that the explicit type arguments you're trying to provide here are subtly not safe. In your original example, |
@sixolet ah in that case the example is wrong. |
... Ah. Actually it's probably the |
... but playing with it, making it covariant doesn't help. Sorry, I thought I knew more than I do. I'm back to thinking it's just a bug in type inference having to do with type variable variance, because |
Maybe we should move the type inference discussion to #2035 and leave this for the explicit type arguments discussion. |
This should be proposed as a PEP 484 extension in the typing issue tracker of typing-sig@ if somebody wants to move ahead with this. |
In Python 3.5/3.6 I propose:
But that's rather ugly, a Scala like syntax
type_args[A]([B()], A())
would be nice but a pain to do.The text was updated successfully, but these errors were encountered: