Skip to content

Commit fcc2329

Browse files
committed
fix appending source to context
1 parent 12d5537 commit fcc2329

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pydantic_settings/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,14 @@ def _settings_build_values(
426426
line_errors = json.loads(e.json())
427427
for line in line_errors:
428428
ctx = line.get("ctx", {})
429-
ctx = {"source": source_name}
429+
ctx["source"] = source_name
430430
line['ctx'] = ctx
431431
all_line_errors.extend(line_errors)
432432

433433
if all_line_errors and validate_each_source:
434434
raise ValidationError.from_exception_data(
435435
title=self.__class__.__name__,
436-
line_errors=[InitErrorDetails(**l) for l in all_line_errors],
436+
line_errors=[InitErrorDetails(**l) for l in all_line_errors],
437437
input_type="python"
438438
)
439439
return state

tests/test_settings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,28 @@ class AnnotatedComplexSettings(BaseSettings):
472472
]
473473

474474

475+
def test_annotated_list_with_error_source(env):
476+
class AnnotatedComplexSettings(BaseSettings, validate_each_source=True):
477+
apples: Annotated[List[str], MinLen(2)] = []
478+
479+
env.set('apples', '["russet", "granny smith"]')
480+
s = AnnotatedComplexSettings()
481+
assert s.apples == ['russet', 'granny smith']
482+
483+
env.set('apples', '["russet"]')
484+
with pytest.raises(ValidationError) as exc_info:
485+
AnnotatedComplexSettings()
486+
assert exc_info.value.errors(include_url=False) == [
487+
{
488+
'ctx': {'actual_length': 1, 'field_type': 'List', 'min_length': 2, 'source': 'EnvSettingsSource'},
489+
'input': ['russet'],
490+
'loc': ('apples',),
491+
'msg': 'List should have at least 2 items after validation, not 1',
492+
'type': 'too_short',
493+
}
494+
]
495+
496+
475497
def test_set_dict_model(env):
476498
env.set('bananas', '[1, 2, 3, 3]')
477499
env.set('CARROTS', '{"a": null, "b": 4}')

0 commit comments

Comments
 (0)