Skip to content

Fix crash with function redefinition #14064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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