Skip to content

from pydantic import RootModel fails in Python 3.10.0 #6912

Closed
@randomir

Description

@randomir

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

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2pendingIs unconfirmed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions