Skip to content

Union passthrough does not work when target union uses a new-style type alias as element for passthrough #742

@jhominal

Description

@jhominal

Hello,

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:

import dataclasses
import typing

import cattrs
import cattrs.strategies


converter = cattrs.Converter()
cattrs.strategies.configure_union_passthrough(str | int, converter)


@dataclasses.dataclass
class DataClass:
    field: str


type NewScalar = str
OldScalar: typing.TypeAlias = str

if __name__ == "__main__":
    data = {"field": "value"}

    v1 = converter.structure(data, OldScalar | DataClass)  # This one works
    print(v1)
    v2 = converter.structure(data, NewScalar | DataClass)  # This one does not work
    print(v2)

The line before last raises cattrs.errors.StructureHandlerNotFoundError: Unsupported type: NewScalar | __main__.DataClass. Register a structure hook for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions