Adding a field to a callback protocol changes the checks performed by mypy
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I noticed something really weird about callback protocols. Consider the following example:
from typing import TypeVar, Protocol
T = TypeVar("T")
class MyCallable(Protocol[T]):
__name__: str
def __call__(
self,
__arg: T,
) -> T: ...
def decorator(x: MyCallable[T]) -> MyCallable[T]:
return x
@decorator
def func(arg: T) -> T:
return arg
x = func(1)
It works fine with mypy 1.10.0 (although it fails to check with pyright 1.1.360: https://github.com/microsoft/pyright/issues/7782).
However, adding a __name__: str field to the protocol changes the checks performed by mypy. The example becomes:
from typing import TypeVar, Protocol
T = TypeVar("T")
class MyCallable(Protocol[T]):
__name__: str
def __call__(
self,
__arg: T,
) -> T: ...
def decorator(x: MyCallable[T]) -> MyCallable[T]:
return x
@decorator
def func(arg: T) -> T:
return arg
x = func(1)
And it fails with:
test.py:25: error: Need type annotation for "x" [var-annotated]
test.py:25: error: Argument 1 to "__call__" of "MyCallable" has incompatible type "int"; expected Never [arg-type]
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 with the callback protocol reproducer in test.py and compare mypy 1.10.0's behavior when name is absent versus present. Trace the inference that produces Never and the var-annotated error, then add a regression test showing that adding the field does not change the inferred argument type or diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100