|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Mapping, Union |
| 4 | + |
| 5 | +from typing_extensions import Annotated, get_args, get_origin |
| 6 | + |
| 7 | +from polyfactory.utils.predicates import ( |
| 8 | + is_generic_alias, |
| 9 | + is_type_alias, |
| 10 | + is_type_var, |
| 11 | + is_union, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def normalize_type(type_annotation: Any) -> Any: |
| 16 | + """Convert modern Python 3.12+ type syntax to standard annotations. |
| 17 | +
|
| 18 | + Handles TypeAliasType and GenericAlias types introduced in Python 3.12+, |
| 19 | + converting them to standard type annotations when needed. |
| 20 | +
|
| 21 | + Args: |
| 22 | + type_annotation: Type to normalize (convert if needed or pass through). |
| 23 | +
|
| 24 | + Returns: |
| 25 | + Normalized type annotation with resolved type aliases and substituted parameters. |
| 26 | +
|
| 27 | + Example: |
| 28 | + ```python |
| 29 | + # Python 3.12+ |
| 30 | + >> from typing import Annotated |
| 31 | + >> import annotated_types as at |
| 32 | +
|
| 33 | + >> type NegativeInt = Annotated[int, at.Lt(0)] |
| 34 | + >> type NonEmptyList[T] = Annotated[list[T], at.Len(1)] |
| 35 | +
|
| 36 | + >> normalize_type(NonEmptyList[NegativeInt]) |
| 37 | + # typing.Annotated[list[typing.Annotated[int, Lt(lt=0)]], Len(min_length=1, max_length=None)] |
| 38 | + ``` |
| 39 | + """ |
| 40 | + |
| 41 | + if is_type_alias(type_annotation): |
| 42 | + return type_annotation.__value__ |
| 43 | + |
| 44 | + if not is_generic_alias(type_annotation): |
| 45 | + return type_annotation |
| 46 | + |
| 47 | + origin = get_origin(type_annotation) |
| 48 | + args = get_args(type_annotation) |
| 49 | + |
| 50 | + if is_type_alias(origin): |
| 51 | + return __handle_generic_type_alias(origin, args) |
| 52 | + |
| 53 | + if args: |
| 54 | + normalized_args = tuple(normalize_type(arg) for arg in args) |
| 55 | + if normalized_args != args: |
| 56 | + return origin[normalized_args[0] if len(normalized_args) == 1 else normalized_args] |
| 57 | + |
| 58 | + return type_annotation |
| 59 | + |
| 60 | + |
| 61 | +def __handle_generic_type_alias(origin: Any, args: tuple) -> Any: |
| 62 | + """Handle generic type alias with parameters.""" |
| 63 | + template = origin.__value__ |
| 64 | + type_params = origin.__type_params__ |
| 65 | + |
| 66 | + if not (type_params and args): |
| 67 | + return template |
| 68 | + |
| 69 | + normalized_args = tuple(normalize_type(arg) for arg in args) |
| 70 | + substitutions = dict(zip(type_params, normalized_args)) |
| 71 | + |
| 72 | + if get_origin(template) is Annotated: |
| 73 | + base_type, *metadata = get_args(template) |
| 74 | + template_result = Annotated[tuple([__apply_substitutions(base_type, substitutions)] + metadata)] # type: ignore[valid-type] |
| 75 | + else: |
| 76 | + template_result = __apply_substitutions(template, substitutions) |
| 77 | + |
| 78 | + return template_result |
| 79 | + |
| 80 | + |
| 81 | +def __apply_substitutions(target: Any, subs: Mapping[Any, Any]) -> Any: |
| 82 | + if is_type_var(target): |
| 83 | + return subs.get(target, target) |
| 84 | + |
| 85 | + if is_union(target): |
| 86 | + args = tuple(__apply_substitutions(arg, subs) for arg in get_args(target)) |
| 87 | + return Union[args] |
| 88 | + |
| 89 | + origin = get_origin(target) |
| 90 | + args = get_args(target) |
| 91 | + |
| 92 | + if is_type_alias(origin): |
| 93 | + sub_args = tuple(__apply_substitutions(arg, subs) for arg in args) if args else () |
| 94 | + return normalize_type(origin[sub_args] if sub_args else origin) |
| 95 | + |
| 96 | + if origin and args: |
| 97 | + sub_args = tuple(__apply_substitutions(arg, subs) for arg in args) |
| 98 | + return origin[sub_args[0] if len(sub_args) == 1 else sub_args] |
| 99 | + |
| 100 | + return target |
0 commit comments