|
77 | 77 | from ._paths import Path, PathError, change_to_path_dir |
78 | 78 | from ._required import clear_required |
79 | 79 | from ._subcommands import find_action, find_parent_action, parse_kwargs |
| 80 | +from ._type_checking import ArgumentParser |
80 | 81 | from ._util import ( |
81 | 82 | NestedArg, |
82 | 83 | NoneType, |
@@ -213,6 +214,61 @@ def get_parse_optional_num_return() -> int: |
213 | 214 | parse_optional_num_return = get_parse_optional_num_return() |
214 | 215 |
|
215 | 216 |
|
| 217 | +def freeze(value): |
| 218 | + if isinstance(value, dict): |
| 219 | + return tuple(sorted(((k, freeze(v)) for k, v in value.items()), key=lambda item: repr(item[0]))) |
| 220 | + if isinstance(value, set): |
| 221 | + return tuple(sorted((freeze(v) for v in value), key=repr)) |
| 222 | + if isinstance(value, (list, tuple)): |
| 223 | + return tuple(freeze(v) for v in value) |
| 224 | + return value |
| 225 | + |
| 226 | + |
| 227 | +_cached_class_parsers: dict[tuple, ArgumentParser] = {} |
| 228 | + |
| 229 | + |
| 230 | +def cached_get_class_parser(*, val_class, sub_add_kwargs, skip_args, parent_parser, nested_links): |
| 231 | + if isinstance(val_class, str): |
| 232 | + val_class = import_object(val_class) |
| 233 | + parser_class = type(parent_parser) |
| 234 | + cache_key = ( |
| 235 | + val_class, |
| 236 | + parser_class, |
| 237 | + parent_parser.parser_mode, |
| 238 | + freeze(sub_add_kwargs), |
| 239 | + freeze(skip_args), |
| 240 | + freeze(nested_links), |
| 241 | + ) |
| 242 | + if cache_key in _cached_class_parsers: |
| 243 | + parser = _cached_class_parsers[cache_key] |
| 244 | + parser.logger = parent_parser.logger |
| 245 | + return parser |
| 246 | + |
| 247 | + kwargs = dict(sub_add_kwargs) if sub_add_kwargs else {} |
| 248 | + if skip_args: |
| 249 | + kwargs.setdefault("skip", set()).update(skip_args) |
| 250 | + |
| 251 | + parser = parser_class(exit_on_error=False, logger=parent_parser.logger, parser_mode=parent_parser.parser_mode) |
| 252 | + remove_actions(parser, (ActionConfigFile, _ActionPrintConfig)) |
| 253 | + if inspect.isclass(val_class) or inspect.isclass(get_typehint_origin(val_class)): |
| 254 | + parser.add_class_arguments(val_class, **kwargs) |
| 255 | + else: |
| 256 | + kwargs = {k: v for k, v in kwargs.items() if k != "instantiate"} |
| 257 | + parser.add_function_arguments(val_class, **kwargs) |
| 258 | + |
| 259 | + if "linked_targets" in kwargs: |
| 260 | + for key in kwargs["linked_targets"]: |
| 261 | + clear_required(parser, key) |
| 262 | + |
| 263 | + for link_kwargs in nested_links: |
| 264 | + parser.link_arguments(**link_kwargs) |
| 265 | + |
| 266 | + parser._inner_parser = True |
| 267 | + |
| 268 | + _cached_class_parsers[cache_key] = parser |
| 269 | + return parser |
| 270 | + |
| 271 | + |
216 | 272 | class ActionTypeHint(Action): |
217 | 273 | """Action to parse a type hint.""" |
218 | 274 |
|
@@ -633,33 +689,13 @@ def instantiate_classes(self, value): |
633 | 689 |
|
634 | 690 | @staticmethod |
635 | 691 | def get_class_parser(val_class, sub_add_kwargs=None, skip_args=None): |
636 | | - if isinstance(val_class, str): |
637 | | - val_class = import_object(val_class) |
638 | | - kwargs = dict(sub_add_kwargs) if sub_add_kwargs else {} |
639 | | - if skip_args: |
640 | | - kwargs.setdefault("skip", set()).update(skip_args) |
641 | | - parser = parent_parser.get() |
642 | | - from ._core import ArgumentParser |
643 | | - |
644 | | - assert isinstance(parser, ArgumentParser) |
645 | | - parser = type(parser)(exit_on_error=False, logger=parser.logger, parser_mode=parser.parser_mode) |
646 | | - remove_actions(parser, (ActionConfigFile, _ActionPrintConfig)) |
647 | | - if inspect.isclass(val_class) or inspect.isclass(get_typehint_origin(val_class)): |
648 | | - parser.add_class_arguments(val_class, **kwargs) |
649 | | - else: |
650 | | - kwargs = {k: v for k, v in kwargs.items() if k != "instantiate"} |
651 | | - parser.add_function_arguments(val_class, **kwargs) |
652 | | - |
653 | | - if "linked_targets" in kwargs: |
654 | | - for key in kwargs["linked_targets"]: |
655 | | - clear_required(parser, key) |
656 | | - |
657 | | - for link_kwargs in nested_links.get(): |
658 | | - parser.link_arguments(**link_kwargs) |
659 | | - |
660 | | - parser._inner_parser = True |
661 | | - |
662 | | - return parser |
| 692 | + return cached_get_class_parser( |
| 693 | + val_class=val_class, |
| 694 | + sub_add_kwargs=sub_add_kwargs, |
| 695 | + skip_args=skip_args, |
| 696 | + parent_parser=parent_parser.get(), |
| 697 | + nested_links=nested_links.get(), |
| 698 | + ) |
663 | 699 |
|
664 | 700 | def extra_help(self): |
665 | 701 | extra = "" |
|
0 commit comments