Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Annotate *args/**kwargs based on the signature of another class/function #17119

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

Closed
karb94 opened this issue Apr 12, 2024 · 2 comments
Closed
Labels

Comments

@karb94
Copy link

karb94 commented Apr 12, 2024

Feature

It is a common pattern to inherit a Parent class and call its __init__() using *args and **kwargs as below.

class Parent:
    def __init__(self, a: str, b: int = 0):
        print(a, b)

class Child(Parent):
    def __init__(self, c: float, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print(c)

Is there a way of annotating the above *args and **kwargs without rewriting Parent's signature (i.e. no TypedDict + Unpack)?

I would find the following UX very convenient:

class Parent:
    def __init__(self, a: str, b: int = 0):
        print(a, b)

class Child(Parent):
    def __init__(self, c: float, *args: Parent.args, **kwargs: Parent.kwargs):
        super().__init__(*args: Parent.args, **kwargs: Parent.kwargs)
        print(c)
@karb94 karb94 added the feature label Apr 12, 2024
@Hnasar
Copy link
Contributor

Hnasar commented Apr 12, 2024

You can use the copy_type suggestion in python/typing#1079, but in general this isn't supported by the python's typing capabilities right now. I think this isn't applicable for a mypy issue.

@karb94
Copy link
Author

karb94 commented Apr 12, 2024

Thank you for the link. Very interesting discussion. Closing as this is not possible at this time.

@karb94 karb94 closed this as completed Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants