Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding deprecated to command and parameter decorators #526

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Brewtils Changelog
==================

3.29.2
------
TBD

- Added deprecated decorator and deprecated kwarg to command and parameter decorators

3.29.1
------
12/31/24
Expand Down
45 changes: 45 additions & 0 deletions brewtils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from brewtils.errors import PluginParamError, _deprecate
from brewtils.models import Command, Parameter, Resolvable

import logging

logger = logging.getLogger(__name__)

if sys.version_info.major == 2:
from funcsigs import Parameter as InspectParameter # noqa
from funcsigs import signature
Expand Down Expand Up @@ -125,6 +129,7 @@ def command(
tag=None, # type: str
tags=None, # type: Optional[List[str]]
allow_any_kwargs=None, # type: Optional[bool]
deprecated=None, # type: Optional[bool]
):
"""Decorator for specifying Command details

Expand Down Expand Up @@ -158,6 +163,7 @@ def echo_json(self, message):
tags: A list of tags that can be used to filter commands
allow_any_kwargs: Flag controlling whether passed kwargs will be restricted to
the Command parameters defined.
deprecated: Boolean to indicate if command is deprecated

Returns:
The decorated function
Expand Down Expand Up @@ -207,6 +213,7 @@ def echo_json(self, message):
metadata=metadata,
tags=tags,
allow_any_kwargs=allow_any_kwargs,
deprecated=deprecated,
)

if output_type is None:
Expand All @@ -231,6 +238,7 @@ def echo_json(self, message):
metadata=metadata,
tags=tags,
allow_any_kwargs=allow_any_kwargs,
deprecated=deprecated,
)

# Python 2 compatibility
Expand Down Expand Up @@ -261,6 +269,7 @@ def parameter(
type_info=None, # type: Optional[dict]
is_kwarg=None, # type: Optional[bool]
model=None, # type: Optional[Type]
deprecated=None, # type: Optional[bool]
):
"""Decorator for specifying Parameter details

Expand Down Expand Up @@ -304,6 +313,7 @@ def echo(self, message):
method.
model: Class to be used as a model for this parameter. Must be a Python type
object, not an instance.
deprecated: Boolean to indicate if this parameter is deprecated

Returns:
The decorated function
Expand Down Expand Up @@ -346,6 +356,7 @@ def echo(self, message):
type_info=type_info,
is_kwarg=is_kwarg,
model=model,
deprecated=deprecated,
)

new_parameter = Parameter(
Expand All @@ -366,6 +377,7 @@ def echo(self, message):
type_info=type_info,
is_kwarg=is_kwarg,
model=model,
deprecated=deprecated,
)

# Python 2 compatibility
Expand Down Expand Up @@ -473,6 +485,24 @@ def pre_shutdown(self):
return _wrapped


def deprecated(_wrapped=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will conflict with the python provided solution. Instead we need to inspect for this being set.
https://docs.python.org/3/library/warnings.html#warnings.deprecated

Copy link
Contributor Author

@1maple1 1maple1 Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed deprecated in favor of deprecated decorator in standard library.

Prior to 3.13: from typing_extensions import deprecated
Python 3.13 and later: from warnings import deprecated
@deprecated("Do not use")

"""Decorator for specifying a deprecated command or parameter

for example::

@deprecated
def pre_running(self):
# Run pre-running processing
return

Args:
_wrapped: The function to decorate. This is handled as a positional argument and
shouldn't be explicitly set.
"""
_wrapped._deprecated = True
return _wrapped


def startup(_wrapped=None):
"""Decorator for specifying a function to run before a plugin is running.

Expand Down Expand Up @@ -551,6 +581,18 @@ def _parse_shutdown_functions(client):
return shutdown_functions


def _parse_deprecated(method):
"""Get a list of deprecated methods"""
cmd = getattr(method, "_command", Command())
if cmd.deprecated:
return True

if callable(method) and getattr(method, "_deprecated", False):
return True

return False


def _parse_startup_functions(client):
# type: (object) -> List[Callable]
"""Get a list of callable fields labeled with the startup annotation
Expand Down Expand Up @@ -679,6 +721,7 @@ def _initialize_command(method):
cmd.name = _method_name(method)
cmd.display_name = cmd.display_name or _method_name(method)
cmd.description = cmd.description or _method_docstring(method)
cmd.deprecated = cmd.deprecated or _parse_deprecated(method)

try:
base_dir = os.path.dirname(inspect.getfile(method))
Expand Down Expand Up @@ -902,6 +945,7 @@ def _initialize_parameter(
is_kwarg=None,
model=None,
method=None,
deprecated=None,
):
# type: (...) -> Parameter
"""Initialize a Parameter
Expand Down Expand Up @@ -942,6 +986,7 @@ def _initialize_parameter(
type_info=type_info,
is_kwarg=is_kwarg,
model=model,
deprecated=deprecated,
)

# Every parameter needs a key, so stop that right here
Expand Down
4 changes: 4 additions & 0 deletions brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def __init__(
tags=None,
topics=None,
allow_any_kwargs=None,
deprecated=None,
):
self.name = name
self.display_name = display_name or name
Expand All @@ -159,6 +160,7 @@ def __init__(
self.tags = tags or []
self.topics = topics or []
self.allow_any_kwargs = allow_any_kwargs
self.deprecated = deprecated

def __str__(self):
return self.name
Expand Down Expand Up @@ -336,6 +338,7 @@ def __init__(
regex=None,
form_input_type=None,
type_info=None,
deprecated=None,
is_kwarg=None,
model=None,
):
Expand All @@ -354,6 +357,7 @@ def __init__(
self.regex = regex
self.form_input_type = form_input_type
self.type_info = type_info or {}
self.deprecated = deprecated

# These are special - they aren't part of the Parameter "API" (they aren't in
# the serialization schema) but we still need them on this model for consistency
Expand Down
2 changes: 2 additions & 0 deletions brewtils/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class ParameterSchema(BaseSchema):
regex = fields.Str(allow_none=True)
form_input_type = fields.Str(allow_none=True)
type_info = fields.Dict(allow_none=True)
deprecated = fields.Bool(allow_none=True)


class CommandSchema(BaseSchema):
Expand All @@ -204,6 +205,7 @@ class CommandSchema(BaseSchema):
tags = fields.List(fields.Str(), allow_none=True)
topics = fields.List(fields.Str(), allow_none=True)
allow_any_kwargs = fields.Boolean(allow_none=True)
deprecated = fields.Boolean(allow_none=True)


class InstanceSchema(BaseSchema):
Expand Down
3 changes: 3 additions & 0 deletions brewtils/test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def nested_parameter_dict():
"regex": None,
"form_input_type": None,
"type_info": {},
"deprecated": False,
}


Expand All @@ -153,6 +154,7 @@ def parameter_dict(nested_parameter_dict, choices_dict):
"regex": ".*",
"form_input_type": None,
"type_info": {},
"deprecated": False,
}


Expand Down Expand Up @@ -184,6 +186,7 @@ def command_dict(parameter_dict, system_id):
"tags": [],
"topics": [],
"allow_any_kwargs": False,
"deprecated": False,
}


Expand Down
Loading