-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Description
Initial Checks
- I confirm that I'm using Pydantic V2 installed directly from the
mainbranch, or equivalent
Description
I was trying to migrate my codebase from V1 to V2 (mostly by replacing import pydantic with import pydantic.v1) and noticed that pydantic.v1.parse_obj_as was not working as intended and was leading to the following error:
Traceback (most recent call last):
File "/Users/sharathhuddar/workspace/django-rest-api/core/tests/test_types.py", line 177, in test_non_https_url
parse_obj_as(HttpsUrl, url)
File "/Users/sharathhuddar/workspace/django-rest-api/django-rest-api-3.7/lib/python3.7/site-packages/pydantic/v1/tools.py", line 37, in parse_obj_as
model_type = _get_parsing_type(type_, type_name=type_name) # type: ignore[arg-type]
File "/Users/sharathhuddar/workspace/django-rest-api/django-rest-api-3.7/lib/python3.7/site-packages/pydantic/v1/tools.py", line 30, in _get_parsing_type
return create_model(type_name, __root__=(type_, ...))
File "/Users/sharathhuddar/workspace/django-rest-api/django-rest-api-3.7/lib/python3.7/site-packages/pydantic/main.py", line 1319, in create_model
return meta(__model_name, resolved_bases, namespace, __pydantic_reset_parent_namespace__=False, **kwds)
File "/Users/sharathhuddar/workspace/django-rest-api/django-rest-api-3.7/lib/python3.7/site-packages/pydantic/_internal/_model_construction.py", line 96, in __new__
namespace, config_wrapper.ignored_types, class_vars, base_field_names
File "/Users/sharathhuddar/workspace/django-rest-api/django-rest-api-3.7/lib/python3.7/site-packages/pydantic/_internal/_model_construction.py", line 279, in inspect_namespace
raise TypeError("To define root models, use `pydantic.RootModel` rather than a field called '__root__'")
TypeError: To define root models, use `pydantic.RootModel` rather than a field called '__root__'
On inspecting the source code, I noticed that parse_obj_as calls _get_parsing_type which inturn calls pydantic.main.create_model instead of pydantic.v1.main.create_model
The issue gets resolved on updating the import statement in pydantic.v1.tools._get_parsing_type: 24 from from pydantic.main import create_model to from pydantic.v1.main import create_model
Example Code
No response
Python, Pydantic & OS Version
python -c "import pydantic.version; print(pydantic.version.version_info())"
pydantic version: 2.0
pydantic-core version: 2.0.1 release build profile
install path: /Users/sharathhuddar/workspace/django-rest-api/django-rest-api-3.7/lib/python3.7/site-packages/pydantic
python version: 3.7.12 (default, Nov 22 2022, 14:45:00) [Clang 13.1.6 (clang-1316.0.21.2.5)]
platform: Darwin-22.2.0-x86_64-i386-64bit
optional deps. installed: ['email-validator', 'typing-extensions']
Selected Assignee: @lig
pydantic-hooky, codebreach, wangzelin007 and jsntcyjsntcy and wangzelin007