Skip to content

For min/max functions a default value changes the key function type #6460

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
kaste opened this issue Feb 21, 2019 · 4 comments
Closed

For min/max functions a default value changes the key function type #6460

kaste opened this issue Feb 21, 2019 · 4 comments

Comments

@kaste
Copy link

kaste commented Feb 21, 2019

Related but maybe not the same #3354

Given

from typing import List, Tuple
l: List[Tuple[int, str]] = []
v = max(l, key=lambda item: item[0], default=None)
reveal_type(v)

mypy complains about the item[0] usage in the key function:

Value of type "Optional[Tuple[int, str]]" is not indexable
Revealed type is 'Union[Tuple[builtins.int, builtins.str], None]'

Also interesting that it shows two different representations for the inferred type.
For user ergonomics the revealed type should be 'Optional[Tuple[int, str]]' as well, no?

@gvanrossum
Copy link
Member

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

Agreed on using the same representation for types; can you file that separately? Or send in a PR if you can.

@kaste
Copy link
Author

kaste commented Feb 21, 2019

Well, changing reveal_type is probably easy but then we get approx. 1.000.000 test failures. E.g.

Expected:
  main:10: error: Revealed type is 'builtins.int' (diff)
  main:11: error: Revealed type is 'Tuple[builtins.int, builtins.int]' (diff)
  main:13: error: Revealed type is 'Literal[1]' (diff)
  main:14: error: Argument 1 to "force2" has incompatible type "Tuple[int, in...
  main:14: error: Revealed type is 'Tuple[builtins.int, builtins.int]' (diff)
Actual:
  main:10: error: Revealed type is "int"        (diff)
  main:11: error: Revealed type is "Tuple[int, int]" (diff)
  main:13: error: Revealed type is "Literal[1]" (diff)
  main:14: error: Argument 1 to "force2" has incompatible type "Tuple[int, in...
  main:14: error: Revealed type is "Tuple[int, int]" (diff)

Say, I find my regex-ninja in me, the diff of the PR would be 💣.

@srittau
Copy link
Contributor

srittau commented Mar 8, 2019

Original issue was fixed in python/typeshed#2833.

@kaste
Copy link
Author

kaste commented Mar 8, 2019

I think a 8 lines diff still counts as contribution to python core. 🙌

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

3 participants