Skip to content

Inconsistent behavior with Literals depending on the amount of elements it contains #9099

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

Open
ethan-leba opened this issue Jul 5, 2020 · 2 comments
Labels

Comments

@ethan-leba
Copy link
Contributor

ethan-leba commented Jul 5, 2020

This is an issue I ran into working on #9097.
Code adapted from this test.

Literals with one value:

from typing import List
from typing_extensions import Literal

lit1: Literal[1]
lit2: Literal[2]

arr2 = [lit1, lit2]

reveal_type(arr2)
➜  mypy git:(master) python3 -m mypy leba_misc/test2.py
leba_misc/test2.py:9: note: Revealed type is 'builtins.list[builtins.int*]'

Literals with multiple values:

from typing import List
from typing_extensions import Literal

lit1: Literal[1]
lit2: Literal[2, 3]

arr2 = [lit1, lit2]

reveal_type(arr2)
➜  mypy git:(master) python3 -m mypy leba_misc/test2.py
leba_misc/test2.py:9: note: Revealed type is 'builtins.list[Union[Literal[1], Literal[2], Literal[3]]]'

On Python 3.7 and mypy 0.790.

@ethan-leba
Copy link
Contributor Author

As far as I understand this, the inconsistency is stemming from the fact that literals are joined by their fallback types, while Unions are simply merged together, and that mypy treats Literals with multiple values as a Union of literals for each value.

So my thoughts on fixing this would to join Literals in the same way that Unions are joined, but I'm not sure if this would cause problems elsewhere.

@ethan-leba
Copy link
Contributor Author

Thoughts on this @Michael0x2a? It looks like you wrote the join visitor for literals poking around on git blame

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

No branches or pull requests

2 participants