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

pydantic.v1.parse_obj_as internally uses pydantic.main.create_model instead of pydantic.v1.main.create_model #6361

Closed
1 task done
SharathHuddar opened this issue Jul 2, 2023 · 9 comments · Fixed by #6364
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@SharathHuddar
Copy link

SharathHuddar commented Jul 2, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2 installed directly from the main branch, 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

@SharathHuddar SharathHuddar added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 2, 2023
SharathHuddar added a commit to SharathHuddar/pydantic that referenced this issue Jul 2, 2023
`_get_parsing_type` in `pydantic.v1` was depending on `create_model` from `pydantic.main` instead of `pydantic.v1.main`. This causes the issue outlined here - pydantic#6361
@SharathHuddar
Copy link
Author

Looks like this bug crept in because V1 code that is copied over to pydantic.v1 has absolute import path. Have raised a PR with relative import path

@hramezani
Copy link
Member

Fixed in 2c0e2a6

@SharathHuddar
Copy link
Author

@hramezani when can I expect this to be released ?

@Kludex
Copy link
Member

Kludex commented Jul 3, 2023

@SharathHuddar We are going to make a release today.

@jsntcy
Copy link

jsntcy commented Jul 4, 2023

@Kludex, once the fix is released, please let us know.

@Kludex
Copy link
Member

Kludex commented Jul 4, 2023

Sorry, we didn't make it to release it yesterday, but for sure it will be released today.

You can watch our releases selecting "Watch" -> "Custom" -> "Releases":
Screenshot 2023-07-04 at 13 47 42

@jsntcy
Copy link

jsntcy commented Jul 5, 2023

@Kludex @hramezani, the issue still exists using the new version.
We use: pydantic-2.0.1 pydantic-core-2.0.2

Source code for the dependency: https://github.com/Azure/partnercenter-cli-extension/blob/main/partnercenter/setup.py#L39-L43

image

@hramezani
Copy link
Member

@jsntcy Based on the error picture that you posted, Your codebase is using Pydantic V1. We had a lot of changes in Pydantic V2. So, to fix the problem, you need to

  • Update your code base to Pydantic V2 based on migration guide
  • Or, Still using Pydantic V1. to do this you need to change the imports. e.g. from pydantic import BaseModel has to be changed to from pydantic.v1 import BaseModel
  • Or, if you are not ready to migrate to V2, pin pydantic to <2

@wangzelin007
Copy link

@jsntcy Based on the error picture that you posted, Your codebase is using Pydantic V1. We had a lot of changes in Pydantic V2. So, to fix the problem, you need to

  • Update your code base to Pydantic V2 based on migration guide
  • Or, Still using Pydantic V1. to do this you need to change the imports. e.g. from pydantic import BaseModel has to be changed to from pydantic.v1 import BaseModel
  • Or, if you are not ready to migrate to V2, pin pydantic to <2

@hramezani,
we ping pydantic to < 2, and actually installed the pydantic 1.10.11, it gives another error TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'NoneType', any suggestion?

skshetry added a commit to iterative/gto that referenced this issue Sep 14, 2023
Also supports pydantic v1.

pydantic v2 provides v1 api through `pydantic.v1` import, so we can support
both versions by trying to import from `pydantic.v1` and fallback to old behaviour.

See
https://docs.pydantic.dev/latest/migration/#continue-using-pydantic-v1-features.

pydantic == 2.0.0 does not work due to pydantic/pydantic#6361,
so we need to blacklist that specific version.
skshetry added a commit to iterative/gto that referenced this issue Sep 15, 2023
Also supports pydantic v1.

pydantic v2 provides v1 api through `pydantic.v1` import, so we can
support both versions by trying to import from `pydantic.v1` and
fallback to old behaviour.

See
https://docs.pydantic.dev/latest/migration/#continue-using-pydantic-v1-features.

pydantic == 2.0.0 does not work due to
pydantic/pydantic#6361, so we need to
blacklist that specific version.
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
6 participants