66
77import json
88from collections .abc import Callable
9- from typing import Any
9+ from typing import Annotated , Any
1010
1111import pydantic
1212import pytest
@@ -74,12 +74,10 @@ class M1(BaseCustomSettings):
7474 VALUE_NULLABLE_DEFAULT_VALUE : S | None = S (S_VALUE = 42 )
7575 VALUE_NULLABLE_DEFAULT_NULL : S | None = None
7676
77- VALUE_NULLABLE_DEFAULT_ENV : S | None = Field (
78- json_schema_extra = {"auto_default_from_env" : True }
79- )
80- VALUE_DEFAULT_ENV : S = Field (
81- json_schema_extra = {"auto_default_from_env" : True }
82- )
77+ VALUE_NULLABLE_DEFAULT_ENV : Annotated [
78+ S | None , Field (auto_default_from_env = True )
79+ ]
80+ VALUE_DEFAULT_ENV : Annotated [S , Field (auto_default_from_env = True )]
8381
8482 class M2 (BaseCustomSettings ):
8583 #
@@ -91,14 +89,12 @@ class M2(BaseCustomSettings):
9189 VALUE_NULLABLE_DEFAULT_NULL : S | None = None
9290
9391 # defaults enabled but if not exists, it disables
94- VALUE_NULLABLE_DEFAULT_ENV : S | None = Field (
95- json_schema_extra = { "auto_default_from_env" : True }
96- )
92+ VALUE_NULLABLE_DEFAULT_ENV : Annotated [
93+ S | None , Field ( auto_default_from_env = True )
94+ ]
9795
9896 # cannot be disabled
99- VALUE_DEFAULT_ENV : S = Field (
100- json_schema_extra = {"auto_default_from_env" : True }
101- )
97+ VALUE_DEFAULT_ENV : Annotated [S , Field (auto_default_from_env = True )]
10298
10399 # Changed in version 3.7: Dictionary order is guaranteed to be insertion order
104100 _classes = {"M1" : M1 , "M2" : M2 , "S" : S }
@@ -108,7 +104,7 @@ class M2(BaseCustomSettings):
108104
109105
110106def test_create_settings_class (
111- create_settings_class : Callable [[str ], type [BaseCustomSettings ]]
107+ create_settings_class : Callable [[str ], type [BaseCustomSettings ]],
112108):
113109 M = create_settings_class ("M1" )
114110
@@ -216,9 +212,12 @@ def test_auto_default_to_none_logs_a_warning(
216212
217213 class SettingsClass (BaseCustomSettings ):
218214 VALUE_NULLABLE_DEFAULT_NULL : S | None = None
219- VALUE_NULLABLE_DEFAULT_ENV : S | None = Field (
220- json_schema_extra = {"auto_default_from_env" : True },
221- )
215+ VALUE_NULLABLE_DEFAULT_ENV : Annotated [
216+ S | None ,
217+ Field (
218+ auto_default_from_env = True ,
219+ ),
220+ ] = None
222221
223222 instance = SettingsClass .create_from_envs ()
224223 assert instance .VALUE_NULLABLE_DEFAULT_NULL is None
@@ -245,9 +244,12 @@ def test_auto_default_to_not_none(
245244
246245 class SettingsClass (BaseCustomSettings ):
247246 VALUE_NULLABLE_DEFAULT_NULL : S | None = None
248- VALUE_NULLABLE_DEFAULT_ENV : S | None = Field (
249- json_schema_extra = {"auto_default_from_env" : True },
250- )
247+ VALUE_NULLABLE_DEFAULT_ENV : Annotated [
248+ S | None ,
249+ Field (
250+ auto_default_from_env = True ,
251+ ),
252+ ] = None
251253
252254 instance = SettingsClass .create_from_envs ()
253255 assert instance .VALUE_NULLABLE_DEFAULT_NULL is None
@@ -342,7 +344,7 @@ def test_issubclass_type_error_with_pydantic_models():
342344
343345 # here reproduces the problem with our settings that ANE and PC had
344346 class SettingsClassThatFailed (BaseCustomSettings ):
345- FOO : dict [str , str ] | None = Field ( default = None )
347+ FOO : dict [str , str ] | None = None
346348
347349 SettingsClassThatFailed (FOO = {})
348350 assert SettingsClassThatFailed (FOO = None ) == SettingsClassThatFailed ()
@@ -352,9 +354,7 @@ def test_upgrade_failure_to_pydantic_settings_2_6(
352354 mock_env_devel_environment : EnvVarsDict ,
353355):
354356 class ProblematicSettings (BaseCustomSettings ):
355- WEBSERVER_EMAIL : SMTPSettings | None = Field (
356- json_schema_extra = {"auto_default_from_env" : True }
357- )
357+ WEBSERVER_EMAIL : SMTPSettings | None = None
358358
359359 model_config = SettingsConfigDict (nested_model_default_partial_update = True )
360360
0 commit comments