Hello! Is it possible to create dynamic __init__ signatures in Python, similar to those found in dataclasses.dataclass or pydantic.BaseModel? Here is what I have tried:
from inspect import Signature, Parameter
from typing import ClassVar
class Person:
__signature__: ClassVar[Signature]
def __init__(self, *args, **kwargs) -> None:
return
Person.__signature__ = Signature(
(
Parameter(
name="name",
kind=Parameter.KEYWORD_ONLY,
annotation=str
),
Parameter(
name="age",
kind=Parameter.KEYWORD_ONLY,
annotation=int
),
),
return_annotation=Person
)
The creation of __signature__ would obviously be done in a metaclass, I'm just doing this as an example. The problem is, even though the signature is correctly returned when using inspect.signature, tools like pylance fail to detect the signature, and tools like mypy fail to recognize invalid calls (like Person(xyz=123)).
Something else I have tried is copying __code__ and __annotations__ from a dummy __init__ function (in a metaclass's __new__), but even that doesn't work.