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

Do not count __slots__ as a protocol member #11885

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,8 @@ def protocol_members(self) -> List[str]:
for base in self.mro[:-1]: # we skip "object" since everyone implements it
if base.is_protocol:
for name in base.names:
members.add(name)
if name != '__slots__': # `__slots__` is not a protocol member
members.add(name)
return sorted(list(members))

def __getitem__(self, name: str) -> 'SymbolTableNode':
Expand Down
36 changes: 36 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2693,6 +2693,42 @@ class A(Protocol):

[builtins fixtures/tuple.pyi]

[case testProrocolSlotsIsNotProtocolMember]
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
# 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'
def foo(f: Foo):
pass

# All should pass:
foo(NoSlots())
foo(EmptySlots())
foo(TupleSlots())
foo(StringSlots())
[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 testNoneVsProtocol]
# mypy: strict-optional
from typing_extensions import Protocol
Expand Down