Skip to content

Unclear message if named tuple is not compatible with protocol #7224

Open
@JukkaL

Description

@JukkaL

Mypy just says that class is not compatible with the bound when type checking this example:

from typing import Protocol, Generic, TypeVar, NamedTuple, List, Any

class Example(Protocol):
    sentence: List[str]

T = TypeVar('T', bound=Example)

class Dataset(Generic[T]):
    ...

class ConcreteExample(NamedTuple):
    sentence: List[str]
    others: Any

class ConcreteDataset(Dataset[ConcreteExample]):
    ...

Here's the error message:

t.py:15: error: Type argument "Tuple[builtins.list[builtins.str], Any, fallback=t.ConcreteExample]" of "Dataset" must be a subtype of "t.Example"

It would be better to also say that sentence in ConcreteExample is read-only, and maybe suggest making it a read-only property in the protocol.

Originally reported in #7212.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions