Skip to content

Commit 26d5743

Browse files
gvanrossumJukkaL
authored andcommitted
Check for subtype, not supertype, when redefining a function. Fix #1434. (#1439)
1 parent 8bd292c commit 26d5743

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

mypy/checker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ def visit_func_def(self, defn: FuncDef) -> Type:
598598
self.fail(messages.INCOMPATIBLE_REDEFINITION, defn)
599599
else:
600600
# TODO: Update conditional type binder.
601-
self.check_subtype(orig_type, new_type, defn,
601+
self.check_subtype(new_type, orig_type, defn,
602602
messages.INCOMPATIBLE_REDEFINITION,
603-
'original type',
604-
'redefinition with type')
603+
'redefinition with type',
604+
'original type')
605605

606606
def check_func_item(self, defn: FuncItem,
607607
type_override: CallableType = None,

mypy/test/data/check-functions.test

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,23 @@ main: note: In function "g":
11461146
def g(): pass
11471147
f = g
11481148
if g():
1149-
def f(x): pass # E: Incompatible redefinition (original type Callable[[], Any], redefinition with type Callable[[Any], Any])
1149+
def f(x): pass # E: Incompatible redefinition (redefinition with type Callable[[Any], Any], original type Callable[[], Any])
1150+
1151+
[case testRedefineFunctionDefinedAsVariableWithVariance1]
1152+
class B: pass
1153+
class C(B): pass
1154+
def g(x: C) -> B: pass
1155+
f = g
1156+
if g(C()):
1157+
def f(x: C) -> C: pass
1158+
1159+
[case testRedefineFunctionDefinedAsVariableWithVariance2]
1160+
class B: pass
1161+
class C(B): pass
1162+
def g(x: C) -> B: pass
1163+
f = g
1164+
if g(C()):
1165+
def f(x: B) -> B: pass
11501166

11511167
[case testRedefineFunctionDefinedAsVariableInitializedToEmptyList]
11521168
f = [] # E: Need type annotation for variable

0 commit comments

Comments
 (0)