8000 Fix crash when overriding with unpacked TypedDict by ilevkivskyi · Pull Request #17359 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix crash when overriding with unpacked TypedDict #17359

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 3 commits into from
Jun 11, 2024
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
Fix crash when overriding with unpacked TypedDict
  • Loading branch information
ilevkivskyi committed Jun 10, 2024
commit 7827d3c1bd3cd434cf1bcc38618f3d8e3321cceb
24 changes: 19 additions & 5 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,25 @@ 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)):
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_types[i], erase_override(override.arg_types[i])
original_arg_type, erase_override(override_arg_type)
):
arg_type_in_super = original.arg_types[i]

if isinstance(node, FuncDef) and not node.is_property:
context: Context = node.arguments[i + len(override.bound_args)]
else:
Expand All @@ -2300,7 +2314,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
44 changes: 44 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3324,3 +3324,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