Skip to content

Commit

Permalink
Cache aliases function / stub aliases property
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Apr 24, 2023
1 parent af8b185 commit a1f86ec
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions jsonargparse/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@
class Action(LoggerProperty, ArgparseAction):
"""Base for jsonargparse Action classes."""

@property
def aliases(self):
return _action_aliases(self)


def _action_aliases(self):
if not hasattr(self, '_aliases'):
options = {optstr.lstrip('-').replace('-', '_')
for optstr in self.option_strings}
options = {opt for opt in options if len(opt) > 1}
self._aliases = {self.dest} | options
return self._aliases


def _is_branch_key(parser, key: str) -> bool:
root_key = split_key_root(key)[0]
Expand Down Expand Up @@ -70,10 +83,10 @@ def _find_action_and_subcommand(
actions = [a for a in actions if not isinstance(a, exclude)]
fallback_action = None

ALLOW_ALIAS = True

for action in actions:
if action.dest == dest or (ALLOW_ALIAS and dest in get_alias_dest(action)):
# _StoreAction seems to break the property
# if dest in action.aliases:
if dest in _action_aliases(action):
if isinstance(action, _ActionConfigLoad):
fallback_action = action
else:
Expand Down Expand Up @@ -752,8 +765,5 @@ def handle_subcommands(


def get_alias_dest(action):
def option_string_to_var(optstr):
" normalize a cli key as a variable "
return optstr.lstrip('-').replace('-', '_')

return [option_string_to_var(optstr) for optstr in action.option_strings]
return [optstr.lstrip('-').replace('-', '_')
for optstr in action.option_strings]

0 comments on commit a1f86ec

Please sign in to comment.