Skip to content

Commit 590525f

Browse files
committed
prepend source to loc field
1 parent 6232d4d commit 590525f

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

pydantic_settings/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,7 @@ def _settings_build_values(
427427
for line in line_errors:
428428
if line.get("type", "") == "missing":
429429
continue
430-
ctx = line.get("ctx", {})
431-
ctx["source"] = source_name
432-
line['ctx'] = ctx
430+
line['loc'] = [source_name] + line['loc']
433431
details = InitErrorDetails(**line)
434432
all_line_errors.append(details)
435433

tests/test_multi_source.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,20 @@ def settings_customise_sources(
5858

5959
assert exc_info.value.errors(include_url=False) == [
6060
{
61-
'ctx': {'source': 'JsonConfigSettingsSource'},
6261
'input': 0,
63-
'loc': ('foobar',),
62+
'loc': ('JsonConfigSettingsSource', 'foobar',),
6463
'msg': 'Input should be a valid string',
6564
'type': 'string_type',
6665
},
6766
{
68-
'ctx': {'source': 'EnvSettingsSource'},
6967
'input': 'a',
70-
'loc': ('nested', 'nested_field'),
68+
'loc': ('EnvSettingsSource', 'nested', 'nested_field'),
7169
'msg': 'Input should be a valid integer, unable to parse string as an integer',
7270
'type': 'int_parsing'
7371
},
7472
{
75-
'ctx': {'source': 'InitSettingsSource'},
7673
'input': 0,
77-
'loc': ('null_field',),
74+
'loc': ('InitSettingsSource', 'null_field',),
7875
'msg': 'Input should be a valid string',
7976
'type': 'string_type'
8077
},

tests/test_settings.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ class AnnotatedComplexSettings(BaseSettings, validate_each_source=True):
485485
AnnotatedComplexSettings()
486486
assert exc_info.value.errors(include_url=False) == [
487487
{
488-
'ctx': {'actual_length': 1, 'field_type': 'List', 'min_length': 2, 'source': 'EnvSettingsSource'},
488+
'ctx': {'actual_length': 1, 'field_type': 'List', 'min_length': 2},
489489
'input': ['russet'],
490-
'loc': ('apples',),
490+
'loc': ('EnvSettingsSource', 'apples',),
491491
'msg': 'List should have at least 2 items after validation, not 1',
492492
'type': 'too_short',
493493
}
@@ -1141,10 +1141,9 @@ class Settings(BaseSettings):
11411141
assert exc_info.value.errors(include_url=False) == [
11421142
{
11431143
'type': 'extra_forbidden',
1144-
'loc': ('f',),
1144+
'loc': ('DotEnvSettingsSource', 'f',),
11451145
'msg': 'Extra inputs are not permitted',
1146-
'input': 'random value',
1147-
'ctx': {'source': 'DotEnvSettingsSource'}
1146+
'input': 'random value'
11481147
}
11491148
]
11501149

0 commit comments

Comments
 (0)