Skip to content

Commit abc79ef

Browse files
committed
Fix Pydantic v2 deprecation warning triggering on settings class import
1 parent 1510a84 commit abc79ef

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/dependency_injector/providers.pyx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,29 @@ try:
4040
except ImportError:
4141
yaml = None
4242

43-
has_pydantic_settings = True
43+
has_pydantic_settings = False
4444
cdef bint pydantic_v1 = False
45+
cdef object PydanticSettings = None
4546
cdef str pydantic_module = "pydantic_settings"
4647
cdef str pydantic_extra = "pydantic2"
4748

4849
try:
4950
from pydantic_settings import BaseSettings as PydanticSettings
5051
except ImportError:
5152
try:
52-
# pydantic-settings requires pydantic v2,
53-
# so it is safe to assume that we're dealing with v1:
54-
from pydantic import BaseSettings as PydanticSettings
55-
pydantic_v1 = True
56-
pydantic_module = "pydantic"
57-
pydantic_extra = "pydantic"
53+
import pydantic
5854
except ImportError:
59-
# if it is present, ofc
60-
has_pydantic_settings = False
55+
pass
56+
else:
57+
# Avoid triggering deprecation warning in v2+
58+
if pydantic.VERSION.startswith("1."):
59+
PydanticSettings = pydantic.BaseSettings
60+
pydantic_v1 = True
61+
pydantic_module = "pydantic"
62+
pydantic_extra = "pydantic"
63+
has_pydantic_settings = True
64+
else:
65+
has_pydantic_settings = True
6166

6267

6368
from .errors import (

0 commit comments

Comments
 (0)