Skip to content

mypy does not infer correctly with a __class__ override #21795

Description

@cmp0xff

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions