Skip to content

Commit ab0ea1e

Browse files
committed
Fix crash with function redefinition (#14064)
Fixes #14027 (issue was surfaced by #13509)
1 parent 592a9ce commit ab0ea1e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Diff for: mypy/checker.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,10 @@ def _visit_func_def(self, defn: FuncDef) -> None:
960960
# Function definition overrides a variable initialized via assignment or a
961961
# decorated function.
962962
orig_type = defn.original_def.type
963-
assert orig_type is not None, f"Error checking function redefinition {defn}"
963+
if orig_type is None:
964+
# If other branch is unreachable, we don't type check it and so we might
965+
# not have a type for the original definition
966+
return
964967
if isinstance(orig_type, PartialType):
965968
if orig_type.type is None:
966969
# Ah this is a partial type. Give it the type of the function.

Diff for: test-data/unit/check-functions.test

+14
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,20 @@ else:
14751475
@dec
14761476
def f(): pass
14771477

1478+
[case testConditionalFunctionDefinitionUnreachable]
1479+
def bar() -> None:
1480+
if False:
1481+
foo = 1
1482+
else:
1483+
def foo(obj): ...
1484+
1485+
def baz() -> None:
1486+
if False:
1487+
foo: int = 1
1488+
else:
1489+
def foo(obj): ... # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type "int")
1490+
[builtins fixtures/tuple.pyi]
1491+
14781492
[case testConditionalRedefinitionOfAnUnconditionalFunctionDefinition1]
14791493
from typing import Any
14801494
def f(x: str) -> None: pass

0 commit comments

Comments
 (0)