Skip to content

Commit c039095

Browse files
committed
remove catchall return, add test case about the Any inheritance
1 parent 9fe8046 commit c039095

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

mypy/checker.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,25 +1628,6 @@ def check_compatibility(self, name: str, base1: TypeInfo,
16281628
return
16291629
first = base1[name]
16301630
second = base2[name]
1631-
if isinstance(first.node, TypeInfo) and isinstance(second.node, TypeInfo):
1632-
# Checking compatibility of two nested classes with the same name is disabled.
1633-
# It creates a lot of false positives in frameworks like Django, DRF and others,
1634-
# where nested classes is used to define custom properties for the outer class, like
1635-
# class MyModel:
1636-
# class Meta:
1637-
# abstract = True
1638-
#
1639-
# This is technically unsafe, it create false-negatives in cases like
1640-
# class Mixin1:
1641-
# class Meta:
1642-
# def get() -> int: pass
1643-
# class Mixin2:
1644-
# class Meta:
1645-
# def get() -> str: pass
1646-
# class A(Mixin2, Mixin1):
1647-
# pass
1648-
# var: Mixin1 = A(); var.Meta.get() # return type will be "str"
1649-
return
16501631
first_type = self.determine_type_of_class_member(first)
16511632
second_type = self.determine_type_of_class_member(second)
16521633

test-data/unit/check-multiple-inheritance.test

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def dec(f: Callable[..., T]) -> Callable[..., T]:
241241
main:3: error: Cannot determine type of 'f' in base class 'B'
242242
main:3: error: Cannot determine type of 'f' in base class 'C'
243243

244-
[case testMultipleInheritanceNestedClassesWithSameNameAllowed]
244+
[case testMultipleInheritanceOfNestedClassesWithSameNameButNotSubclassesOfOneAnother]
245245
class Mixin1:
246246
class Meta:
247247
pass
@@ -251,6 +251,7 @@ class Mixin2:
251251
class A(Mixin1, Mixin2):
252252
pass
253253
[out]
254+
main:7: error: Definition of "Meta" in base class "Mixin1" is incompatible with definition in base class "Mixin2"
254255

255256
[case testMultipleInheritanceFailIfNestedClassAndAttrHaveSameName]
256257
class Mixin1:
@@ -338,3 +339,28 @@ class Base2:
338339
class Combo(Base2, Base1): ...
339340
[out]
340341
main:9: error: Definition of "NestedVar" in base class "Base2" is incompatible with definition in base class "Base1"
342+
343+
[case testDoNotFailIfBothNestedClassesInheritFromAny]
344+
from typing import Any
345+
class Mixin1:
346+
class Meta(Any):
347+
pass
348+
class Mixin2:
349+
class Meta(Any):
350+
pass
351+
class A(Mixin1, Mixin2):
352+
pass
353+
[out]
354+
355+
[case testDoNotFailIfOneOfNestedClassesIsOuterInheritedFromAny]
356+
from typing import Any
357+
class Outer(Any):
358+
pass
359+
class Mixin1:
360+
Meta = Outer
361+
class Mixin2:
362+
class Meta(Any):
363+
pass
364+
class A(Mixin1, Mixin2):
365+
pass
366+
[out]

0 commit comments

Comments
 (0)