Skip to content

Commit

Permalink
refactor: more detailed deprecated argument warning message (#2808)
Browse files Browse the repository at this point in the history
* more detailed deprecated warning message

---------

Co-authored-by: Kathy Pippert <[email protected]>
  • Loading branch information
raph-luc and PipKat authored May 22, 2024
1 parent c3c4157 commit 93d66ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
34 changes: 29 additions & 5 deletions src/ansys/fluent/core/utils/deprecate_args.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
"""Module that provides a method to handle deprecated arguments."""

import functools
import logging
import warnings

from ansys.fluent.core.launcher.pyfluent_enums import FluentEnum

logger = logging.getLogger("pyfluent.general")


def deprecate_argument(
old_arg, new_arg, converter, deprecation_class=DeprecationWarning
):
"""Warns user that the argument provided is deprecated, and automatically replaces
the deprecated argument with the appropriate new argument."""
"""Warns that the argument provided is deprecated and automatically replaces the
deprecated argument with the appropriate new argument."""

def _str_repr(var):
"""Converts a string or FluentEnum variable to quoted string representation."""
if isinstance(var, (str, FluentEnum)):
return f'"{var}"'
else:
return var

def decorator(func):
"""Holds the original method to perform operations on it."""
Expand All @@ -21,10 +33,22 @@ def wrapper(*args, **kwargs):
warnings.warn(
f"'{old_arg}' is deprecated. Use '{new_arg}' instead.",
deprecation_class,
stacklevel=2,
)
val = converter(kwargs[old_arg])
if val is not None and kwargs.get(new_arg) is None:
kwargs[new_arg] = val
old_value = kwargs[old_arg]
new_value = kwargs.get(new_arg)
if new_value is None:
new_value = converter(kwargs[old_arg])
kwargs[new_arg] = new_value
logger.warning(
f"Using '{new_arg} = {_str_repr(new_value)}' for '{func.__name__}()'"
f" instead of '{old_arg} = {_str_repr(old_value)}'."
)
else:
logger.warning(
f"Ignoring '{old_arg} = {_str_repr(old_value)}' specification for '{func.__name__}()',"
f" only '{new_arg} = {_str_repr(new_value)}' applies."
)
kwargs.pop(old_arg)
return func(*args, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def test_gpu_version_error():
version="2d",
precision="single",
processor_count=5,
show_gui=True,
ui_mode="gui",
gpu=True,
)
pyfluent.setup_for_fluent(
mode="meshing",
version="2d",
precision="single",
processor_count=5,
show_gui=True,
ui_mode="gui",
gpu=True,
)

Expand Down

0 comments on commit 93d66ad

Please sign in to comment.