Skip to content

Commit 27ab644

Browse files
committed
Allow overloading to succeed if an arg is an instance of a class derived from Any. Fix #1363. (#1365)
1 parent bfedd4a commit 27ab644

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

mypy/checkexpr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,8 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
16251625
"""
16261626
if (isinstance(actual, NoneTyp) or isinstance(actual, AnyType) or
16271627
isinstance(formal, AnyType) or isinstance(formal, TypeVarType) or
1628-
isinstance(formal, CallableType)):
1628+
isinstance(formal, CallableType) or
1629+
(isinstance(actual, Instance) and actual.type.fallback_to_any)):
16291630
# These could match anything at runtime.
16301631
return 2
16311632
if isinstance(actual, UnionType):

mypy/subtypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def visit_deleted_type(self, left: DeletedType) -> bool:
9595
return True
9696

9797
def visit_instance(self, left: Instance) -> bool:
98+
if left.type.fallback_to_any:
99+
return True
98100
right = self.right
99101
if isinstance(right, Instance):
100102
if left.type._promote and is_subtype(left.type._promote,

mypy/test/data/check-overloading.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,19 @@ f(y=[''], x=0)() # E: "int" not callable
567567
a = f(y=[['']], x=0) # E: List item 0 has incompatible type List[str]
568568
a() # E: "int" not callable
569569
[builtins fixtures/list.py]
570+
571+
[case testOverloadWithDerivedFromAny]
572+
from typing import Any, overload
573+
Base = None # type: Any
574+
575+
class C:
576+
@overload
577+
def __init__(self, a: str) -> None: pass
578+
@overload
579+
def __init__(self, a: int) -> None: pass
580+
581+
class Derived(Base):
582+
def to_dict(self) -> C:
583+
return C(self) # fails without the fix for #1363
584+
C(Derived()) # fails without the hack
585+
C(Base()) # Always ok

0 commit comments

Comments
 (0)