-
-
Notifications
You must be signed in to change notification settings - Fork 61
Fix serialization of unions with nested PEP 695 type aliases #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| "is_dataclass_dict_mixin_subclass", | ||
| "collect_type_params", | ||
| "resolve_type_params", | ||
| "resolve_type_alias_type", | ||
| "substitute_type_params", | ||
| "get_generic_name", | ||
| "get_name_error_name", | ||
|
|
@@ -643,6 +644,20 @@ def substitute_type_params(typ: Type, substitutions: dict[Type, Type]) -> Type: | |
| return typ | ||
|
|
||
|
|
||
| def resolve_type_alias_type(typ: Type) -> Type: | ||
| while True: | ||
| if is_type_alias_type(typ): | ||
| typ = typ.__value__ | ||
| elif is_type_alias_type(get_type_origin(typ)): | ||
| origin = get_type_origin(typ) | ||
| type_params = getattr(origin, "__type_params__", ()) | ||
| args = get_args(typ) | ||
| param_map = dict(zip(type_params, args)) | ||
| typ = substitute_type_params(origin.__value__, param_map) | ||
| else: | ||
| return typ | ||
|
Comment on lines
+647
to
+685
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added cycle protection in 829f7c8: the resolver tracks seen values and bounds the number of resolution steps, raising |
||
|
|
||
|
|
||
| def get_name_error_name(e: NameError) -> str: | ||
| return e.name # type: ignore | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced the silent truncation in 829f7c8: missing trailing parameters now fall back to their PEP 696 defaults when defined (
TypeAliasTypesubscription doesn't auto-fill defaults, soPair[int]withTypeVar("V", default=str)previously leaked an unsubstituted~Vinto the resolved type), and a genuine mismatch raisesTypeError: Too few/Too many arguments for ...; actual N, expected Min the same format as the existing generics arity check. The check is skipped for TypeVarTuple/ParamSpec parameters since their subscription arity is flexible. One behavior note: invalid annotations likePair[int]without a default used to serialize with a half-substituted value and now fail at class creation — happy to relax that if you'd rather keep them lenient.