Skip to content
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

False override error with Unpack and TypedDict #18250

Closed
mharding-hpe opened this issue Dec 5, 2024 · 2 comments
Closed

False override error with Unpack and TypedDict #18250

mharding-hpe opened this issue Dec 5, 2024 · 2 comments
Labels
bug mypy got something wrong

Comments

@mharding-hpe
Copy link

Bug Report

When using Unpack and TypedDict in a child class method, mypy is giving a false override error.

To Reproduce

from typing import final, TypedDict, Unpack

@final
class Kwargs(TypedDict):
    a: int

class Parent:
    def harf(self, required_arg: str, a: int) -> None:
        pass

class Child(Parent):
    def harf(self, required_arg: str, **kwargs: Unpack[Kwargs]) -> None:
        super().harf(required_arg, **kwargs)

https://mypy-play.net/?mypy=latest&python=3.12&gist=78008ce1aa8f6b34a332ea34e2937efc

Expected Behavior

I don't think mypy should report an error. The Kwargs class is final, so it should not be subclassed, and it is total, so it should have all of its keywords.

Actual Behavior

main.py:12: error: Signature of "harf" incompatible with supertype "Parent"  [override]
main.py:12: note:      Superclass:
main.py:12: note:          def harf(self, required_arg: str, a: int) -> None
main.py:12: note:      Subclass:
main.py:12: note:          def harf(self, required_arg: str, *, a: int) -> None
Found 1 error in 1 file (checked 1 source file)

The * makes it seem like mypy thinks my subclass method allows for additional parameters that are not present in the parent method, but I don't see where it is getting that idea.

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags: None (I reproduced it on the playground)
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12
@mharding-hpe mharding-hpe added the bug mypy got something wrong label Dec 5, 2024
@erictraut
Copy link

erictraut commented Dec 5, 2024

Mypy is correct to generate an error here. The Parent.harf method has a parameter a that accepts both positional and keyword arguments, but you're attempting to override it with a method that accepts a only as a keyword argument.

You can fix the type error by changing Parent.harf to accept a only as a keyword argument, like this:

class Parent:
    def harf(self, required_arg: str, *, a: int) -> None:
        pass

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Dec 5, 2024
@mharding-hpe
Copy link
Author

Thank you so much for the prompt response! I very much appreciate it, and apologize for the false bug report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants