I am not sure if this is what OP is talking about, but this is something I have also run across when trying to type hint alternate constructors:
class Parser:
def __init__(self, grammar: str, *, option_1: int=..., option_2: str=..., option3: Literal[...] = ...):
....
@classmethod
def from_file(cls, grammar_path: PathLike | str, **kwargs) -> Self:
return cls(open(grammar_path).read(), **kwargs)
How to tell the type checkers that kwargs
should be the same as the keywords for __init__
?
This is a slight special case where the *args
part changes. But even when that doesn’t change: How to do this?