python / python/mypy

Adding a field to a callback protocol changes the checks performed by mypy

Open
#17,179 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.