I have a parent class Foo
implementing a classmethod. I’d like to allow subclasses of Foo
to specify variables as annotations and specify that this subclass classmethod should be called with keyword only arguments that match its annotations (like we can do with dataclasses constructor whose constructor only allows class annotations members as parameters). I don’t mean to do any runtime check, this is just a matter of type hinting so that my type checker warn me when I call this method with an argument that is not in the class annotations. For example
class Foo:
@classmethod
def my_class_method(cls, **kwargs) -> None:
# Some implementation
pass
class Child(Foo):
valueA: int
valueB: str
Is there any way to specify to type checker that Child.my_classmethod
should only accept as key word parameters valueA
and valueB
and nothing else ? I know i could just change the signature of my_class_method
manually for each subclass but I have many of those and I have several methods that should only accept class annotations as key words parameters.. Is there any way to accomplish that ?
I did try to accomplish that by subclassing dataclasses.dataclass
but I was not able to make it work since it only type hint the constructor
I also tried to do some tricks with typing.Literal
and the class annotations but it did not work either
4