Skip to content

Commit

Permalink
Fix type checker crash in multiple inheritance
Browse files Browse the repository at this point in the history
This potentially addresses #998.
  • Loading branch information
JukkaL committed Nov 25, 2015
1 parent 122226c commit 42e2583
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,15 +914,21 @@ def check_compatibility(self, name: str, base1: TypeInfo,
second_type = second.type
if second_type is None and isinstance(second.node, FuncDef):
second_type = self.function_type(cast(FuncDef, second.node))
# TODO: What if first_type or second_type is None? What if some classes are generic?
# TODO: What if some classes are generic?
if (isinstance(first_type, FunctionLike) and
isinstance(second_type, FunctionLike)):
# Method override
first_sig = method_type(cast(FunctionLike, first_type))
second_sig = method_type(cast(FunctionLike, second_type))
ok = is_subtype(first_sig, second_sig)
else:
elif first_type and second_type:
ok = is_equivalent(first_type, second_type)
else:
if first_type is None:
self.msg.cannot_determine_type_in_base(name, base1.name(), ctx)
if second_type is None:
self.msg.cannot_determine_type_in_base(name, base2.name(), ctx)
ok = True
if not ok:
self.msg.base_class_definitions_incompatible(name, base1, base2,
ctx)
Expand Down
3 changes: 3 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ def string_interpolation_mixing_key_and_non_keys(self, context: Context) -> None
def cannot_determine_type(self, name: str, context: Context) -> None:
self.fail("Cannot determine type of '%s'" % name, context)

def cannot_determine_type_in_base(self, name: str, base: str, context: Context) -> None:
self.fail("Cannot determine type of '%s' in base class '%s'" % (name, base), context)

def invalid_method_type(self, sig: CallableType, context: Context) -> None:
self.fail('Invalid method type', context)

Expand Down
15 changes: 15 additions & 0 deletions mypy/test/data/check-multiple-inheritance.test
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,18 @@ class G(Generic[T]):
class A(G[int]):
def f(self, s: int) -> 'A': pass
class B(A, int): pass

[case testCannotDetermineTypeInMultipleInheritance]
class A(B, C):
def f(self): pass
class B:
@dec
def f(self): pass
class C:
@dec
def f(self): pass
def dec(f): return f
[out]
main: note: In class "A":
main:1: error: Cannot determine type of 'f' in base class 'B'
main:1: error: Cannot determine type of 'f' in base class 'C'

0 comments on commit 42e2583

Please sign in to comment.