Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude the same special attributes from Protocol as CPython #15490

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,25 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_temp_node(self)


# Special attributes not collected as protocol members by Python 3.12
# See typing._SPECIAL_NAMES
EXCLUDED_PROTOCOL_ATTRIBUTES: Final = frozenset(
{
"__abstractmethods__",
"__annotations__",
"__dict__",
"__doc__",
"__init__",
"__module__",
"__new__",
"__slots__",
"__subclasshook__",
"__weakref__",
"__class_getitem__", # Since Python 3.9
}
)


class TypeInfo(SymbolNode):
"""The type structure of a single class.

Expand Down Expand Up @@ -3115,6 +3134,8 @@ def protocol_members(self) -> list[str]:
if isinstance(node.node, (TypeAlias, TypeVarExpr, MypyFile)):
# These are auxiliary definitions (and type aliases are prohibited).
continue
if name in EXCLUDED_PROTOCOL_ATTRIBUTES:
continue
members.add(name)
return sorted(list(members))

Expand Down
64 changes: 64 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,70 @@ class A(Protocol):

[builtins fixtures/tuple.pyi]

[case testProtocolSlotsIsNotProtocolMember]
# https://github.com/python/mypy/issues/11884
from typing import Protocol

class Foo(Protocol):
__slots__ = ()
class NoSlots:
pass
class EmptySlots:
__slots__ = ()
class TupleSlots:
__slots__ = ('x', 'y')
class StringSlots:
__slots__ = 'x y'
class InitSlots:
__slots__ = ('x',)
def __init__(self) -> None:
self.x = None
def foo(f: Foo):
pass

# All should pass:
foo(NoSlots())
foo(EmptySlots())
foo(TupleSlots())
foo(StringSlots())
foo(InitSlots())
[builtins fixtures/tuple.pyi]

[case testProtocolSlotsAndRuntimeCheckable]
from typing import Protocol, runtime_checkable

@runtime_checkable
class Foo(Protocol):
__slots__ = ()
class Bar:
pass
issubclass(Bar, Foo) # Used to be an error, when `__slots__` counted as a protocol member
[builtins fixtures/isinstance.pyi]
[typing fixtures/typing-full.pyi]


[case testProtocolWithClassGetItem]
# https://github.com/python/mypy/issues/11886
from typing import Any, Iterable, Protocol, Union

class B:
...

class C:
def __class_getitem__(cls, __item: Any) -> Any:
...

class SupportsClassGetItem(Protocol):
__slots__: Union[str, Iterable[str]] = ()
def __class_getitem__(cls, __item: Any) -> Any:
...

b1: SupportsClassGetItem = B()
c1: SupportsClassGetItem = C()
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]


[case testNoneVsProtocol]
# mypy: strict-optional
from typing_extensions import Protocol
Expand Down