Skip to content

Commit e9fb316

Browse files
kschwabhramezani
andauthored
Fix for env nested enum. (#589)
Co-authored-by: Hasan Ramezani <[email protected]>
1 parent 0e9b329 commit e9fb316

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pydantic_settings/sources/providers/env.py

+3
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
242242
if (target_field or is_dict) and env_val:
243243
if target_field:
244244
is_complex, allow_json_failure = self._field_is_complex(target_field)
245+
if self.env_parse_enums:
246+
enum_val = _annotation_enum_name_to_val(target_field.annotation, env_val)
247+
env_val = env_val if enum_val is None else enum_val
245248
else:
246249
# nested field type is dict
247250
is_complex, allow_json_failure = True, True

tests/test_settings.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -2348,13 +2348,18 @@ class Settings(BaseSettings):
23482348

23492349

23502350
def test_env_parse_enums(env):
2351-
class Settings(BaseSettings):
2351+
class NestedEnum(BaseModel):
2352+
fruit: FruitsEnum
2353+
2354+
class Settings(BaseSettings, env_nested_delimiter='__'):
23522355
fruit: FruitsEnum
23532356
union_fruit: Optional[Union[int, FruitsEnum]] = None
2357+
nested: NestedEnum
23542358

23552359
with pytest.raises(ValidationError) as exc_info:
23562360
env.set('FRUIT', 'kiwi')
23572361
env.set('UNION_FRUIT', 'kiwi')
2362+
env.set('NESTED__FRUIT', 'kiwi')
23582363
s = Settings()
23592364
assert exc_info.value.errors(include_url=False) == [
23602365
{
@@ -2385,22 +2390,43 @@ class Settings(BaseSettings):
23852390
'msg': 'Input should be 0, 1 or 2',
23862391
'type': 'enum',
23872392
},
2393+
{
2394+
'ctx': {
2395+
'expected': '0, 1 or 2',
2396+
},
2397+
'input': 'kiwi',
2398+
'loc': (
2399+
'nested',
2400+
'fruit',
2401+
),
2402+
'msg': 'Input should be 0, 1 or 2',
2403+
'type': 'enum',
2404+
},
23882405
]
23892406

23902407
env.set('FRUIT', str(FruitsEnum.lime.value))
23912408
env.set('UNION_FRUIT', str(FruitsEnum.lime.value))
2409+
env.set('NESTED__FRUIT', str(FruitsEnum.lime.value))
23922410
s = Settings()
23932411
assert s.fruit == FruitsEnum.lime
2412+
assert s.union_fruit == FruitsEnum.lime
2413+
assert s.nested.fruit == FruitsEnum.lime
23942414

23952415
env.set('FRUIT', 'kiwi')
23962416
env.set('UNION_FRUIT', 'kiwi')
2417+
env.set('NESTED__FRUIT', 'kiwi')
23972418
s = Settings(_env_parse_enums=True)
23982419
assert s.fruit == FruitsEnum.kiwi
2420+
assert s.union_fruit == FruitsEnum.kiwi
2421+
assert s.nested.fruit == FruitsEnum.kiwi
23992422

24002423
env.set('FRUIT', str(FruitsEnum.lime.value))
24012424
env.set('UNION_FRUIT', str(FruitsEnum.lime.value))
2425+
env.set('NESTED__FRUIT', str(FruitsEnum.lime.value))
24022426
s = Settings(_env_parse_enums=True)
24032427
assert s.fruit == FruitsEnum.lime
2428+
assert s.union_fruit == FruitsEnum.lime
2429+
assert s.nested.fruit == FruitsEnum.lime
24042430

24052431

24062432
def test_env_parse_none_str(env):

0 commit comments

Comments
 (0)