-
Notifications
You must be signed in to change notification settings - Fork 315
Enable showing DeprecationWarning in debug_on and add unit test #1554
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
Changes from all commits
2ef8b92
94fcfcb
3eb9e19
220738a
7a761e0
85c985b
09d9e05
fbeed6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import logging | ||
import os | ||
import warnings | ||
import contextlib | ||
from typing import Mapping | ||
|
||
import numpy as np | ||
|
@@ -43,16 +44,83 @@ def ensure_dir(filename): | |
os.makedirs(directory) | ||
|
||
|
||
def debug_on(): | ||
"""Turn debugging logging on.""" | ||
def debug_on(deprecation_warnings=True): | ||
"""Turn debugging logging on. | ||
|
||
Sets up a StreamHandler to to `sys.stderr` at debug level for all | ||
loggers, such that all debug messages (and log messages with higher | ||
severity) are logged to the standard error stream. | ||
|
||
By default, since Satpy 0.26, this also enables the global visibility | ||
of deprecation warnings. This can be suppressed by passing a false | ||
value. | ||
|
||
Args: | ||
deprecation_warnings (Optional[bool]): Switch on deprecation warnings. | ||
Defaults to True. | ||
|
||
Returns: | ||
None | ||
""" | ||
logging_on(logging.DEBUG) | ||
if deprecation_warnings: | ||
deprecation_warnings_on() | ||
|
||
|
||
def debug_off(): | ||
mraspaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Turn debugging logging off. | ||
|
||
This disables both debugging logging and the global visibility of | ||
deprecation warnings. | ||
""" | ||
logging_off() | ||
deprecation_warnings_off() | ||
|
||
|
||
@contextlib.contextmanager | ||
def debug(deprecation_warnings=True): | ||
"""Context manager to temporarily set debugging on. | ||
|
||
Example:: | ||
|
||
>>> with satpy.utils.debug(): | ||
... code_here() | ||
|
||
Args: | ||
deprecation_warnings (Optional[bool]): Switch on deprecation warnings. | ||
Defaults to True. | ||
""" | ||
debug_on(deprecation_warnings=deprecation_warnings) | ||
yield | ||
debug_off() | ||
|
||
|
||
def trace_on(): | ||
"""Turn trace logging on.""" | ||
logging_on(TRACE_LEVEL) | ||
|
||
|
||
class _WarningManager: | ||
"""Class to handle switching warnings on and off.""" | ||
|
||
filt = None | ||
|
||
|
||
_warning_manager = _WarningManager() | ||
mraspaud marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+103
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need a class here? would it work with just a module variable, eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would have to be a global variable. I personally hate global variables. I find them confusing, error-prone, and hard to test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a way, isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a way yes, and in a way no. There is no global keyword, and I'm not changing somebody else's namespace; rather, I'm just changing the state of a mutable object that happens to be in the module namespace. The difference is subtle and in this case largely semantic, but if somebody did have the idea to use their own reference to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I get your point. Now, looking at the bigger picture, why use a class at all and not just a constant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe it's thread safe, let's just hope people don't rely on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. about thread-safety:
(last lines in https://docs.python.org/3/library/warnings.html) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No further comments from me 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not thrilled with the Python 'warnings' module, so there is no solution that I would be thrilled with. |
||
|
||
|
||
def deprecation_warnings_on(): | ||
"""Switch on deprecation warnings.""" | ||
warnings.filterwarnings("default", category=DeprecationWarning) | ||
_warning_manager.filt = warnings.filters[0] | ||
|
||
|
||
def deprecation_warnings_off(): | ||
"""Switch off deprecation warnings.""" | ||
if _warning_manager.filt in warnings.filters: | ||
warnings.filters.remove(_warning_manager.filt) | ||
|
||
|
||
def logging_on(level=logging.WARNING): | ||
"""Turn logging on.""" | ||
global _is_logging_on | ||
|
Uh oh!
There was an error while loading. Please reload this page.