Skip to content

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

Closed

Description

@karb94

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)

Activity

  1. Hnasar commented on Apr 12, 2024

    @Hnasar
    Contributor

    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.

  2. karb94 commented on Apr 12, 2024

    @karb94
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions