You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Union passthrough does not work when target union uses a new-style type alias as element for passthrough #742
I have just found an issue with the configure_union_passthrough strategy, when using the 3.12 type alias as an element of a union to structure.
Here is my attempt at an MRE, tested on Python 3.12:
importdataclassesimporttypingimportcattrsimportcattrs.strategiesconverter=cattrs.Converter()
cattrs.strategies.configure_union_passthrough(str|int, converter)
@dataclasses.dataclassclassDataClass:
field: str
type NewScalar=strOldScalar: typing.TypeAlias=strif__name__=="__main__":
data= {"field": "value"}
v1=converter.structure(data, OldScalar|DataClass) # This one worksprint(v1)
v2=converter.structure(data, NewScalar|DataClass) # This one does not workprint(v2)
The line before last raises cattrs.errors.StructureHandlerNotFoundError: Unsupported type: NewScalar | __main__.DataClass. Register a structure hook for it.
Given the way that new-style type aliases can be nested, I wonder if that would basically require a function that "resolves" union by going through the layers of TypeAliasType values.
Activity
Tinche commentedon Apr 1, 2026
Got it. We should probably be smart here and support both type aliases and newtypes out of the box. I'll see what I can do in the next few days.
jhominal commentedon Apr 2, 2026
Given the way that new-style type aliases can be nested, I wonder if that would basically require a function that "resolves" union by going through the layers of
TypeAliasTypevalues.The thing that worries me here is the way that a bugfix interacts with the way that
cattrscurrently documents hooks that resolve specifically on a new-style type alias.