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

"default", "module", and "once" actions do not work in Python 3 #25

Closed
heidecjj opened this issue Apr 21, 2020 · 1 comment
Closed

"default", "module", and "once" actions do not work in Python 3 #25

heidecjj opened this issue Apr 21, 2020 · 1 comment

Comments

@heidecjj
Copy link
Contributor

Expected Behavior

Paste the following into a file named once.py

from deprecated import deprecated
@deprecated(reason='this should happen once', action='once')
def a():
    return

a()
a()
a()

I expect the deprecation warning to print once:

$ python3 once.py
once.py:6: DeprecationWarning: Call to deprecated function (or staticmethod) a. (this should happen once)
  a()

Actual Behavior

Tell us what happens instead.

$ python3 once.py
once.py:6: DeprecationWarning: Call to deprecated function (or staticmethod) a. (this should happen once)
  a()
once.py:7: DeprecationWarning: Call to deprecated function (or staticmethod) a. (this should happen once)
  a()
once.py:8: DeprecationWarning: Call to deprecated function (or staticmethod) a. (this should happen once)
  a()

Environment

  • Python version: Python 3.6.9
  • Deprecated version: 1.2.9

Additional Info

Note: this issue is slightly different from the issue #24 aims to solve. #24 deals with actions specified by global warning filters while this issue deals with actions specified to the @deprecated decorator.

This is not a problem in Python 2. This is only a problem in Python 3 due to the way warnings.catch_warnings() works. Entering and exiting the warnings.catch_warnings() context manager causes Python 3 to clear out its warning registries. The warning registries keep track of which warnings have fired in order to enforce actions like "once", "default", and "module". When deprecated fires its warning inside warnings.catch_warnings(), the warning registries will always be empty, so "once", "default", and "module" end up behaving like "always". I'm not sure there's much we can do to fix this problem.

@heidecjj
Copy link
Contributor Author

heidecjj commented Jan 7, 2022

Here's the underlying python bug that causes #25: https://bugs.python.org/issue29672

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