Repository navigation
Conversation
Generic PEP 695 type aliases like `type Pair[T] = tuple[T, T]` were unstructured using the hook for their unparametrized base, so the type arguments were dropped and nested attrs classes, dataclasses and other values were left unconverted. Structuring already substituted the type arguments; unstructuring now does the same.
This branch has not been deployed
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 #838.
Generic PEP 695 type aliases such as
type Pair[T] = tuple[T, T]were unstructured with the hook for their raw value (tuple[T, T]), so their type arguments were dropped.Pair[SomeAttrsClass]ortype Registry[K, V] = dict[K, V]with attrs class or datetime values came back with the instances unconverted. The JSON converter then failed to dump the result, and other converters produced non-primitive output. Structuring already substituted the type arguments intype_alias_structure_factory.This change moves that substitution into a small helper in
cattrs/typealiases.pyand addstype_alias_unstructure_factory. The factory looks up the unstructure hook for the parametrized base.BaseConverternow registers that factory in place of the lambda that usedget_type_alias_basedirectly. Non-generic aliases behave as before.A test in
tests/test_generics_695.pycoverstype Gen1[T] = Tandtype Gen2[K, V] = dict[K, V]on both converters. OnConverter, which supports heterogeneous tuples, it also coverstype Pair[T] = tuple[T, T]and an attrs class round trip. The test fails on main and passes with the change. I ran the full test suite on Python 3.12, with all extras, and on 3.15. On 3.15, the tests that need pyyaml or immutables could not be installed, so those were not run there.ruff checkandruff formatare clean for the touched files. The one remainingruff checkfinding (RUF063 ingen/typeddicts.py) was already on main.HISTORY.md has an entry under NEXT.