Skip to content

Cannot infer type argument 1 of "min" with differing default type #3354

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
jkleint opened this issue May 11, 2017 · 5 comments
Closed

Cannot infer type argument 1 of "min" with differing default type #3354

jkleint opened this issue May 11, 2017 · 5 comments

Comments

@jkleint
Copy link

jkleint commented May 11, 2017

(Apologies if this is a typeshed issue.) When calling min with different types for the iterable and the default, like min(Iterable[T], default=S), min often does not infer the correct type for the result, and gives a confusing error message "Cannot infer type argument 1 of "min". I assumed that meant it couldn't infer the type of argument 1, but I think it actually means it can't infer the first (only) type parameter in the definition:

def min(iterable: Iterable[_T], key: Callable[[_T], Any] = ..., default: _T = ...) -> _T: ...

I would expect the result of min(Iterable[T], default=S) to be:

  • The common supertype of S and T (possibly S or T itself), if they share a supertype narrower than Any or object
  • Union[S, T] otherwise (Optional[T] in the case S is None)

But maybe that's hard / impossible to express with the type system?

Here's some code that illustrates the problem. Oddly, fixed-length tuples typecheck fine, but variable-length tuples do not. Explicitly specifying the type of the result (which is the same as the type parameter) does not help.

from typing import Tuple, Optional

t1 = (0, 1, 2)          # type: Tuple[int, int, int]        # The inferred type without any type hint
min(t1, default=None)   # typechecks

t2 = (0, 1, 2)          # type: Tuple[int, ...]
min(t2, default=None)   # error: Cannot infer type argument 1 of "min"
min(t2, default=None)   # type: Optional[int]               # Does not help

min([0, 1, 2], default=None)        # error: Cannot infer type argument 1 of "min"
min(iter((0, 1, 2)), default=None)  # error: Cannot infer type argument 1 of "min"
$ python -V ; pip freeze | egrep 'mypy|typing'
Python 3.4.3+
mypy==0.501
typing==3.6.1
@gvanrossum
Copy link
Member

I think this may be similar to #1732.

@jkleint
Copy link
Author

jkleint commented May 11, 2017

Ah, I should have noted it only fails to typecheck with --strict-optional.

@ilevkivskyi
Copy link
Member

I think this may be similar to #1732.

Yes, this is similar. And I think the solution should be probably the same: add an overload with a Union.

@gvanrossum
Copy link
Member

So should this be closed here and opened in typeshed instead? @srittau.

@97littleleaf11
Copy link
Collaborator

Has been fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants