Resolve PEP 695 type alias in the union unpacker - #344
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
The union unpacker builds its type-match condition from the raw member's __name__. For a PEP 695 type alias that is the alias name (e.g. "ScalarAlias"), which is not bound in the generated code's namespace, so the emitted `type(value) is ScalarAlias` line raises NameError before any working branch runs. Because from_dict turns that into InvalidFieldValue, the alias member breaks deserialization of the whole union for every value. Resolve the alias to its underlying type before taking __name__, exactly as the packer side already does (Fatal1ty#336). resolve_type_alias_type is a no-op for non-alias types and on Python < 3.12, so the change is safe on all supported versions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #343.
Problem
A PEP 695
typealias used as a member of a union field serializes fine but fails to deserialize —from_dictraisesInvalidFieldValuefor every value, so the type no longer round-trips:Cause
UnionUnpackerBuilder._add_bodybuilds the type-match condition from the raw member's__name__:For a
TypeAliasType,__name__is the alias name ("ScalarAlias"), which is not bound in the generated code's namespace. The emitted line becomestype(value) is ScalarAlias, raisingNameErrorbefore any working branch is reached — and sincefrom_dictconverts exceptions intoInvalidFieldValue, the alias member breaks the entire union, not just its own branch.Fix
Resolve the alias to its underlying type before taking
__name__, exactly as the packer side already does (resolve_type_alias_type, added in #336):resolve_type_alias_typeis a no-op for non-alias types and on Python < 3.12, so this is safe on all supported versions.Tests
Added
test_type_alias_type_with_union_value_unpackermirroring the existing..._packertest but assertingfrom_dict. It fails onmaster(InvalidFieldValue) and passes with the fix. The union / PEP 695 / recursive-union / discriminated-union suites pass (161 passed).black,ruff, andmypyare clean on the change.