1414from pydantic ._internal ._utils import deep_update , is_model_class
1515from pydantic .dataclasses import is_pydantic_dataclass
1616from pydantic .main import BaseModel
17- from pydantic_core import ValidationError , InitErrorDetails
17+ from pydantic_core import InitErrorDetails , ValidationError
1818
1919from .sources import (
2020 ENV_FILE_SENTINEL ,
@@ -417,15 +417,13 @@ def _settings_build_values(
417417 states [source_name ] = source_state
418418 state = deep_update (source_state , state )
419419
420- if validate_each_source :
421- if not source_state :
422- continue
420+ if source_state and validate_each_source :
423421 try :
424422 _ = super ().__init__ (** source_state )
425423 except ValidationError as e :
426424 line_errors = json .loads (e .json ())
427425 for line in line_errors :
428- if line .get (" type" , "" ) == 'missing' :
426+ if line .get (' type' , '' ) == 'missing' :
429427 continue
430428 line ['loc' ] = [source_name ] + line ['loc' ]
431429 ctx = line .get ('ctx' , {})
@@ -438,16 +436,14 @@ def _settings_build_values(
438436 _ = super ().__init__ (** state )
439437 except ValidationError as e :
440438 line_errors = json .loads (e .json ())
441- for line in line_errors :
442- if line .get ('type' , '' ) != 'missing' :
443- continue
444- all_line_errors .append (line )
445-
446- if all_line_errors and validate_each_source :
447- raise ValidationError .from_exception_data (
448- title = self .__class__ .__name__ ,
449- line_errors = all_line_errors
450- )
439+ all_line_errors .extend ([line for line in line_errors if line .get ('type' , '' ) == 'missing' ])
440+
441+ if all_line_errors :
442+ raise ValidationError .from_exception_data (
443+ title = self .__class__ .__name__ ,
444+ line_errors = all_line_errors
445+ )
446+
451447 return state
452448 else :
453449 # no one should mean to do this, but I think returning an empty dict is marginally preferable
0 commit comments