@@ -1051,6 +1051,7 @@ def validate(
10511051 skip_none : bool = True ,
10521052 skip_required : bool = False ,
10531053 branch : Optional [str ] = None ,
1054+ ** kwargs ,
10541055 ) -> None :
10551056 """Checks that the content of a given configuration object conforms with the parser.
10561057
@@ -1064,13 +1065,14 @@ def validate(
10641065 TypeError: If any of the values are not valid.
10651066 KeyError: If a key in cfg is not defined in the parser.
10661067 """
1068+ prefix = get_private_kwargs (kwargs , _prefix = "" )
10671069 cfg = ccfg = cfg .clone ()
10681070 if isinstance (branch , str ):
10691071 branch_cfg = cfg
10701072 cfg = Namespace ()
10711073 cfg [branch ] = branch_cfg
10721074
1073- def check_required (cfg , parser , prefix = "" ):
1075+ def check_required (cfg , parser , prefix ):
10741076 for reqkey in parser .required_args :
10751077 try :
10761078 val = cfg [reqkey ]
@@ -1082,7 +1084,7 @@ def check_required(cfg, parser, prefix=""):
10821084 ) from ex
10831085 subcommand , subparser = _ActionSubCommands .get_subcommand (parser , cfg , fail_no_subcommand = False )
10841086 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 + "." )
10861088
10871089 def check_values (cfg ):
10881090 sorted_keys = {k : _find_action (self , k ) for k in cfg .get_sorted_keys ()}
@@ -1119,10 +1121,10 @@ def check_values(cfg):
11191121 raise NSKeyError (f"Key '{ key } ' is not expected" )
11201122
11211123 try :
1122- if not skip_required and not lenient_check .get ():
1123- check_required (cfg , self )
11241124 with parser_context (load_value_mode = self .parser_mode ):
11251125 check_values (cfg )
1126+ if not skip_required and not lenient_check .get ():
1127+ check_required (cfg , self , prefix )
11261128 except (TypeError , KeyError ) as ex :
11271129 prefix = "Validation failed: "
11281130 message = ex .args [0 ]
@@ -1391,7 +1393,7 @@ def _check_value_key(self, action: argparse.Action, value: Any, key: str, cfg: O
13911393 if leaf_key == action .dest :
13921394 return value
13931395 subparser = action ._name_parser_map [leaf_key ] # type: ignore[attr-defined]
1394- subparser .validate (value )
1396+ subparser .validate (value , _prefix = key + "." )
13951397 elif isinstance (action , _ActionConfigLoad ):
13961398 if isinstance (value , str ):
13971399 value = action .check_type (value , self )
0 commit comments