Skip to content

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

Closed

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.

Activity

  1. Tinche commented on Apr 1, 2026

    @Tinche
    Member

    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.

  2. jhominal commented on Apr 2, 2026

    @jhominal
    Author

    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.

    The thing that worries me here is the way that a bugfix interacts with the way that cattrs currently documents hooks that resolve specifically on a new-style type alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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