Skip to content

Literal, get_args and list comprehension false positive #13094

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
bloussou opened this issue Jul 8, 2022 · 1 comment
Closed

Literal, get_args and list comprehension false positive #13094

bloussou opened this issue Jul 8, 2022 · 1 comment
Labels
bug mypy got something wrong topic-literal-types topic-type-narrowing Conditional type narrowing / binder

Comments

@bloussou
Copy link

bloussou commented Jul 8, 2022

Bug Report

Looks like mypy fail to consider that the following code is valid where pyright deals correctly with it. I think it is a false positive.

To Reproduce

from typing import get_args, Literal, Iterable

_Names = Literal["Joe", "Bob"]
_AllNames = Literal["Brian", _Names]

all_names: Iterable[_AllNames] = []
names: Iterable[_Names] = get_args(_Names)
filtered_names: Iterable[_Names] = [name for name in all_names if name in names] # fail here

Expected Behavior

I don't think it should throw an error since name is a _Names. Pyright is seeing this as correct code.

Actual Behavior

It is failing with the following error when copying the code in a file and running poetry run mypy ./test_literal.py --show-error-codes

test_literal.py:8: error: List comprehension has incompatible type List[Literal['Brian', 'Joe', 'Bob']]; expected List[Literal['Joe', 'Bob']]  [misc]
test_literal.py:8: error: Unsupported operand types for in ("Literal['Brian', 'Joe', 'Bob']" and "Iterable[Literal['Joe', 'Bob']]")  [operator]

Your Environment

  • Mypy version used: 0.941 and 0.961
  • Python version used: 3.9.10
  • Operating system and version: Ubuntu 22.04 LTS
@bloussou bloussou added the bug mypy got something wrong label Jul 8, 2022
@bloussou bloussou changed the title Literal and get_args false positive Literal, get_args and list comprehension false positive Jul 8, 2022
@AlexWaygood AlexWaygood added topic-literal-types topic-type-narrowing Conditional type narrowing / binder labels Jul 8, 2022
@AlexWaygood
Copy link
Member

Duplicate of #9718

@AlexWaygood AlexWaygood marked this as a duplicate of #9718 Jul 8, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-literal-types topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants