Skip to content

Commit

Permalink
Fix crash when overriding with unpacked TypedDict (#17359)
Browse files Browse the repository at this point in the history
Fixes #17208

While writing the fix (that is trivial), I could not notice that the
relevant code simply assumes functions can have nothing but positional
parameters. This could lead really misleading error messages, so I
decided to fix this as well.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ilevkivskyi and pre-commit-ci[bot] authored Jun 11, 2024
1 parent 317533c commit b8a0260
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
26 changes: 19 additions & 7 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,7 @@ def check_override(
if fail:
emitted_msg = False

offset_arguments = isinstance(override, CallableType) and override.unpack_kwargs
# Normalize signatures, so we get better diagnostics.
if isinstance(override, (CallableType, Overloaded)):
override = override.with_unpacked_kwargs()
Expand Down Expand Up @@ -2285,12 +2286,23 @@ def check_override(
def erase_override(t: Type) -> Type:
return erase_typevars(t, ids_to_erase=override_ids)

for i in range(len(override.arg_types)):
if not is_subtype(
original.arg_types[i], erase_override(override.arg_types[i])
):
arg_type_in_super = original.arg_types[i]

for i, (sub_kind, super_kind) in enumerate(
zip(override.arg_kinds, original.arg_kinds)
):
if sub_kind.is_positional() and super_kind.is_positional():
override_arg_type = override.arg_types[i]
original_arg_type = original.arg_types[i]
elif sub_kind.is_named() and super_kind.is_named() and not offset_arguments:
arg_name = override.arg_names[i]
if arg_name in original.arg_names:
override_arg_type = override.arg_types[i]
original_i = original.arg_names.index(arg_name)
original_arg_type = original.arg_types[original_i]
else:
continue
else:
continue
if not is_subtype(original_arg_type, erase_override(override_arg_type)):
if isinstance(node, FuncDef) and not node.is_property:
context: Context = node.arguments[i + len(override.bound_args)]
else:
Expand All @@ -2300,7 +2312,7 @@ def erase_override(t: Type) -> Type:
name,
type_name,
name_in_super,
arg_type_in_super,
original_arg_type,
supertype,
context,
secondary_context=node,
Expand Down
53 changes: 48 additions & 5 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ class B(A):
class C(A):
def f(self, *, b: int, a: str) -> None: pass # Fail
[out]
main:10: error: Signature of "f" incompatible with supertype "A"
main:10: note: Superclass:
main:10: note: def f(self, *, a: int, b: str) -> None
main:10: note: Subclass:
main:10: note: def f(self, *, b: int, a: str) -> None
main:10: error: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "str"
main:10: note: This violates the Liskov substitution principle
main:10: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:10: error: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int"

[case testPositionalOverridingArgumentNameInsensitivity]
import typing
Expand Down Expand Up @@ -3324,3 +3323,47 @@ class Bar(Foo):
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
...
[builtins fixtures/property.pyi]

[case testNoCrashOnUnpackOverride]
from typing import Unpack
from typing_extensions import TypedDict

class Params(TypedDict):
x: int
y: str

class Other(TypedDict):
x: int
y: int

class B:
def meth(self, **kwargs: Unpack[Params]) -> None:
...
class C(B):
def meth(self, **kwargs: Unpack[Other]) -> None: # E: Signature of "meth" incompatible with supertype "B" \
# N: Superclass: \
# N: def meth(*, x: int, y: str) -> None \
# N: Subclass: \
# N: def meth(*, x: int, y: int) -> None

...
[builtins fixtures/tuple.pyi]

[case testOverrideErrorLocationNamed]
class B:
def meth(
self, *,
x: int,
y: str,
) -> None:
...
class C(B):
def meth(
self, *,
y: int, # E: Argument 1 of "meth" is incompatible with supertype "B"; supertype defines the argument type as "str" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
x: int,
) -> None:
...
[builtins fixtures/tuple.pyi]

0 comments on commit b8a0260

Please sign in to comment.