Skip to content

__slots__ requires explicit type definition and must be Any (and nothing else) when used with Protocols #11013

Description

@posita
# test_case1.pyfrom __future__ importannotationsfromtypingimportProtocol, runtime_checkable@runtime_checkableclassFoo(Protocol):
__slots__= ()
deffoo(self):
...
classBar:
__slots__= ("_t",)
def__init__(self):
self._t=Truedeffoo(self):
passprint(f"isinstance(Bar(), Foo) == {isinstance(Bar(), Foo)}") # <- prints … == Truefoo: Foo=Bar() # <- errors here
% python test_case1.py
isinstance(Bar(), Foo) == True
% mypy --version
mypy 0.910
% mypy --config-file=/dev/null test_case1.py
/dev/null: No [mypy] section in config file
test_case1.py:19: error: Incompatible types in assignment (expression has type "Bar", variable has type "Foo")
test_case1.py:19: note: Following member(s) of "Bar" have conflicts:
test_case1.py:19: note: __slots__: expected "Tuple[]", got "Tuple[str]"

The "fix" is to provide Tuple[str, ...] as the type for __slots__:

# test_case2.pyfrom __future__ importannotationsfromtypingimportProtocol, Tuple, runtime_checkable@runtime_checkableclassFoo(Protocol):
__slots__: Tuple[str, ...] = ()
deffoo(self):
...
classBar:
__slots__ : Tuple[str, ...] = ("_t",)
def__init__(self):
self._t=Truedeffoo(self):
passprint(f"isinstance(Bar(), Foo) == {isinstance(Bar(), Foo)}") # still Truefoo: Foo=Bar() # <- works now?
% mypy --config-file=/dev/null test_case2.py
/dev/null: No [mypy] section in config file
Success: no issues found in 1 source file

Shouldn't mypy already know the type of __slots__?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions