Skip to content

Commit

Permalink
Fix crash with function redefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Nov 11, 2022
1 parent b18281c commit 4c4cf8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,11 @@ 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:
# I'm not sure why this happens, probably a bug elsewhere
# See testConditionalFunctionDefinitionWithTuple
# Setting orig_type to UninhabitedType ensures an error is reported
orig_type = UninhabitedType()
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
11 changes: 11 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,17 @@ else:
@dec
def f(): pass

[case testConditionalFunctionDefinitionWithTuple]
def bar() -> None:
# weirdly, needs to be an empty tuple. empty lists and various other special assignments
# don't trigger the bug
cond = ()
if cond:
foo = 1
else:
def foo(obj): ... # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type <nothing>)
[builtins fixtures/tuple.pyi]

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

0 comments on commit 4c4cf8f

Please sign in to comment.