Skip to content

Commit 5cc1d34

Browse files
committed
[init] Add warnings message format
Providing deprecation and other specific messages via the 'warnings' module enables users to filter these messages. The added function on init level replaces the default 'warnings' print message with a less intrusive one for the whole package.
1 parent d38515f commit 5cc1d34

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

odml/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,26 @@
1212
from .info import VERSION
1313
from .tools.parser_utils import SUPPORTED_PARSERS as PARSERS
1414

15+
16+
def _format_warning(warn_msg, *args, **kwargs):
17+
"""
18+
Used to provide users with deprecation warnings via the warnings module
19+
but without spamming them with full stack traces.
20+
"""
21+
final_msg = "%s\n" % str(warn_msg)
22+
# If available add category name to the message
23+
if args and hasattr(args[0], "__name__"):
24+
final_msg = "%s: %s" % (args[0].__name__, final_msg)
25+
26+
return final_msg
27+
28+
29+
# Monkey patch formatting 'warnings' messages for the whole module.
30+
warnings.formatwarning = _format_warning
31+
1532
if _python_version.major < 3 or _python_version.major == 3 and _python_version.minor < 6:
1633
msg = "The '%s' package is not tested with your Python version. " % __name__
17-
msg += "Please consider upgrading to the latest Python distribution."
34+
msg += "\n\tPlease consider upgrading to the latest Python distribution."
1835
warnings.warn(msg)
1936

2037
__version__ = VERSION

0 commit comments

Comments
 (0)