Skip to content

Commit 38e94de

Browse files
committed
validate for missing using full state
1 parent fcc2329 commit 38e94de

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

pydantic_settings/main.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def _settings_build_values(
405405
if sources:
406406
state: dict[str, Any] = {}
407407
states: dict[str, dict[str, Any]] = {}
408-
all_line_errors: list[dict[str, Any]] = []
408+
all_line_errors: list[InitErrorDetails] = []
409409
for source in sources:
410410
if isinstance(source, PydanticBaseSettingsSource):
411411
source._set_current_state(state)
@@ -425,16 +425,29 @@ def _settings_build_values(
425425
except ValidationError as e:
426426
line_errors = json.loads(e.json())
427427
for line in line_errors:
428+
if line.get("type", "") == "missing":
429+
continue
428430
ctx = line.get("ctx", {})
429431
ctx["source"] = source_name
430432
line['ctx'] = ctx
431-
all_line_errors.extend(line_errors)
433+
details = InitErrorDetails(**line)
434+
all_line_errors.append(details)
435+
436+
if validate_each_source:
437+
try:
438+
_ = super(BaseSettings, self).__init__(**state)
439+
except ValidationError as e:
440+
line_errors = json.loads(e.json())
441+
for line in line_errors:
442+
if line.get("type", "") != "missing":
443+
continue
444+
details = InitErrorDetails(**line)
445+
all_line_errors.append(details)
432446

433447
if all_line_errors and validate_each_source:
434448
raise ValidationError.from_exception_data(
435449
title=self.__class__.__name__,
436-
line_errors=[InitErrorDetails(**l) for l in all_line_errors],
437-
input_type="python"
450+
line_errors=all_line_errors
438451
)
439452
return state
440453
else:

0 commit comments

Comments
 (0)