Skip to content

Commit

Permalink
CLI: update cmdline command deprecate decorator to include version
Browse files Browse the repository at this point in the history
A version for the removal of the command can be specified that is
printed in the deprecation warning.
  • Loading branch information
agoscinski committed Jun 6, 2024
1 parent e7953fd commit 4e1bf76
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/aiida/cmdline/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from contextlib import contextmanager

from click_spinner import spinner
from packaging.version import Version
from wrapt import decorator

from . import echo
Expand Down Expand Up @@ -239,13 +240,17 @@ def do_circus_stuff():
return wrapped(*args, **kwargs)


def deprecated_command(message):
def deprecated_command(message: str, version: str = '3.0.0'):
"""Function decorator that will mark a click command as deprecated when invoked.
:param message: The message that should be printed with the deprecation warning.
:param version: Specifies the version for which the command will be removed. Any string is valid that is accepted by
:class:`packaging.version.Version`.
Example::
@click.command()
@deprecated_command('This command has been deprecated in AiiDA v1.0, please use 'foo' instead.)
@deprecated_command('This command has been deprecated, please use 'foo' instead.' version='1.0')
def mycommand():
pass
"""
Expand All @@ -259,7 +264,15 @@ def wrapper(wrapped, _, args, kwargs):

template = templates.env.get_template('deprecated.tpl')
width = 80
echo.echo(template.render(msg=wrap(message, width - 4), width=width))
from packaging.version import InvalidVersion

try:
version_object = Version(version)
except (TypeError, InvalidVersion) as exception:
raise ValueError(f'`{version}` is not a valid value for `version`') from exception

Check warning on line 272 in src/aiida/cmdline/utils/decorators.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/cmdline/utils/decorators.py#L271-L272

Added lines #L271 - L272 were not covered by tests

extended_message = f'{message} (this will be removed in v{version_object.major}.{version_object.minor})'
echo.echo(template.render(msg=wrap(extended_message, width - 4), width=width))

return wrapped(*args, **kwargs)

Expand Down

0 comments on commit 4e1bf76

Please sign in to comment.