3
3
import dataclasses
4
4
import inspect
5
5
import re
6
- from argparse import SUPPRESS
6
+ from argparse import SUPPRESS , ArgumentParser
7
7
from contextlib import suppress
8
8
from typing import Any , Callable , List , Optional , Set , Tuple , Type , Union
9
9
@@ -255,7 +255,7 @@ def _add_signature_arguments(
255
255
## Create group if requested ##
256
256
doc_group = get_doc_short_description (function_or_class , method_name , logger = self .logger )
257
257
component = getattr (function_or_class , method_name ) if method_name else function_or_class
258
- group = self ._create_group_if_requested (
258
+ container = self ._create_group_if_requested (
259
259
component ,
260
260
nested_key ,
261
261
as_group ,
@@ -268,7 +268,7 @@ def _add_signature_arguments(
268
268
added_args : List [str ] = []
269
269
for param in params :
270
270
self ._add_signature_parameter (
271
- group ,
271
+ container ,
272
272
nested_key ,
273
273
param ,
274
274
added_args ,
@@ -283,7 +283,7 @@ def _add_signature_arguments(
283
283
284
284
def _add_signature_parameter (
285
285
self ,
286
- group ,
286
+ container ,
287
287
nested_key : Optional [str ],
288
288
param ,
289
289
added_args : List [str ],
@@ -339,11 +339,14 @@ def _add_signature_parameter(
339
339
dest = (nested_key + "." if nested_key else "" ) + name
340
340
args = [dest if is_required and as_positional else "--" + dest ]
341
341
if param .origin :
342
+ parser = container
343
+ if not isinstance (container , ArgumentParser ):
344
+ parser = getattr (container , "parser" )
342
345
group_name = "; " .join (str (o ) for o in param .origin )
343
- if group_name in group . parser .groups :
344
- group = group . parser .groups [group_name ]
346
+ if group_name in parser .groups :
347
+ container = parser .groups [group_name ]
345
348
else :
346
- group = group . parser .add_argument_group (
349
+ container = parser .add_argument_group (
347
350
f"Conditional arguments [origins: { group_name } ]" ,
348
351
name = group_name ,
349
352
)
@@ -372,7 +375,7 @@ def _add_signature_parameter(
372
375
args = args ,
373
376
kwargs = kwargs ,
374
377
enable_path = enable_path ,
375
- container = group ,
378
+ container = container ,
376
379
logger = self .logger ,
377
380
sub_add_kwargs = sub_add_kwargs ,
378
381
)
@@ -387,7 +390,7 @@ def _add_signature_parameter(
387
390
if is_dataclass_like_typehint :
388
391
kwargs .update (sub_add_kwargs )
389
392
with ActionTypeHint .allow_default_instance_context ():
390
- action = group .add_argument (* args , ** kwargs )
393
+ action = container .add_argument (* args , ** kwargs )
391
394
action .sub_add_kwargs = sub_add_kwargs
392
395
if is_subclass_typehint and len (subclass_skip ) > 0 :
393
396
action .sub_add_kwargs ["skip" ] = subclass_skip
0 commit comments