Unclear message if named tuple is not compatible with protocol
Open
Nobody has claimed this yet.
diagnostics
priority-1-normal
topic-error-reporting
topic-named-tuple
topic-protocols
topic-usability
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example from the issue and checking the current bound error for the NamedTuple type. Update the diagnostic so it explains that ConcreteExample.sentence is read-only and can suggest a read-only property in the protocol; verify the revised message with an appropriate type-checking test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100