Skip to content

Commit bb36995

Browse files
committed
fix: exclude __class__ from protocol members
Fixes #21795.
1 parent 869d6ef commit bb36995

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

mypy/nodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,7 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
35953595
{
35963596
"__abstractmethods__",
35973597
"__annotations__",
3598+
"__class__",
35983599
"__dict__",
35993600
"__doc__",
36003601
"__init__",

test-data/unit/check-overloading.test

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6856,3 +6856,42 @@ if isinstance(headers, dict):
68566856

68576857
reveal_type(headers) # N: Revealed type is "__main__.Headers | typing.Iterable[tuple[builtins.bytes, builtins.bytes]]"
68586858
[builtins fixtures/isinstancelist.pyi]
6859+
6860+
[case testOverloadProtocolClassPropertyDoesNotEraseListItemType]
6861+
from collections.abc import Iterator, Sequence
6862+
from typing import Any, Protocol, TypeVar, overload
6863+
6864+
T_co = TypeVar("T_co", covariant=True)
6865+
6866+
class ReducedCovariantList(Protocol[T_co]):
6867+
@property
6868+
def __class__(self) -> type[list[Any]]: ...
6869+
def __iter__(self) -> Iterator[T_co]: ...
6870+
6871+
@overload
6872+
def f(x: ReducedCovariantList[str]) -> str: ...
6873+
@overload
6874+
def f(x: Sequence[int]) -> int: ...
6875+
def f(x: object) -> object: ...
6876+
6877+
reveal_type(f([1])) # N: Revealed type is "builtins.int"
6878+
[file builtins.py]
6879+
from typing import Generic, Self, TypeVar
6880+
from collections.abc import Iterator, Sequence
6881+
6882+
class object:
6883+
@property
6884+
def __class__(self) -> type[Self]: pass
6885+
class type: pass
6886+
class function: pass
6887+
class property: pass
6888+
class int: pass
6889+
class str: pass
6890+
class bool: pass
6891+
class dict: pass
6892+
class ellipsis: pass
6893+
class tuple: pass
6894+
6895+
T = TypeVar("T")
6896+
class list(Sequence[T], Generic[T]):
6897+
def __iter__(self) -> Iterator[T]: pass

0 commit comments

Comments
 (0)