Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support TypeAliasType for structuring #478

Closed
declaresub opened this issue Dec 29, 2023 · 1 comment
Closed

Support TypeAliasType for structuring #478

declaresub opened this issue Dec 29, 2023 · 1 comment
Milestone

Comments

@declaresub
Copy link
Contributor

  • cattrs version: 23.2.3
  • Python version: 3.12.0
  • Operating System: Mac OS 14.2

Structuring in cattrs supports union types represented by a TypeAlias, but not defined via type. Here's a bit of example code.

import attrs
import cattrs.preconf.json
from typing import Literal, TypeAlias

converter = cattrs.preconf.json.make_converter()
@attrs.define
class Foo:
    _type: Literal['foo'] = 'foo'

@attrs.define
class Bar:
    _type: Literal['bar'] = 'bar'

FooBar: TypeAlias = Foo | Bar

data = {'_type': 'foo'}
obj = converter.structure(data, FooBar)

This does what one expects. But the next bit raises a StructureHandlerNotFoundError.

type FooBar1 = Foo | Bar
obj = converter.structure(data, FooBar1)

It would be nice if structure also accepted TypeAliasTypes.

Even though I know better, I suggest that implementation looks straightforward -- I made this change in converters,py around line 330:


    def structure(self, obj: Any, cl: Type[T]) -> T:
        from typing import TypeAliasType

        _cl = cl
        while isinstance(_cl, TypeAliasType):
            _cl = _cl.__value__

        """Convert unstructured Python data structures to structured data."""
        return self._structure_func.dispatch(_cl)(obj, _cl)

after which I was able to structure data with type FooBar1. I note that I was unable to construct a type for which the while loop above would fail to terminate.

@Tinche
Copy link
Member

Tinche commented Dec 30, 2023

Howdy,

I agree the new type aliases are really cool. I've landed support for them on the main branch a few weeks ago, so you can either wait for the 24.1.0 release, use the main branch (I try to keep it very stable) or look at #452 for the changes to apply to a converter to have it support type aliases.

Almost all problems in cattrs are solved by registering hooks and this one is no different ;)

@Tinche Tinche closed this as completed Dec 30, 2023
@Tinche Tinche added this to the 24.1 milestone Dec 30, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants