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 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: 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
11 changes: 10 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,17 @@ 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)
if isinstance(old_type, (CallableType, Overloaded)) and isinstance(
new_type, (CallableType, Overloaded)
):
self.note("Original:", defn)
self.pretty_callable_or_overload(old_type, defn, offset=4)
self.note("Redefinition:", defn)
self.pretty_callable_or_overload(new_type, defn, offset=4)

def cannot_instantiate_abstract_class(
self, class_name: str, abstract_attributes: dict[str, bool], context: Context
Expand Down
36 changes: 30 additions & 6 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1393,23 +1393,35 @@ 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: \
# N: def f(x: int) -> None \
# N: Redefinition: \
# N: def f(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: \
# N: def f(x: int) -> None \
# N: Redefinition: \
# N: def f(y: int) -> None

[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: \
# N: def f(x: int) -> None \
# N: Redefinition: \
# N: def f(x: int = ...) -> None

[case testConditionalFunctionDefinitionUsingDecorator1]
from typing import Callable
Expand Down Expand Up @@ -1467,14 +1479,22 @@ from typing import Any
def f(x: str) -> None: pass
x = None # type: Any
if x:
def f(x: int) -> None: pass # E: All conditional function variants must have identical signatures
def f(x: int) -> None: pass # E: All conditional function variants must have identical signatures \
# N: Original: \
# N: def f(x: str) -> None \
# N: Redefinition: \
# N: def f(x: int) -> None

[case testConditionalRedefinitionOfAnUnconditionalFunctionDefinition2]
from typing import Any
def f(x: int) -> None: pass # N: "f" defined here
x = None # type: Any
if x:
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: \
# N: def f(x: int) -> None \
# N: Redefinition: \
# N: def f(y: int) -> None
f(x=1) # The first definition takes precedence.
f(y=1) # E: Unexpected keyword argument "y" for "f"

Expand Down Expand Up @@ -1640,7 +1660,11 @@ 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: \
# N: def f(self: A, x: int) -> None \
# N: Redefinition: \
# N: def f(self: A, x: Any) -> Any
[out]

[case testConditionalFunctionDefinitionInTry]
Expand Down
6 changes: 5 additions & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,11 @@ 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: \
# N: def g(x: Any, y: Any) -> Any \
# N: Redefinition: \
# N: def g(x: Any) -> Any
[file m.py]
def f(x): pass
def g(x, y): pass
Expand Down
13 changes: 11 additions & 2 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,11 @@ 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: \
# N: def g(x: int) -> None \
# N: Redefinition: \
# N: def g(x: str) -> None
pass
else:
def f(x: int) -> None:
Expand All @@ -1881,7 +1885,12 @@ 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: \
# N: def g(x: A) -> None \
# N: Redefinition: \
# N: def g(x: str) -> None

pass

reveal_type(g) # N: Revealed type is "def (x: __main__.A)"
Expand Down
18 changes: 15 additions & 3 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,19 @@ elif foo():
elif foo():
def f(x: Union[int, str, int, int, str]) -> None: pass
elif foo():
def f(x: Union[int, str, float]) -> None: pass # E: All conditional function variants must have identical signatures
def f(x: Union[int, str, float]) -> None: pass # E: All conditional function variants must have identical signatures \
# N: Original: \
# N: def f(x: Union[int, str]) -> None \
# N: Redefinition: \
# N: def f(x: Union[int, str, float]) -> None
elif foo():
def f(x: Union[S, T]) -> None: pass
elif foo():
def f(x: Union[str]) -> None: pass # E: All conditional function variants must have identical signatures
def f(x: Union[str]) -> None: pass # E: All conditional function variants must have identical signatures \
# N: Original: \
# N: def f(x: Union[int, str]) -> None \
# N: Redefinition: \
# N: def f(x: str) -> None
else:
def f(x: Union[Union[int, T], Union[S, T], str]) -> None: pass

Expand All @@ -206,7 +214,11 @@ else:
if foo():
def g(x: Union[int, str, bytes]) -> None: pass
else:
def g(x: Union[int, str]) -> None: pass # E: All conditional function variants must have identical signatures
def g(x: Union[int, str]) -> None: pass # E: All conditional function variants must have identical signatures \
# N: Original: \
# N: def g(x: Union[int, str, bytes]) -> None \
# N: Redefinition: \
# N: def g(x: Union[int, str]) -> None

[case testUnionSimplificationSpecialCases]
from typing import Any, TypeVar, Union
Expand Down
12 changes: 10 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,11 @@ 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: \
# N: def foo() -> int \
# N: Redefinition: \
# N: def foo() -> str
[builtins fixtures/ops.pyi]
[out]

Expand All @@ -253,7 +257,11 @@ 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: \
# N: def foo() -> int \
# N: Redefinition: \
# N: def 47F0 foo() -> str
[builtins fixtures/ops.pyi]
[out]

Expand Down
0