Skip to content

Commit 2e6bba3

Browse files
committed
Merge branch 'main' into cli-kebab-case
2 parents c14aada + 3f831e9 commit 2e6bba3

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

pydantic_settings/sources.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,8 +1439,8 @@ def _get_sub_models(self, model: type[BaseModel], field_name: str, field_info: F
14391439
raise SettingsError(f'CliSubCommand is not outermost annotation for {model.__name__}.{field_name}')
14401440
elif _annotation_contains_types(type_, (_CliPositionalArg,), is_include_origin=False):
14411441
raise SettingsError(f'CliPositionalArg is not outermost annotation for {model.__name__}.{field_name}')
1442-
if is_model_class(type_) or is_pydantic_dataclass(type_):
1443-
sub_models.append(type_) # type: ignore
1442+
if is_model_class(_strip_annotated(type_)) or is_pydantic_dataclass(_strip_annotated(type_)):
1443+
sub_models.append(_strip_annotated(type_))
14441444
return sub_models
14451445

14461446
def _verify_cli_flag_annotations(self, model: type[BaseModel], field_name: str, field_info: FieldInfo) -> None:

tests/test_source_azure_key_vault.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def settings_customise_sources(
115115
assert settings.sql_server_user == expected_secret_value
116116
assert settings.sql_server.password == expected_secret_value
117117

118-
def _raise_resource_not_found_when_getting_parent_secret_name(self, secret_name: str) -> KeyVaultSecret:
118+
def _raise_resource_not_found_when_getting_parent_secret_name(self, secret_name: str):
119119
expected_secret_value = 'SecretValue'
120120
key_vault_secret = KeyVaultSecret(SecretProperties(), expected_secret_value)
121121

tests/test_source_cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
BaseModel,
1616
ConfigDict,
1717
DirectoryPath,
18+
Discriminator,
1819
Field,
20+
Tag,
1921
ValidationError,
2022
)
2123
from pydantic import (
@@ -2270,6 +2272,28 @@ class MySettings(BaseSettings):
22702272
)
22712273

22722274

2275+
def test_cli_submodels_strip_annotated():
2276+
class PolyA(BaseModel):
2277+
a: int = 1
2278+
type: Literal['a'] = 'a'
2279+
2280+
class PolyB(BaseModel):
2281+
b: str = '2'
2282+
type: Literal['b'] = 'b'
2283+
2284+
def _get_type(model: Union[BaseModel, Dict]) -> str:
2285+
if isinstance(model, dict):
2286+
return model.get('type', 'a')
2287+
return model.type # type: ignore
2288+
2289+
Poly = Annotated[Union[Annotated[PolyA, Tag('a')], Annotated[PolyB, Tag('b')]], Discriminator(_get_type)]
2290+
2291+
class WithUnion(BaseSettings):
2292+
poly: Poly
2293+
2294+
assert CliApp.run(WithUnion, ['--poly.type=a']).model_dump() == {'poly': {'a': 1, 'type': 'a'}}
2295+
2296+
22732297
def test_cli_kebab_case(capsys, monkeypatch):
22742298
class DeepSubModel(BaseModel):
22752299
deep_submodel_positional_arg: CliPositionalArg[str]

0 commit comments

Comments
 (0)