Skip to content

Incorrect Error with NamedTuple slices ?  #8776

Description

@Carreau

Assuming the following file:

$ cat foo.py
from typing import NamedTuple, List, Tuple, Sequence

from typing import NamedTuple, List, Sequence


class NT(NamedTuple):
    matched_text: str
    matches: Sequence[str]   # Sequence will not match List below
    matches_origin: int


def f() -> Tuple[str, List[str]]: # List will not match sequence above.
    nt = NT("a", ["a", "b"], 1)
    return nt[:2]

Got

$ mypy foo.py
foo.py:14: error: Incompatible return value type (got "NT", expected "Tuple[str, List[str]]")
Found 1 error in 1 file (checked 1 source file)

I would have expected (got "Tuple[str, Seq[str]]", expected...) not got "NT")

Looks like the inference is good, if I replace Sequence by List or vice versa, It's all fine, just the error forget about the slicing.

Metadata

Metadata

Assignees

No one assigned

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions