From b058afc615be0fb98b3d6c50daebb97489ac5929 Mon Sep 17 00:00:00 2001 From: Arian Amiri Date: Sat, 17 Feb 2024 16:43:33 -0500 Subject: [PATCH 1/3] ignore pyenv .python-version file --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 61ff0da5..2c9b24f2 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,7 @@ src/dependency_injector/providers/*.so # Workspace for samples .workspace/ + + +# pyenv +.python-version From 3bb4eea9c8b1dc6847b83359d1af8a3ca99f09b9 Mon Sep 17 00:00:00 2001 From: Arian Amiri Date: Sat, 17 Feb 2024 16:49:20 -0500 Subject: [PATCH 2/3] Support pydantic 2 settings In pydantic 2, the BaseSettings class was exported to a standalone package. This change preserves support pydantic 1 while adding support pydantic 2. All error related to loading pydantic settings reflect the structure of pydantic 2. --- setup.py | 4 +-- src/dependency_injector/providers.pyx | 52 ++++++++++++++++++--------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index 3edd5c08..25c56b3a 100644 --- a/setup.py +++ b/setup.py @@ -75,8 +75,8 @@ def _open(filename): "yaml": [ "pyyaml", ], - "pydantic": [ - "pydantic", + "pydantic-settings": [ + "pydantic-settings", ], "flask": [ "flask", diff --git a/src/dependency_injector/providers.pyx b/src/dependency_injector/providers.pyx index 8208ad63..508a82b8 100644 --- a/src/dependency_injector/providers.pyx +++ b/src/dependency_injector/providers.pyx @@ -48,6 +48,13 @@ try: except ImportError: yaml = None + +try: + import pydantic_settings +except ImportError: + pydantic_settings = None + + try: import pydantic except ImportError: @@ -61,6 +68,17 @@ from .errors import ( cimport cython +if pydantic_settings: + pydantic_settings_pkg = pydantic_settings + using_pydantic_2 = True +elif pydantic and pydantic.version.VERSION.startswith("1"): + pydantic_settings_pkg = pydantic.settings + using_pydantic_2 = False +else: + pydantic_settings_pkg = None + using_pydantic_2 = None + + if sys.version_info[0] == 3: # pragma: no cover CLASS_TYPES = (type,) else: # pragma: no cover @@ -1796,26 +1814,27 @@ cdef class ConfigurationOption(Provider): :rtype: None """ - if pydantic is None: + if pydantic_settings_pkg is None: raise Error( - "Unable to load pydantic configuration - pydantic is not installed. " - "Install pydantic or install Dependency Injector with pydantic extras: " - "\"pip install dependency-injector[pydantic]\"" + "Unable to load pydantic-settings configuration - pydantic-settings is not installed. " + "Install pydantic-settings or install Dependency Injector with pydantic-settings extras: " + "\"pip install dependency-injector[pydantic-settings]\"" ) - if isinstance(settings, CLASS_TYPES) and issubclass(settings, pydantic.BaseSettings): + if isinstance(settings, CLASS_TYPES) and issubclass(settings, pydantic_settings_pkg.BaseSettings): raise Error( "Got settings class, but expect instance: " "instead \"{0}\" use \"{0}()\"".format(settings.__name__) ) - if not isinstance(settings, pydantic.BaseSettings): + if not isinstance(settings, pydantic_settings_pkg.BaseSettings): raise Error( - "Unable to recognize settings instance, expect \"pydantic.BaseSettings\", " + "Unable to recognize settings instance, expect \"pydantic_settings.BaseSettings\", " "got {0} instead".format(settings) ) - self.from_dict(settings.dict(**kwargs), required=required) + settings_dict = settings.model_dump(**kwargs) if using_pydantic_2 else settings.dict(**kwargs) + self.from_dict(settings_dict, required=required) def from_dict(self, options, required=UNDEFINED): """Load configuration from the dictionary. @@ -2365,26 +2384,27 @@ cdef class Configuration(Object): :rtype: None """ - if pydantic is None: + if pydantic_settings_pkg is None: raise Error( - "Unable to load pydantic configuration - pydantic is not installed. " - "Install pydantic or install Dependency Injector with pydantic extras: " - "\"pip install dependency-injector[pydantic]\"" + "Unable to load pydantic-settings configuration - pydantic-settings is not installed. " + "Install pydantic-settings or install Dependency Injector with pydantic-settings extras: " + "\"pip install dependency-injector[pydantic-settings]\"" ) - if isinstance(settings, CLASS_TYPES) and issubclass(settings, pydantic.BaseSettings): + if isinstance(settings, CLASS_TYPES) and issubclass(settings, pydantic_settings_pkg.BaseSettings): raise Error( "Got settings class, but expect instance: " "instead \"{0}\" use \"{0}()\"".format(settings.__name__) ) - if not isinstance(settings, pydantic.BaseSettings): + if not isinstance(settings, pydantic_settings_pkg.BaseSettings): raise Error( - "Unable to recognize settings instance, expect \"pydantic.BaseSettings\", " + "Unable to recognize settings instance, expect \"pydantic_settings.BaseSettings\", " "got {0} instead".format(settings) ) - self.from_dict(settings.dict(**kwargs), required=required) + settings_dict = settings.model_dump(**kwargs) if using_pydantic_2 else settings.dict(**kwargs) + self.from_dict(settings_dict, required=required) def from_dict(self, options, required=UNDEFINED): """Load configuration from the dictionary. From cace03daba5a3e03667ace4aa1819d94c8c0b031 Mon Sep 17 00:00:00 2001 From: Arian Amiri Date: Sun, 18 Feb 2024 16:28:52 -0500 Subject: [PATCH 3/3] Fixed some usages in documentation and tests --- docs/providers/configuration.rst | 14 +++++++------- .../configuration/configuration_pydantic.py | 9 ++++++--- .../configuration/configuration_pydantic_init.py | 7 ++++--- requirements-dev.txt | 2 +- tests/typing/configuration.py | 2 +- .../configuration/test_from_pydantic_py36.py | 5 +++-- tox.ini | 2 +- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/docs/providers/configuration.rst b/docs/providers/configuration.rst index 3e4696f1..1b2c3099 100644 --- a/docs/providers/configuration.rst +++ b/docs/providers/configuration.rst @@ -183,7 +183,7 @@ See also: :ref:`configuration-envs-interpolation`. Loading from a Pydantic settings -------------------------------- -``Configuration`` provider can load configuration from a ``pydantic`` settings object using the +``Configuration`` provider can load configuration from a ``pydantic-settings`` Settings object using the :py:meth:`Configuration.from_pydantic` method: .. literalinclude:: ../../examples/providers/configuration/configuration_pydantic.py @@ -191,14 +191,14 @@ Loading from a Pydantic settings :lines: 3- :emphasize-lines: 31 -To get the data from pydantic settings ``Configuration`` provider calls ``Settings.dict()`` method. +To get the data from pydantic settings ``Configuration`` provider calls ``Settings.model_dump()`` method. If you need to pass an argument to this call, use ``.from_pydantic()`` keyword arguments. .. code-block:: python container.config.from_pydantic(Settings(), exclude={"optional"}) -Alternatively, you can provide a ``pydantic`` settings object over the configuration provider argument. In that case, +Alternatively, you can provide a ``pydantic-settings`` Settings object over the configuration provider argument. In that case, the container will call ``config.from_pydantic()`` automatically: .. code-block:: python @@ -215,15 +215,15 @@ the container will call ``config.from_pydantic()`` automatically: .. note:: - ``Dependency Injector`` doesn't install ``pydantic`` by default. + ``Dependency Injector`` doesn't install ``pydantic-settings`` by default. You can install the ``Dependency Injector`` with an extra dependency:: - pip install dependency-injector[pydantic] + pip install dependency-injector[pydantic-settings] - or install ``pydantic`` directly:: + or install ``pydantic-settings`` directly:: - pip install pydantic + pip install pydantic-settings *Don't forget to mirror the changes in the requirements file.* diff --git a/examples/providers/configuration/configuration_pydantic.py b/examples/providers/configuration/configuration_pydantic.py index aaed5d26..84c6d52e 100644 --- a/examples/providers/configuration/configuration_pydantic.py +++ b/examples/providers/configuration/configuration_pydantic.py @@ -2,8 +2,11 @@ import os +from typing import Annotated + from dependency_injector import containers, providers -from pydantic import BaseSettings, Field +from pydantic import Field +from pydantic_settings import BaseSettings # Emulate environment variables os.environ["AWS_ACCESS_KEY_ID"] = "KEY" @@ -12,8 +15,8 @@ class AwsSettings(BaseSettings): - access_key_id: str = Field(env="aws_access_key_id") - secret_access_key: str = Field(env="aws_secret_access_key") + access_key_id: str = Field(alias="aws_access_key_id") + secret_access_key: str = Field(alias="aws_secret_access_key") class Settings(BaseSettings): diff --git a/examples/providers/configuration/configuration_pydantic_init.py b/examples/providers/configuration/configuration_pydantic_init.py index f904d9df..981ef3f9 100644 --- a/examples/providers/configuration/configuration_pydantic_init.py +++ b/examples/providers/configuration/configuration_pydantic_init.py @@ -3,7 +3,8 @@ import os from dependency_injector import containers, providers -from pydantic import BaseSettings, Field +from pydantic import Field +from pydantic_settings import BaseSettings # Emulate environment variables os.environ["AWS_ACCESS_KEY_ID"] = "KEY" @@ -12,8 +13,8 @@ class AwsSettings(BaseSettings): - access_key_id: str = Field(env="aws_access_key_id") - secret_access_key: str = Field(env="aws_secret_access_key") + access_key_id: str = Field(alias="aws_access_key_id") + secret_access_key: str = Field(alias="aws_secret_access_key") class Settings(BaseSettings): diff --git a/requirements-dev.txt b/requirements-dev.txt index 2c101e8c..8b4ff967 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,7 +11,7 @@ mypy pyyaml httpx fastapi -pydantic +pydantic-settings numpy scipy boto3 diff --git a/tests/typing/configuration.py b/tests/typing/configuration.py index 386689d2..5a0d3d28 100644 --- a/tests/typing/configuration.py +++ b/tests/typing/configuration.py @@ -1,7 +1,7 @@ from pathlib import Path from dependency_injector import providers -from pydantic import BaseSettings as PydanticSettings +from pydantic_settings import BaseSettings as PydanticSettings # Test 1: to check the getattr diff --git a/tests/unit/providers/configuration/test_from_pydantic_py36.py b/tests/unit/providers/configuration/test_from_pydantic_py36.py index f5a2c97e..dfb05e0a 100644 --- a/tests/unit/providers/configuration/test_from_pydantic_py36.py +++ b/tests/unit/providers/configuration/test_from_pydantic_py36.py @@ -1,6 +1,7 @@ """Configuration.from_pydantic() tests.""" import pydantic +import pydantic_settings from dependency_injector import providers, errors from pytest import fixture, mark, raises @@ -13,7 +14,7 @@ class Section12(pydantic.BaseModel): value2 = 2 -class Settings1(pydantic.BaseSettings): +class Settings1(pydantic_settings.BaseSettings): section1 = Section11() section2 = Section12() @@ -27,7 +28,7 @@ class Section3(pydantic.BaseModel): value3 = 3 -class Settings2(pydantic.BaseSettings): +class Settings2(pydantic_settings.BaseSettings): section1 = Section21() section3 = Section3() diff --git a/tox.ini b/tox.ini index f436345c..034e9b89 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,7 @@ deps= mypy_boto3_s3 extras= yaml - pydantic + pydantic-settings flask aiohttp commands = pytest -c tests/.configs/pytest.ini