Skip to content

Literal, get_args and list comprehension false positive  #13094

Closed as not planned
@bloussou

Description

@bloussou

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions