Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

JsonConverter.structure cannot handle Annotated union type. #418

Closed
declaresub opened this issue Aug 18, 2023 · 3 comments · Fixed by #419
Closed

JsonConverter.structure cannot handle Annotated union type. #418

declaresub opened this issue Aug 18, 2023 · 3 comments · Fixed by #419
Labels
Milestone

Comments

@declaresub
Copy link

  • cattrs version: 23.1.2
  • Python version: 3.11.4
  • Operating System: Mac OS 13.4.1 (c)

Description

cattrs fails when attempting to structure data using a class with an Annotated union type.

What I Did

The code:

from typing import Annotated
import attrs
from cattrs.preconf.json import make_converter

conv = make_converter()

@attrs.define
class GoodModel:
    name: Annotated[str, 'required']
    
@attrs.define
class BadModel:
    name: Annotated[str | None, 'required'] 

data = {'name': 'test'}
print('Loading data using GoodModel.')
conv.structure(data, GoodModel)
print('Loading data using GoodModel succeeded.')
print('Loading data using BadModel.')
conv.structure(data, BadModel)

and the output:

Loading data using GoodModel.
Loading data using GoodModel succeeded.
Loading data using BadModel.
  + Exception Group Traceback (most recent call last):
  |   File "/Users/charles/Documents/annotated_test.py", line 20, in <module>
  |     conv.structure(data, BadModel)
  |   File "/Users/charles/Documents/ccWrangler/git/ccwrangler-api/.venv/3.11.4/lib/python3.11/site-packages/cattrs/converters.py", line 334, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "<cattrs generated structure __main__.BadModel>", line 9, in structure_BadModel
  | cattrs.errors.ClassValidationError: While structuring BadModel (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<cattrs generated structure __main__.BadModel>", line 5, in structure_BadModel
    |   File "/Users/charles/Documents/ccWrangler/git/ccwrangler-api/.venv/3.11.4/lib/python3.11/site-packages/cattrs/converters.py", line 646, in _structure_optional
    |     other = union_params[0] if union_params[1] is NoneType else union_params[1]
    |                                ~~~~~~~~~~~~^^^
    | IndexError: tuple index out of range
    | Structuring class BadModel @ attribute name
@Tinche
Copy link
Member

Tinche commented Aug 18, 2023

Hm, confirmed. Should be an easy fix though, looking into it.

@Tinche Tinche added the bug label Aug 18, 2023
@Tinche Tinche added this to the 23.2 milestone Aug 18, 2023
@Tinche Tinche linked a pull request Aug 18, 2023 that will close this issue
@Tinche
Copy link
Member

Tinche commented Aug 18, 2023

Should be fixed on main!

@declaresub
Copy link
Author

I tested the fix using the code example above; all appears good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants