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

Support pydantic v2 BaseSettings #755

Closed
arianamiri opened this issue Oct 25, 2023 · 6 comments
Closed

Support pydantic v2 BaseSettings #755

arianamiri opened this issue Oct 25, 2023 · 6 comments
Labels

Comments

@arianamiri
Copy link

In pydantic 2.0+ the BaseSettings class has been moved into a optional package, pydantic-settings. The configuration provider's from_pydantic method expect to find it in the pydantic package:

https://github.com/ets-labs/python-dependency-injector/blob/master/src/dependency_injector/providers.pyx#L1806-L1816

This results in an error like:

    container.config.from_pydantic(settings)
src/dependency_injector/providers.pyx:2381: in dependency_injector.providers.Configuration.from_pydantic
    ???
.venv/lib/python3.11/site-packages/pydantic/__init__.py:218: in __getattr__
    return _getattr_migration(attr_name)
.venv/lib/python3.11/site-packages/pydantic/_migration.py:294: in wrapper
    raise PydanticImportError(
E   pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.4/migration/#basesettings-has-moved-to-pydantic-settings for more details.

Furthermore, the BaseModel.dict method used in the last line of this method is deprecated and has been replaced by BaseModel.model_dump. The method still exists but emits deprecation warnings when accessed.

@arianamiri arianamiri changed the title Support pydantic v3 BaseSettings Support pydantic v2 BaseSettings Oct 25, 2023
@tsaryapkin
Copy link

I created PR covering this issue.

@cdonate
Copy link

cdonate commented Jan 10, 2024

A work around that I used for this would be:

config = providers.Configuration() 
json_config = Settings().model_dump(mode='json') 
config.from_dict(json_config)

@pow3rpi
Copy link

pow3rpi commented Jun 28, 2024

The same issue

Temporary solved with:

config = providers.Configuration()
config.from_dict(AppSecrets().dict())

@bmcandr
Copy link

bmcandr commented Jul 2, 2024

FYI for anyone else who lands here: @cdonate's workaround is compatible with Pydantic v2.

While @pow3rpi's workaround is technically compatible with Pydantic v2, the .dict() and .json() model serialization methods are marked as deprecated in v2 and will eventually be removed in favor of .model_dump() and .model_dump_json().

@pow3rpi
Copy link

pow3rpi commented Jul 2, 2024

@bmcandr Good point! Thanks.

@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
@ZipFile ZipFile added the feature label 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

6 participants