@@ -274,14 +274,24 @@ class Settings(BaseSettings):
274
274
for name , value in field_values .items ():
275
275
sub_model_field : FieldInfo | None = None
276
276
277
+ annotation = field .annotation
278
+
279
+ # If field is Optional, we need to find the actual type
280
+ args = get_args (annotation )
281
+ if origin_is_union (get_origin (field .annotation )) and len (args ) == 2 and type (None ) in args :
282
+ for arg in args :
283
+ if arg != type (None ):
284
+ annotation = arg
285
+ break
286
+
277
287
# This is here to make mypy happy
278
288
# Item "None" of "Optional[Type[Any]]" has no attribute "model_fields"
279
- if not field . annotation or not hasattr (field . annotation , 'model_fields' ):
289
+ if not annotation or not hasattr (annotation , 'model_fields' ):
280
290
values [name ] = value
281
291
continue
282
292
283
293
# Find field in sub model by looking in fields case insensitively
284
- for sub_model_field_name , f in field . annotation .model_fields .items ():
294
+ for sub_model_field_name , f in annotation .model_fields .items ():
285
295
if not f .validation_alias and sub_model_field_name .lower () == name .lower ():
286
296
sub_model_field = f
287
297
break
@@ -337,7 +347,7 @@ def __call__(self) -> dict[str, Any]:
337
347
field_value = None
338
348
if (
339
349
not self .case_sensitive
340
- and lenient_issubclass (field .annotation , BaseModel )
350
+ # and lenient_issubclass(field.annotation, BaseModel)
341
351
and isinstance (field_value , dict )
342
352
):
343
353
data [field_key ] = self ._replace_field_names_case_insensitively (field , field_value )
0 commit comments