`mypy` does not infer correctly with a `__class__` override
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
mypy does not infer correctly with a __class__ override.
To Reproduce
https://mypy-play.net/?gist=9e3ffebda54d02c5e68f45fe7cd33eb0
from typing import Any, Protocol, overload, reveal_type
from collections.abc import Iterator, Sequence
from typing_extensions import TypeVar
_T_co = TypeVar("_T_co", covariant=True)
class ReducedCovariantList(Protocol[_T_co]):
@property
def __class__(self) -> type[list[Any]]: ...
def __iter__(self) -> Iterator[_T_co]: ...
@overload
def foo(x: ReducedCovariantList[str]) -> str: ...
@overload
def foo(x: Sequence[int]) -> int: ...
# ty gives Unknown; mypy gives str; pyright and pyrefly give int
reveal_type(foo([1]))
# if we remove the __class__ override, both ty and mypy give int
Expected Behaviour
reveal_type gives int
Actual Behavior
reveal_type gives str
Your Environment
- Mypy version used: 2.3.0
- Mypy command-line flags: -
- Mypy configuration options from
mypy.ini(and other config files): - - Python version used: 3.12
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
Run the mypy-play reproduction linked in the issue and compare the reveal_type(foo([1])) result with the expected int. Trace how overload resolution handles ReducedCovariantList when its Protocol defines a __class__ property. Done means the example infers int and a regression test covers the override case.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100