48
48
except ImportError :
49
49
yaml = None
50
50
51
+
52
+ try :
53
+ import pydantic_settings
54
+ except ImportError :
55
+ pydantic_settings = None
56
+
57
+
51
58
try :
52
59
import pydantic
53
60
except ImportError :
@@ -61,6 +68,17 @@ from .errors import (
61
68
cimport cython
62
69
63
70
71
+ if pydantic_settings:
72
+ pydantic_settings_pkg = pydantic_settings
73
+ using_pydantic_2 = True
74
+ elif pydantic and pydantic.version.VERSION.startswith(" 1" ):
75
+ pydantic_settings_pkg = pydantic.settings
76
+ using_pydantic_2 = False
77
+ else :
78
+ pydantic_settings_pkg = None
79
+ using_pydantic_2 = None
80
+
81
+
64
82
if sys.version_info[0 ] == 3 : # pragma: no cover
65
83
CLASS_TYPES = (type ,)
66
84
else : # pragma: no cover
@@ -1796,26 +1814,27 @@ cdef class ConfigurationOption(Provider):
1796
1814
1797
1815
:rtype: None
1798
1816
"""
1799
- if pydantic is None :
1817
+ if pydantic_settings_pkg is None :
1800
1818
raise Error(
1801
- " Unable to load pydantic configuration - pydantic is not installed. "
1802
- " Install pydantic or install Dependency Injector with pydantic extras: "
1803
- " \" pip install dependency-injector[pydantic]\" "
1819
+ " Unable to load pydantic-settings configuration - pydantic-settings is not installed. "
1820
+ " Install pydantic-settings or install Dependency Injector with pydantic-settings extras: "
1821
+ " \" pip install dependency-injector[pydantic-settings ]\" "
1804
1822
)
1805
1823
1806
- if isinstance (settings, CLASS_TYPES) and issubclass (settings, pydantic .BaseSettings):
1824
+ if isinstance (settings, CLASS_TYPES) and issubclass (settings, pydantic_settings_pkg .BaseSettings):
1807
1825
raise Error(
1808
1826
" Got settings class, but expect instance: "
1809
1827
" instead \" {0}\" use \" {0}()\" " .format(settings.__name__ )
1810
1828
)
1811
1829
1812
- if not isinstance (settings, pydantic .BaseSettings):
1830
+ if not isinstance (settings, pydantic_settings_pkg .BaseSettings):
1813
1831
raise Error(
1814
- " Unable to recognize settings instance, expect \" pydantic .BaseSettings\" , "
1832
+ " Unable to recognize settings instance, expect \" pydantic_settings .BaseSettings\" , "
1815
1833
" got {0} instead" .format(settings)
1816
1834
)
1817
1835
1818
- self .from_dict(settings.dict(** kwargs), required = required)
1836
+ settings_dict = settings.model_dump(** kwargs) if using_pydantic_2 else settings.dict(** kwargs)
1837
+ self .from_dict(settings_dict, required = required)
1819
1838
1820
1839
def from_dict (self , options , required = UNDEFINED):
1821
1840
""" Load configuration from the dictionary.
@@ -2365,26 +2384,27 @@ cdef class Configuration(Object):
2365
2384
2366
2385
:rtype: None
2367
2386
"""
2368
- if pydantic is None :
2387
+ if pydantic_settings_pkg is None :
2369
2388
raise Error(
2370
- " Unable to load pydantic configuration - pydantic is not installed. "
2371
- " Install pydantic or install Dependency Injector with pydantic extras: "
2372
- " \" pip install dependency-injector[pydantic]\" "
2389
+ " Unable to load pydantic-settings configuration - pydantic-settings is not installed. "
2390
+ " Install pydantic-settings or install Dependency Injector with pydantic-settings extras: "
2391
+ " \" pip install dependency-injector[pydantic-settings ]\" "
2373
2392
)
2374
2393
2375
- if isinstance (settings, CLASS_TYPES) and issubclass (settings, pydantic .BaseSettings):
2394
+ if isinstance (settings, CLASS_TYPES) and issubclass (settings, pydantic_settings_pkg .BaseSettings):
2376
2395
raise Error(
2377
2396
" Got settings class, but expect instance: "
2378
2397
" instead \" {0}\" use \" {0}()\" " .format(settings.__name__ )
2379
2398
)
2380
2399
2381
- if not isinstance (settings, pydantic .BaseSettings):
2400
+ if not isinstance (settings, pydantic_settings_pkg .BaseSettings):
2382
2401
raise Error(
2383
- " Unable to recognize settings instance, expect \" pydantic .BaseSettings\" , "
2402
+ " Unable to recognize settings instance, expect \" pydantic_settings .BaseSettings\" , "
2384
2403
" got {0} instead" .format(settings)
2385
2404
)
2386
2405
2387
- self .from_dict(settings.dict(** kwargs), required = required)
2406
+ settings_dict = settings.model_dump(** kwargs) if using_pydantic_2 else settings.dict(** kwargs)
2407
+ self .from_dict(settings_dict, required = required)
2388
2408
2389
2409
def from_dict (self , options , required = UNDEFINED):
2390
2410
""" Load configuration from the dictionary.
0 commit comments