File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed
pydantic_settings/sources/providers Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -956,7 +956,7 @@ def _add_parser_args(
956
956
957
957
self ._convert_bool_flag (arg .kwargs , field_info , model_default )
958
958
959
- if arg .is_parser_submodel :
959
+ if arg .is_parser_submodel and not getattr ( field_info . annotation , '__pydantic_root_model__' , False ) :
960
960
self ._add_parser_submodels (
961
961
parser ,
962
962
model ,
@@ -1107,6 +1107,7 @@ def _add_parser_submodels(
1107
1107
model_group_kwargs ['description' ] = CLI_SUPPRESS
1108
1108
if not self .cli_avoid_json :
1109
1109
added_args .append (arg_names [0 ])
1110
+ kwargs ['required' ] = False
1110
1111
kwargs ['nargs' ] = '?'
1111
1112
kwargs ['const' ] = '{}'
1112
1113
kwargs ['help' ] = (
@@ -1205,8 +1206,12 @@ def _metavar_format_recurse(self, obj: Any) -> str:
1205
1206
)
1206
1207
elif obj is type (None ):
1207
1208
return self .cli_parse_none_str
1208
- elif is_model_class (obj ):
1209
- return 'JSON'
1209
+ elif is_model_class (obj ) or is_pydantic_dataclass (obj ):
1210
+ return (
1211
+ self ._metavar_format_recurse (_get_model_fields (obj )['root' ].annotation )
1212
+ if getattr (obj , '__pydantic_root_model__' , False )
1213
+ else 'JSON'
1214
+ )
1210
1215
elif isinstance (obj , type ):
1211
1216
return obj .__qualname__
1212
1217
else :
Original file line number Diff line number Diff line change 19
19
DirectoryPath ,
20
20
Discriminator ,
21
21
Field ,
22
+ RootModel ,
22
23
Tag ,
23
24
ValidationError ,
24
25
field_validator ,
@@ -1778,20 +1779,31 @@ class Settings(BaseSettings):
1778
1779
1779
1780
1780
1781
def test_cli_enforce_required (env ):
1782
+ class MyRootModel (RootModel [str ]):
1783
+ root : str
1784
+
1781
1785
class Settings (BaseSettings , cli_exit_on_error = False ):
1782
1786
my_required_field : str
1787
+ my_root_model_required_field : MyRootModel
1783
1788
1784
1789
env .set ('MY_REQUIRED_FIELD' , 'hello from environment' )
1790
+ env .set ('MY_ROOT_MODEL_REQUIRED_FIELD' , 'hi from environment' )
1785
1791
1786
1792
assert Settings (_cli_parse_args = [], _cli_enforce_required = False ).model_dump () == {
1787
- 'my_required_field' : 'hello from environment'
1793
+ 'my_required_field' : 'hello from environment' ,
1794
+ 'my_root_model_required_field' : 'hi from environment' ,
1788
1795
}
1789
1796
1790
1797
with pytest .raises (
1791
1798
SettingsError , match = 'error parsing CLI: the following arguments are required: --my_required_field'
1792
1799
):
1793
1800
Settings (_cli_parse_args = [], _cli_enforce_required = True ).model_dump ()
1794
1801
1802
+ with pytest .raises (
1803
+ SettingsError , match = 'error parsing CLI: the following arguments are required: --my_root_model_required_field'
1804
+ ):
1805
+ Settings (_cli_parse_args = ['--my_required_field' , 'hello from cli' ], _cli_enforce_required = True ).model_dump ()
1806
+
1795
1807
1796
1808
def test_cli_exit_on_error (capsys , monkeypatch ):
1797
1809
class Settings (BaseSettings , cli_parse_args = True ): ...
You can’t perform that action at this time.
0 commit comments