Skip to content
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

Generic function type argument inference problem #230

Closed
JukkaL opened this issue Jul 14, 2013 · 5 comments · Fixed by #16122
Closed

Generic function type argument inference problem #230

JukkaL opened this issue Jul 14, 2013 · 5 comments · Fixed by #16122
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 14, 2013

The type checker can't infer the type argument in the call to f below, even though there seems to be no reason why it shouldn't be able to do it:

from typing import List, TypeVar
T = TypeVar('T')
def f(a: List[T], b: List[T]) -> None: pass
x = ['']
f(x, [])  # Cannot infer type argument 1 of "f"
@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 15, 2015

After #556 is done, this becomes more annoying.

@rwbarton
Copy link
Contributor

This doesn't seem so easy to fix. We need to either make use of information learned from type checking the first argument to infer a type variable in the second argument (but what if the arguments were in the other order?) or be able to return a polymorphic type when type checking the second argument. I think the latter is easier and it's related to some other changes I want to make anyways (e.g. #1551), but it will require some reorganization.

@gvanrossum gvanrossum modified the milestones: 0.5, 0.4.x Jul 14, 2016
@gvanrossum
Copy link
Member

Based on Reid's comment I'm moving this to 0.5.0 (or maybe even later?).

@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 17, 2018
@earshinov
Copy link

Is there some syntax to pass type arguments explicitly when Python cannot infer the types? I am looking for something like this: f[str](x, []). If not, is there an issue or document when this "explicit passing of type arguments" feature is tracked?

@ilevkivskyi
Copy link
Member

@earshinov I think the best recent issue to discuss allowing explicit type application for functions is #6073.

hauntsaninja pushed a commit that referenced this issue Sep 26, 2023
Fixes #230
Fixes #6463
I bet it fixes some other duplicates, I closed couple yesterday, but
likely there are more.

This may look a bit ad-hoc, but after some thinking this now starts to
make sense to me for two reasons:
* Unless I am missing something, this should be completely safe.
Special-casing only applies to inferred types (i.e. empty collection
literals etc).
* Empty collections _are_ actually special. Even if we solve some
classes of issues with more principled solutions (e.g. I want to re-work
type inference against unions in near future), there will always be some
corner cases involving empty collections.

Similar issues keep coming, so I think it is a good idea to add this
special-casing (especially taking into account how simple it is, and
that it closer some "popular" issues).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants