From 7e0148d7cb75b5e96282a8f5d7af58528d60ce86 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 2 Jan 2022 16:13:32 +0300 Subject: [PATCH 1/2] Do not count `__slots__` as a protocol member, refs #11884 --- mypy/nodes.py | 3 ++- test-data/unit/check-protocols.test | 36 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 78a018f94a78..0e7d44ac5010 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -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': diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 6768263e9832..d787d40687e6 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2693,6 +2693,42 @@ class A(Protocol): [builtins fixtures/tuple.pyi] +[case testProrocolSlotsIsNotProtocolMember] +# 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 From 26a950de229b27276a56d62c2426e3b541630d9f Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sat, 7 May 2022 16:14:55 -0700 Subject: [PATCH 2/2] Update test-data/unit/check-protocols.test --- test-data/unit/check-protocols.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index d787d40687e6..e239b9a69ab8 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2693,7 +2693,7 @@ class A(Protocol): [builtins fixtures/tuple.pyi] -[case testProrocolSlotsIsNotProtocolMember] +[case testProtocolSlotsIsNotProtocolMember] # https://github.com/python/mypy/issues/11884 from typing import Protocol