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

Unable to build model for Python3.11 using '|' syntax #500

Open
rhyn0 opened this issue Oct 11, 2024 · 0 comments · May be fixed by #501
Open

Unable to build model for Python3.11 using '|' syntax #500

rhyn0 opened this issue Oct 11, 2024 · 0 comments · May be fixed by #501
Labels
bug Something isn't working

Comments

@rhyn0
Copy link

rhyn0 commented Oct 11, 2024

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.

@rhyn0 rhyn0 added the bug Something isn't working label Oct 11, 2024
rhyn0 added a commit to rhyn0/odmantic that referenced this issue Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant