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
Using the union operator | within a model's Field type, breaks in pydantic with TypeError: type 'types.UnionType' is not subscriptable
Current Behavior
Add a model with the union operator:
classMedia(Model):
value: str|None=None
The code currently fails with:
class Media(Model):
File "/Users/c718230/.virtualenvs/rg-bff-backend/lib/python3.11/site-packages/odmantic/model.py", line 490, in __new__
mcs.__validate_cls_namespace__(name, namespace)
File "/Users/c718230/.virtualenvs/rg-bff-backend/lib/python3.11/site-packages/odmantic/model.py", line 334, in __validate_cls_namespace__
parse_obj_as(field_type, value)
File "pydantic/tools.py", line 37, in pydantic.tools.parse_obj_as
File "pydantic/tools.py", line 30, in pydantic.tools._get_parsing_type
File "pydantic/main.py", line 1026, in pydantic.main.create_model
File "pydantic/main.py", line 178, in pydantic.main.ModelMetaclass.__new__
File "pydantic/typing.py", line 408, in pydantic.typing.resolve_annotations
File "/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 377, in _eval_type
t = t.__origin__[args]
~~~~~~~~~~~~^^^^^^
TypeError: type 'types.UnionType' is not subscriptable
On the other hand using Optional[str] works as expected.
Expected behavior
Should have worked normally
Environment
ODMantic version: 0.9.2
MongoDB version: 6.0.6
Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):
I had the same issue. Not sure if it's odmantic or pydantic related, but here's a workaround that works for me in case you have something that's a union of 2 types other than NoneType:
fromtypingimportUnionclassFoo(Model):
bar: Union[int, float] =None
Bug
Using the union operator
|
within a model's Field type, breaks in pydantic withTypeError: type 'types.UnionType' is not subscriptable
Current Behavior
Add a model with the union operator:
The code currently fails with:
On the other hand using
Optional[str]
works as expected.Expected behavior
Should have worked normally
Environment
python -c "import pydantic.utils; print(pydantic.utils.version_info())
):Additional context
NA
EDIT
Confirmed the issue is also on normal models
The text was updated successfully, but these errors were encountered: