Skip to content
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
3 changes: 3 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,9 @@ def check_compatibility(self, name: str, base1: TypeInfo,
if second_type is None:
self.msg.cannot_determine_type_in_base(name, base2.name(), ctx)
ok = True
# __slots__ is special and the type can vary across class hierarchy.
if name == '__slots__':
ok = True
if not ok:
self.msg.base_class_definitions_incompatible(name, base1, base2,
ctx)
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -4186,6 +4186,9 @@ class F(six.with_metaclass(t.M)): pass
@six.add_metaclass(t.M)
class G: pass

-- Misc
-- ----

[case testCorrectEnclosingClassPushedInDeferred]
class C:
def __getattr__(self, attr: str) -> int:
Expand Down Expand Up @@ -4296,3 +4299,13 @@ class C: pass

@undefined # E: Name 'undefined' is not defined
class D: pass

[case testSlotsCompatibility]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test that there is still an error if we set __slots__ to something nonsensical like an int?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case.

class A:
__slots__ = ()
class B(A):
__slots__ = ('a', 'b')
class C:
__slots__ = ('x',)
class D(B, C):
__slots__ = ('aa', 'bb', 'cc')
9 changes: 9 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1250,3 +1250,12 @@ def g(si: Iterable[str]):
pass
f(E)
g(E)

[case testInvalidSlots]
class A:
__slots__ = 1
class B:
__slots__ = (1, 2)
[out]
_testInvalidSlots.py:2: error: Incompatible types in assignment (expression has type "int", base class "object" defined the type as "Union[str, Iterable[str], None]")
_testInvalidSlots.py:4: error: Incompatible types in assignment (expression has type "Tuple[int, int]", base class "object" defined the type as "Union[str, Iterable[str], None]")