Skip to content

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

Closed
graingert opened this issue Mar 31, 2017 · 17 comments
Closed

Explicit type arguments to override type inference #3096

graingert opened this issue Mar 31, 2017 · 17 comments

Comments

@graingert
Copy link
Contributor

graingert commented Mar 31, 2017

In Python 3.5/3.6 I propose:

from mypy_extensions import type_args

class A: pass
class B(A): pass

type_args(bisect_left, (A))([B()], A())

But that's rather ugly, a Scala like syntax type_args[A]([B()], A()) would be nice but a pain to do.

@JelleZijlstra
Copy link
Member

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 def f(x: T) -> T:, then call f(1), mypy would infer the type argument to f as int, but if you do type_args(f, (float))(1) instead, it would infer it as float.

@graingert
Copy link
Contributor Author

graingert commented Mar 31, 2017

of course type_args would be:

_T = TypeVar('_T')
def type_args(t: _T, types: Any) -> _T:
    return f

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 31, 2017

This is something that used to be supported until mypy switched to Python syntax. I don't like the proposed syntax, though.

@graingert
Copy link
Contributor Author

@JukkaL how was it supported before?

@refi64
Copy link
Contributor

refi64 commented Mar 31, 2017

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)

@graingert
Copy link
Contributor Author

@kirbyfan64 I really like typed_args(func)[A, B](x, y)

@gvanrossum
Copy link
Member

Can't you use a cast()? Seems a pretty unusual use case.

@graingert
Copy link
Contributor Author

graingert commented Mar 31, 2017

@gvanrossum providing explicit type args to a generic function is type safe, cast is not.

@gvanrossum
Copy link
Member

I really like typed_args(func)[A, B](x, y)

Maybe you can find a way to use braces too? :-)

@graingert
Copy link
Contributor Author

@gvanrossum sure, you can use typed_args(func)[{A, B}](x, y)

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 31, 2017

@graingert The earliest versions of mypy translated to Python and it used myfunc<A>(args), IIRC.

@sixolet
Copy link
Collaborator

sixolet commented Mar 31, 2017

@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, [B()] is inferred as List[B], and List[B] is not a valid List[A], due to the type argument of List being (correctly) invariant. There's no type at all you can fill in for the _T of bisect_left that will satisfy. It's ok at runtime here only because you're only reading from the list, not changing it. It sounds like @gvanrossum is right that cast is the feature you're looking for.

@graingert
Copy link
Contributor Author

@sixolet ah in that case the example is wrong.

@sixolet
Copy link
Collaborator

sixolet commented Mar 31, 2017

... Ah. Actually it's probably the _T of bisect_left that is invariant, blocking this, since bisect_left takes a Sequence

@sixolet
Copy link
Collaborator

sixolet commented Mar 31, 2017

... 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 List[int] should be a valid Sequence[int] and Sequence[int] should be a valid Sequence[float]

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 31, 2017

Maybe we should move the type inference discussion to #2035 and leave this for the explicit type arguments discussion.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 28, 2020

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.

@JukkaL JukkaL closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants