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

sum(List[Literal[0,1]]) is inferred as Literal[0,1] while it should be int #13055

Closed
jvdwetering opened this issue Jul 2, 2022 · 2 comments
Closed

Comments

@jvdwetering
Copy link

Bug report
Using sum on a set of Literal ints has an inferred type of the same Literals, instead of inferring the correct more general type of int.

To reproduce

from typing import Literal, List

Z2 = Literal[0,1]

l: List[Z2] = [0,1,1]
a = sum(l)
reveal_type(a) # "Union[Literal[0], Literal[1]]"

a = a - 1 # error: Incompatible types in assignment (expression has type "int", variable has type "Literal[0, 1]")

Expected behaviour
The type of a should be inferred as int.

Actual behaviour
The type of a is inferred to be Literal[0,1]. This is the case for mypy==0.940 and onwards. On mypy==0.930 the correct typing of int is inferred. Explicitly annotating a with int also fixes the issue.

Your environment
Mypy version: 0.961 (also tried 0.940)
Mypy command line flags: none
Mypy configuration options from mypy.ini (and other config files): no other config
Python version used: 3.9.1
Operating system and version: Windows 10

@AlexWaygood
Copy link
Member

AlexWaygood commented Jul 2, 2022

This is a typeshed problem, can you open an issue there? https://github.com/python/typeshed

@AlexWaygood
Copy link
Member

Superseded by python/typeshed#8230

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jul 2, 2022
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

2 participants