Creating types with functions/classes

Hello everyone! I want to see what's your opinion on allowing functions to return types and allow them to be used by a mypy and other type checkers. For context, I have a library that allows to annotate function arguments with some metadata, and it looks similar to this: ```python def argument(name: str, description: str) -> dict: return {"name": name, "description": description} def get_user_name(id_: Annotated[int, argument(name="id", description="this should be an uuid")]) -> str | None: return None ``` this works fine, but I was wondering if we could do something (or allow) like this: ```python def argument(type_: T, *, name: str, description: str) -> T: return Annotated[type_, {"name": name, "description": description}] def get_user_name(id_: argument(int, name="id", description="this should be an uuid")) -> str | None: return None ``` it might seem like a minor change, but to me it feels like it could improve the dx of my library 😊 Just to clarify, the goal is not to allow dynamic types, it is more to have a convenient way of making an alias to lazy type Let me know what you think

Type annotations cannot contain complex expression forms like function calls. This is for good reason because type evaluation of call expressions can be very expensive, and different type checkers can produce different results (e.g. based on unspecified behaviors in overload resolution, heuristics used during TypeVar solving, etc.). For those reasons, I don't think this proposal would work. The technique you're using currently (your first code sample) is the approach I recommend. -- Eric Traut Contributor to Pyright & Pylance Microsoft

Type annotations cannot contain complex expression forms like function calls. This is for good reason because type evaluation of call expressions can be very expensive, and different type checkers can produce different results (e.g. based on unspecified behaviors in overload resolution, heuristics used during TypeVar solving, etc.). For those reasons, I don't think this proposal would work. The technique you're using currently (your first code sample) is the approach I recommend. -- Eric Traut Contributor to Pyright & Pylance Microsoft
participants (2)
-
Eric Traut
-
Patrick Arminio