@@ -1051,6 +1051,7 @@ def validate(
1051
1051
skip_none : bool = True ,
1052
1052
skip_required : bool = False ,
1053
1053
branch : Optional [str ] = None ,
1054
+ ** kwargs ,
1054
1055
) -> None :
1055
1056
"""Checks that the content of a given configuration object conforms with the parser.
1056
1057
@@ -1064,13 +1065,14 @@ def validate(
1064
1065
TypeError: If any of the values are not valid.
1065
1066
KeyError: If a key in cfg is not defined in the parser.
1066
1067
"""
1068
+ prefix = get_private_kwargs (kwargs , _prefix = "" )
1067
1069
cfg = ccfg = cfg .clone ()
1068
1070
if isinstance (branch , str ):
1069
1071
branch_cfg = cfg
1070
1072
cfg = Namespace ()
1071
1073
cfg [branch ] = branch_cfg
1072
1074
1073
- def check_required (cfg , parser , prefix = "" ):
1075
+ def check_required (cfg , parser , prefix ):
1074
1076
for reqkey in parser .required_args :
1075
1077
try :
1076
1078
val = cfg [reqkey ]
@@ -1082,7 +1084,7 @@ def check_required(cfg, parser, prefix=""):
1082
1084
) from ex
1083
1085
subcommand , subparser = _ActionSubCommands .get_subcommand (parser , cfg , fail_no_subcommand = False )
1084
1086
if subcommand is not None and subparser is not None :
1085
- check_required (cfg .get (subcommand ), subparser , subcommand + "." )
1087
+ check_required (cfg .get (subcommand ), subparser , prefix + subcommand + "." )
1086
1088
1087
1089
def check_values (cfg ):
1088
1090
sorted_keys = {k : _find_action (self , k ) for k in cfg .get_sorted_keys ()}
@@ -1119,10 +1121,10 @@ def check_values(cfg):
1119
1121
raise NSKeyError (f"Key '{ key } ' is not expected" )
1120
1122
1121
1123
try :
1122
- if not skip_required and not lenient_check .get ():
1123
- check_required (cfg , self )
1124
1124
with parser_context (load_value_mode = self .parser_mode ):
1125
1125
check_values (cfg )
1126
+ if not skip_required and not lenient_check .get ():
1127
+ check_required (cfg , self , prefix )
1126
1128
except (TypeError , KeyError ) as ex :
1127
1129
prefix = "Validation failed: "
1128
1130
message = ex .args [0 ]
@@ -1391,7 +1393,7 @@ def _check_value_key(self, action: argparse.Action, value: Any, key: str, cfg: O
1391
1393
if leaf_key == action .dest :
1392
1394
return value
1393
1395
subparser = action ._name_parser_map [leaf_key ] # type: ignore[attr-defined]
1394
- subparser .validate (value )
1396
+ subparser .validate (value , _prefix = key + "." )
1395
1397
elif isinstance (action , _ActionConfigLoad ):
1396
1398
if isinstance (value , str ):
1397
1399
value = action .check_type (value , self )
0 commit comments