python / python/mypy

Unclear message if named tuple is not compatible with protocol

Open
#7,224 3 comments 1 reaction 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.