-
Notifications
You must be signed in to change notification settings - Fork 91
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Bug
When creating a simple model with a field that is the union of two types - it fails to build the model in Python3.11 but passes in Python3.10.
from odmantic import Model
class Example(Model):
my_special_field: str | None = None
Current Behavior
The Python3.10 output of running just example contains no error.
❯ python --version
Python 3.10.14
❯ python playground.py
But when running with Python3.11 - an error is raised about inability to parse the type annotation.
❯ python --version
Python 3.11.9
❯ python playground.py
Traceback (most recent call last):
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 279, in eval_type_backport
return _eval_type_backport(value, globalns, localns, type_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 303, in _eval_type_backport
return _eval_type(value, globalns, localns, type_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 332, in _eval_type
return typing._eval_type( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 406, in _eval_type
t = t.__origin__[args]
~~~~~~~~~~~~^^^^^^
TypeError: type 'types.UnionType' is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/ryanozawa/coding/odmantic/playground.py", line 9, in <module>
class Failing(Model):
File "/Users/ryanozawa/coding/odmantic/odmantic/model.py", line 493, in __new__
return super().__new__(mcs, name, bases, namespace, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/odmantic/model.py", line 411, in __new__
cls = super().__new__(mcs, name, bases, namespace, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 219, in __new__
set_model_fields(cls, bases, config_wrapper, types_namespace)
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 512, in set_model_fields
fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_fields.py", line 105, in collect_model_fields
type_hints = get_cls_type_hints_lenient(cls, types_namespace)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 245, in get_cls_type_hints_lenient
hints[name] = eval_type_lenient(value, globalns, localns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 257, in eval_type_lenient
return eval_type_backport(value, globalns, localns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 286, in eval_type_backport
assert isinstance(value, typing.ForwardRef)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
Expected behavior
Python3.11 output should be the same as for Python3.10 - no errors.
Environment
- ODMantic version: 1.0.2
- MongoDB version: N/A
- Pydantic infos (output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())
):
pydantic version: 2.9.2
pydantic-core version: 2.23.4
pydantic-core build: profile=release pgo=false
install path: /Users/ryanozawa/coding/odmantic/.venv/lib/python3.11/site-packages/pydantic
python version: 3.11.9 (main, Apr 2 2024, 08:25:04) [Clang 15.0.0 (clang-1500.3.9.4)]
platform: macOS-14.6.1-arm64-arm-64bit
related packages: fastapi-0.115.0 typing_extensions-4.12.2
commit: unknown
Additional context
This seems related to how model is constructed in odmantic.model
and is raised in Python3.11 due to changes in how types are evaluated from standard lib.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working