Skip to content

Commit

Permalink
FIX: Do not evaluate callable defaults during tab-completion
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed Oct 13, 2023
1 parent b6f7ec1 commit e265067
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions aiida/cmdline/params/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# yapf: disable
# pylint: disable=wildcard-import

from .callable import *
from .config import *
from .main import *
from .multivalue import *
Expand All @@ -39,6 +40,7 @@
'COMPUTER',
'COMPUTERS',
'CONFIG_FILE',
'CallableDefaultOption',
'ConfigFileOption',
'DATA',
'DATUM',
Expand Down
34 changes: 34 additions & 0 deletions aiida/cmdline/params/options/callable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""
.. py:module::callable
:synopsis: A monkey-patched subclass of click.Option that does not evaluate callable default during tab completion
"""

import typing as t

import click

__all__ = ('CallableDefaultOption',)


class CallableDefaultOption(click.Option):
"""A monkeypatch for click.Option that does not evaluate default callbacks during tab completion
This is a temporary solution until a proper fix is implemented in click, see:
https://github.com/pallets/click/issues/2614
"""

def get_default(self, ctx: click.Context, call: bool = True) -> t.Optional[t.Union[t.Any, t.Callable[[], t.Any]]]:
"""provides the functionality of :meth:`click.Option.get_default`,
but ensures we do not evaluate callable defaults when in tab-completion context."""
if ctx.resilient_parsing:
return None
return super().get_default(ctx=ctx, call=call)
2 changes: 1 addition & 1 deletion aiida/cmdline/params/options/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def get_default(self, ctx: click.Context, call: bool = True) -> t.Optional[t.Uni
if self._contextual_default is not None:
default = self._contextual_default(ctx)
else:
default = super().get_default(ctx)
default = super().get_default(ctx, call=call)

try:
default = self.type.deconvert_default(default)
Expand Down
2 changes: 2 additions & 0 deletions aiida/cmdline/params/options/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .. import types
from ...utils import defaults, echo # pylint: disable=no-name-in-module
from .callable import CallableDefaultOption
from .config import ConfigFileOption
from .multivalue import MultipleValueOption
from .overridable import OverridableOption
Expand Down Expand Up @@ -145,6 +146,7 @@ def set_log_level(_ctx, _param, value):
'profile',
type=types.ProfileParamType(),
default=defaults.get_default_profile,
cls=CallableDefaultOption,
help='Execute the command for this profile instead of the default profile.'
)

Expand Down

0 comments on commit e265067

Please sign in to comment.