Skip to content

Commit 907a132

Browse files
committed
Tests.
1 parent 43cc284 commit 907a132

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

pydantic_settings/sources/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) ->
5454

5555
origin = get_origin(annotation)
5656

57+
# Check if annotation is of the form Union[type, ...].
58+
if typing_objects.is_union(origin):
59+
return _union_is_complex(annotation, metadata)
60+
5761
# Check if annotation is of the form Annotated[type, metadata].
5862
if typing_objects.is_annotated(origin):
5963
# Return result of recursive call on inner type.

tests/test_settings.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,21 @@ class AnnotatedComplexSettings(BaseSettings):
474474
]
475475

476476

477+
def test_cli_nested_annotated_unions(env):
478+
class Cat(BaseModel):
479+
meow: str
480+
481+
class Dog(BaseModel):
482+
woof: str
483+
484+
class Settings(BaseSettings):
485+
model_config = SettingsConfigDict(env_nested_delimiter='__')
486+
animals: Annotated[Union[Annotated[Union[Cat, Dog], 'my_nested_annotation'], None], 'my_annotation']
487+
488+
env.set('ANIMALS__MEOW', 'hiss')
489+
assert Settings().model_dump() == {'animals': {'meow': 'hiss'}}
490+
491+
477492
def test_set_dict_model(env):
478493
env.set('bananas', '[1, 2, 3, 3]')
479494
env.set('CARROTS', '{"a": null, "b": 4}')

tests/test_source_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,8 @@ class Dog(BaseModel):
12851285
class Settings(BaseSettings):
12861286
animals: CliSubCommand[Annotated[Union[Cat, Dog], 'my_annotation']]
12871287

1288-
assert CliApp.run(Settings, cli_args=['Cat', '--meow=purr']).model_dump == {'animals': {'meow': 'purr'}}
1289-
assert CliApp.run(Settings, cli_args=['Dog', '--woof=bark']).model_dump == {'animals': {'woof': 'bark'}}
1288+
assert CliApp.run(Settings, cli_args=['Cat', '--meow=purr']).model_dump() == {'animals': {'meow': 'purr'}}
1289+
assert CliApp.run(Settings, cli_args=['Dog', '--woof=bark']).model_dump() == {'animals': {'woof': 'bark'}}
12901290

12911291

12921292
def test_cli_union_similar_sub_models():

0 commit comments

Comments
 (0)