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

from pydantic import RootModel fails in Python 3.10.0 #6912

Closed
1 task done
randomir opened this issue Jul 27, 2023 · 2 comments · Fixed by #6919
Closed
1 task done

from pydantic import RootModel fails in Python 3.10.0 #6912

randomir opened this issue Jul 27, 2023 · 2 comments · Fixed by #6919
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@randomir
Copy link
Contributor

randomir commented Jul 27, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

The problem

RootModel import on Python 3.10.0 (and only 3.10.0) fails with:

...
  File "<snipped>/lib/python3.10/site-packages/pydantic/root_model.py", line 26, in <module>
    class RootModel(BaseModel, typing.Generic[RootModelRootType]):
  File "<snipped>/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 172, in __new__
    set_model_fields(cls, bases, config_wrapper, types_namespace)
  File "<snipped>/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 400, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
  File "<snipped>/lib/python3.10/site-packages/pydantic/_internal/_fields.py", line 98, in collect_model_fields
    type_hints = get_cls_type_hints_lenient(cls, types_namespace)
  File "<snipped>/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py", line 212, in get_cls_type_hints_lenient
    hints[name] = eval_type_lenient(value, globalns, localns)
  File "<snipped>/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py", line 221, in eval_type_lenient
    value = _make_forward_ref(value, is_argument=False, is_class=True)
TypeError: ForwardRef.__init__() got an unexpected keyword argument 'is_class'

The problem does not exist on 3.10.1+.

Seems like the reason is already described here, but for Python 3.9.

The fix

Since Pydantic depends on bpo-45166 fix to get_type_hints, and that fix was only included in 3.10.1 (was not present in 3.10.0), the hard-coded python version for 3.9 branch here:

if sys.version_info < (3, 9, 8):

should be expanded to include the bpo-45166 fix in the 3.10 branch as well.

Example Code

from pydantic import RootModel

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=true mimalloc=true
                 install path: /<omitted>/lib/python3.10/site-packages/pydantic
               python version: 3.10.0 (default, Nov 24 2021, 12:59:45) [GCC 5.4.0 20160609]
                     platform: Linux-4.15.0-142-generic-x86_64-with-glibc2.23
     optional deps. installed: ['typing-extensions']

Selected Assignee: @Kludex

@randomir randomir added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 27, 2023
randomir added a commit to randomir/pydantic that referenced this issue Jul 27, 2023
randomir added a commit to randomir/pydantic that referenced this issue Jul 27, 2023
@dmontagu
Copy link
Contributor

dmontagu commented Jul 27, 2023

It seems to me the fix might be changing the line to:

if sys.version_info < (3, 9, 8) or sys.version_info == (3, 10, 0): 

Is that what you meant?

If so, @randomir If you have a python 3.10.0 environment handy, would you mind testing and opening a PR for this?

(If that's wrong, PR still welcome to fix it.)

Thanks!

@randomir
Copy link
Contributor Author

You're right @dmontagu, I opened #6919. Verified it in 3.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
3 participants