Specify specific kwargs in inherited method (for code-completion) without having to re-implement parent method

Viewed 15

I have a generic method in a parent class which is supposed to be called on the child classes only. In this method the kwargs are supposed to match the attributes of the child. I want to dynamically specify the child attributes as the kwargs of the method (for code-completion, documentation) without having to re-implement the method and call super().

Example code:

class Parent:
    @classmethod
    def method(cls, **kwargs):
        for key in kwargs:
            if not hasattr(cls, key):
                raise AttributeError(f'{cls.__name__} has no attribute {key}')

class Child(Parent):
    a: int = 1
    b: str = 'c'

In this scenario, when I call Child.method I would like to have code completion for a and b. Limiting the kwargs on the child also allow me to eliminate the for loop checking that the kwargs are correct.

Ideally I would like to do this only once on the parent, and not have to think about it on the child (also I don't want to have to update the method each time I change the child attributes).

0 Answers
Related