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

Narrow literal type iterating fixed container #13558

Closed
mixilchenko opened this issue Aug 30, 2022 · 1 comment
Closed

Narrow literal type iterating fixed container #13558

mixilchenko opened this issue Aug 30, 2022 · 1 comment
Labels

Comments

@mixilchenko
Copy link

Code to run static analysis on

from typing import Final, Literal

for x1 in ('a', 'b'):
    reveal_type(x1)
    
xs2: tuple[Literal['a', 'b'], ...] = ('a', 'b')
for x2 in xs2:
    reveal_type(x2)

xs3: Final = ('a', 'b')
for x3 in xs3:
    reveal_type(x3)

x4: Literal['a', 'b']
for x4 in ('a', 'b'):
    reveal_type(x4)

Mypy (master and 0.971) result

main.py:4: note: Revealed type is "builtins.str"
main.py:8: note: Revealed type is "Union[Literal['a'], Literal['b']]"
main.py:12: note: Revealed type is "builtins.str"
main.py:15: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['a', 'b']")
main.py:16: note: Revealed type is "Union[Literal['a'], Literal['b']]"

Expected Mypy result

main.py:4: note: Revealed type is "Union[Literal['a'], Literal['b']]"
main.py:8: note: Revealed type is "Union[Literal['a'], Literal['b']]"
main.py:12: note: Revealed type is "Union[Literal['a'], Literal['b']]"
main.py:16: note: Revealed type is "Union[Literal['a'], Literal['b']]"
@mixilchenko mixilchenko added the bug mypy got something wrong label Aug 30, 2022
@AlexWaygood AlexWaygood added topic-literal-types topic-type-narrowing Conditional type narrowing / binder feature and removed bug mypy got something wrong labels Sep 2, 2022
@AlexWaygood
Copy link
Member

Duplicate of #12535

@AlexWaygood AlexWaygood marked this as a duplicate of #12535 Sep 2, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Sep 2, 2022
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