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

Compatibility Issue with Pydantic 2 #726

Closed
Pentusha opened this issue Jul 17, 2023 · 8 comments
Closed

Compatibility Issue with Pydantic 2 #726

Pentusha opened this issue Jul 17, 2023 · 8 comments
Assignees
Labels

Comments

@Pentusha
Copy link

The major version of Pydantic was recently released and lost backward compatibility. In particular, the .from_pydantic method stopped working for providers.Configuration due to the fact that the BaseSettings class now needs to be imported from a new package called pydantic-settings.

[tool.poetry.dependencies]
python = "^3.11"
pydantic = "^2.0.3"
pydantic-settings = "^2.0.2"
Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 112, in _get_module_details
  File "/home/pentusha/projects/kyt.service-template/app/__init__.py", line 4, in <module>
    from app.core.containers import app_container
  File "/home/pentusha/projects/kyt.service-template/app/core/containers.py", line 80, in <module>
    app_container = AppContainer()
                    ^^^^^^^^^^^^^^
  File "src/dependency_injector/containers.pyx", line 742, in dependency_injector.containers.DeclarativeContainer.__new__
  File "src/dependency_injector/containers.pyx", line 393, in dependency_injector.containers.DynamicContainer.load_config
  File "src/dependency_injector/providers.pyx", line 2106, in dependency_injector.providers.Configuration.load
  File "src/dependency_injector/providers.pyx", line 2381, in dependency_injector.providers.Configuration.from_pydantic
  File "/home/pentusha/.cache/pypoetry/virtualenvs/kyt-service-template-cl7ERtKI-py3.11/lib/python3.11/site-packages/pydantic/__init__.py", line 207, in __getattr__
    return _getattr_migration(attr_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pentusha/.cache/pypoetry/virtualenvs/kyt-service-template-cl7ERtKI-py3.11/lib/python3.11/site-packages/pydantic/_migration.py", line 288, in wrapper
    raise PydanticImportError(
pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.0.3/migration/#basesettings-has-moved-to-pydantic-settings for more details.
@Pentusha
Copy link
Author

Pentusha commented Jul 18, 2023

If anyone is interested in a workaround with monkeypatch:

import pydantic
from pydantic_settings import BaseSettings

pydantic.BaseSettings = BaseSettings


class AppContainer(containers.DeclarativeContainer):
    config = providers.Configuration(strict=True)
    env = providers.Configuration(strict=True, pydantic_settings=[env_secrets])
    ...

@ZipFile
Copy link
Contributor

ZipFile commented Jul 26, 2023

You do not really need to monkeypatch anything:

-container.config.from_pydantic(settings)
+container.config.from_dict(settings.model_dump())

from_pydantic did essentially the same:

self.from_dict(settings.dict(**kwargs), required=required)

@akalex
Copy link

akalex commented Jul 31, 2023

Hey @rmk135,
Any chance that this issue will be considered soon?
Many thanks for considering this request.

@abdalazizrashid
Copy link

Is this package maintained?

@Pentusha
Copy link
Author

@abdalazizrashid seems like no. You can switch to my fork, but only python3.12 compatibility was introduces compared to original repo.

@rmk135 rmk135 self-assigned this Nov 11, 2024
@rmk135 rmk135 added the feature label Nov 11, 2024
@rmk135
Copy link
Member

rmk135 commented Nov 13, 2024

Hi all! I looked into this issue today. I have a working example with pydantic version 2 on my machine. It will take some time to productize it, update the docs, etc. I think it's possible to make it work seamlessly with both pydantic version 1 and version 2.

@rmk135
Copy link
Member

rmk135 commented Nov 13, 2024

FYI all, there has been a nice PR already created for this: #768 (comment)

It has some merge conflicts, but we can make it work one way or another.

@ZipFile
Copy link
Contributor

ZipFile commented Dec 8, 2024

Fixed in #832, available since v4.44.0.

@ZipFile ZipFile closed this as completed Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants