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
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
The text was updated successfully, but these errors were encountered:
Description
cattrs fails when attempting to structure data using a class with an Annotated union type.
What I Did
The code:
and the output:
The text was updated successfully, but these errors were encountered: