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

action="once" not working #64

Closed
eliasmistler opened this issue May 19, 2023 · 1 comment
Closed

action="once" not working #64

eliasmistler opened this issue May 19, 2023 · 1 comment

Comments

@eliasmistler
Copy link

Expected Behavior

When setting the action="once" option, the deprecation warning should only be logged once.

from deprecated import deprecated


@deprecated(action='once')
def my_func():
    print("running the old func")


if __name__ == "__main__":
    my_func()
    my_func()
    my_func()

Actual Behavior

The deprecation warning is displayed every time the function is called:

DeprecationWarning: Call to deprecated function (or staticmethod) my_func.
  my_func()
DeprecationWarning: Call to deprecated function (or staticmethod) my_func.
  my_func()
DeprecationWarning: Call to deprecated function (or staticmethod) my_func.
  my_func()
running the old func
running the old func
running the old func

Environment

  • Python version: 3.10.6
  • Deprecated version: 1.2.13
@tantale
Copy link
Collaborator

tantale commented May 27, 2023

I agree, it's a problem, but not unique to the Deprecated library.

The "action" parameter is used with the warnings.filterwarnings() function to define the behavior of warnings in a specific part of the Python code.

The value "once" indicates that the warning should only be reported the first time it is encountered. After that, the warning will no longer be signaled even if it occurs again later.

When specifying the action at the deprecated function level, we use a context manager that allows the warning behavior to be modified locally to the function and thus restored at the end of the function call. This is required so as not to modify the application's global context. Indeed, in the case of "once", this has the effect of restoring the internal call counter, so a warning message is issued each time.

One way around this problem is not to specify the parameter at all, as "once" is the default value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants