From 93d66ad3f68cd4e2d4496e3c91e3678ad36c28fd Mon Sep 17 00:00:00 2001 From: Raphael Luciano Date: Wed, 22 May 2024 12:50:40 -0400 Subject: [PATCH] refactor: more detailed deprecated argument warning message (#2808) * more detailed deprecated warning message --------- Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> --- src/ansys/fluent/core/utils/deprecate_args.py | 34 ++++++++++++++++--- tests/test_launcher.py | 4 +-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/ansys/fluent/core/utils/deprecate_args.py b/src/ansys/fluent/core/utils/deprecate_args.py index 6b4ef0a46bc..ab6c019563e 100644 --- a/src/ansys/fluent/core/utils/deprecate_args.py +++ b/src/ansys/fluent/core/utils/deprecate_args.py @@ -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.""" @@ -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) diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 6496429fddb..2cd8c07052e 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -42,7 +42,7 @@ def test_gpu_version_error(): version="2d", precision="single", processor_count=5, - show_gui=True, + ui_mode="gui", gpu=True, ) pyfluent.setup_for_fluent( @@ -50,7 +50,7 @@ def test_gpu_version_error(): version="2d", precision="single", processor_count=5, - show_gui=True, + ui_mode="gui", gpu=True, )