Skip to content

Commit

Permalink
Fix crash with function redefinition (#14064)
Browse files Browse the repository at this point in the history
Fixes #14027 (issue was surfaced by #13509)
  • Loading branch information
hauntsaninja committed Nov 11, 2022
1 parent 592a9ce commit ab0ea1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,10 @@ def _visit_func_def(self, defn: FuncDef) -> None:
# Function definition overrides a variable initialized via assignment or a
# decorated function.
orig_type = defn.original_def.type
assert orig_type is not None, f"Error checking function redefinition {defn}"
if orig_type is None:
# If other branch is unreachable, we don't type check it and so we might
# not have a type for the original definition
return
if isinstance(orig_type, PartialType):
if orig_type.type is None:
# Ah this is a partial type. Give it the type of the function.
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,20 @@ else:
@dec
def f(): pass

[case testConditionalFunctionDefinitionUnreachable]
def bar() -> None:
if False:
foo = 1
else:
def foo(obj): ...

def baz() -> None:
if False:
foo: int = 1
else:
def foo(obj): ... # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type "int")
[builtins fixtures/tuple.pyi]

[case testConditionalRedefinitionOfAnUnconditionalFunctionDefinition1]
from typing import Any
def f(x: str) -> None: pass
Expand Down

0 comments on commit ab0ea1e

Please sign in to comment.