8000 Better diagnostic for conditional function mismatch by hauntsaninja · Pull Request #13604 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Better diagnostic for conditional function mismatch #13604

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 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Better diagnostic for conditional function mismatch
Fixes #10575
  • Loading branch information
hauntsaninja committed Sep 4, 2022
commit 6c95b758b525108851952a8832f0e079b33d87b1
5 changes: 3 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,9 @@ def _visit_func_def(self, defn: FuncDef) -> None:
new_type = self.function_type(defn)
if isinstance(defn.original_def, FuncDef):
# Function definition overrides function definition.
if not is_same_type(new_type, self.function_type(defn.original_def)):
self.msg.incompatible_conditional_function_def(defn)
old_type = self.function_type(defn.original_def)
if not is_same_type(new_type, old_type):
self.msg.incompatible_conditional_function_def(defn, old_type, new_type)
else:
# Function definition overrides a variable initialized via assignment or a
# decorated function.
Expand Down
6 changes: 5 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,12 @@ def incompatible_self_argument(
context,
)

def incompatible_conditional_function_def(self, defn: FuncDef) -> None:
def incompatible_conditional_function_def(
self, defn: FuncDef, old_type: FunctionLike, new_type: FunctionLike
) -> None:
self.fail("All conditional function variants must have identical signatures", defn)
self.note(f"Original signature: {old_type}", defn)
self.note(f"Redefinition signature: {new_type}", defn)

def cannot_instantiate_abstract_class(
self, class_name: str, abstract_attributes: dict[str, bool], context: Context
Expand Down
16 changes: 12 additions & 4 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1393,23 +1393,29 @@ x = None # type: Any
if x:
def f(x: int) -> None: pass
else:
def f(x): pass # E: All conditional function variants must have identical signatures
def f(x): pass # E: All conditional function variants must have identical signatures \
# N: Original signature: def (x: builtins.int) \
# N: Redefinition signature: def (x: Any) -> Any

[case testIncompatibleConditionalFunctionDefinition2]
from typing import Any
x = None # type: Any
if x:
def f(x: int) -> None: pass
else:
def f(y: int) -> None: pass # E: All conditional function variants must have identical signatures
def f(y: int) -> None: pass # E: All conditional function variants must have identical signatures \
# N: Original signature: def (x: builtins.int) \
# N: Redefinition signature: def (y: builtins.int)

[case testIncompatibleConditionalFunctionDefinition3]
from typing import Any
x = None # type: Any
if x:
def f(x: int) -> None: pass
else:
def f(x: int = 0) -> None: pass # E: All conditional function variants must have identical signatures
def f(x: int = 0) -> None: pass # E: All conditional function variants must have identical signatures \
# N: Original signature: def (x: builtins.int) \
# N: Redefinition signature: def (x: builtins.int =)

[case testConditionalFunctionDefinitionUsingDecorator1]
from typing import Callable
Expand Down Expand Up @@ -1640,7 +1646,9 @@ class A:
if x:
def f(self, x: int) -> None: pass
else:
def f(self, x): pass # E: All conditional function variants must have identical signatures
def f(self, x): pass # E: All conditional function variants must have identical signatures \
# N: Original signature: def (self: __main__.A, x: builtins.int) \
# N: Redefinition signature: def (self: __main__.A, x: Any) -> Any
[out]

[case testConditionalFunctionDefinitionInTry]
Expand Down
4 changes: 3 additions & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,9 @@ try:
from m import f, g
except:
def f(x): pass
def g(x): pass # E: All conditional function variants must have identical signatures
def g(x): pass # E: All conditional function variants must have identical signatures \
# N: Original signature: def (x: Any, y: Any) -> Any \
# N: Redefinition signature: def (x: Any) -> Any
[file m.py]
def f(x): pass
def g(x, y): pass
Expand Down
9 changes: 7 additions & 2 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,9 @@ if int():
elif bool():
def f(x: int) -> None:
1() # E: "int" not callable
def g(x: str) -> None: # E: All conditional function variants must have identical signatures
def g(x: str) -> None: # E: All conditional function variants must have identical signatures \
# N: Original signature: def (x: builtins.int) \
# N: Redefinition signature: def (x: builtins.str)
pass
else:
def f(x: int) -> None:
Expand All @@ -1881,7 +1883,10 @@ if int():
else:
def f(x: A) -> None:
1() # E: "int" not callable
def g(x: str) -> None: # E: All conditional function variants must have identical signatures
def g(x: str) -> None: # E: All conditional function variants must have identical signatures \
# N: Original signature: def (x: __main__.A) \
# N: Redefinition signature: def (x: builtins.str)

pass

reveal_type(g) # N: Revealed type is "def (x: __main__.A)"
Expand Down
8 changes: 6 additions & 2 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ import sys
if sys.version_info >= (3, 5, 0):
def foo() -> int: return 0
else:
def foo() -> str: return '' # E: All conditional function variants must have identical signatures
def foo() -> str: return '' # E: All conditional function variants must have identical signatures \
# N: Original signature: def () -> builtins.int \
# N: Redefinition signature: def () -> builtins.str
[builtins fixtures/ops.pyi]
[out]

Expand All @@ -253,7 +255,9 @@ import sys
if sys.version_info[1:] >= (5, 0):
def foo() -> int: return 0
else:
def foo() -> str: return '' # E: All conditional function variants must have identical signatures
def foo() -> str: return '' # E: All conditional function variants must have identical signatures \
# N: Original signature: def () -> builtins.int \
# N: Redefinition signature: def () -> builtins.str
[builtins fixtures/ops.pyi]
[out]

Expand Down
0