-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better warnings (#838)Co-authored-by: pre-commit-ci[bot] <66853113+pr…
…e-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <steven.silvester@ieee.org> * Replace FutureWarnings with DeprecationWarnings per https://docs.python.org/3/library/warnings.html#warning-categories, FutureWarnings are intended for end users, and DeprecationWarnings are for programmers. We are targetting programmers here. This is a step towards fixing #837 * ref: remove unused _names_re this is private and unused. * feat: Always include stacklevel when warn()ing Fixes #837 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix import * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * lint --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <steven.silvester@ieee.org>
- Loading branch information
1 parent
21fdc40
commit 5cbf807
Showing
6 changed files
with
88 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import inspect | ||
import os | ||
import warnings | ||
|
||
|
||
def warn(msg, category, *, stacklevel, source=None): | ||
"""Like warnings.warn(), but category and stacklevel are required. | ||
You pretty much never want the default stacklevel of 1, so this helps | ||
encourage setting it explicitly.""" | ||
return warnings.warn(msg, category=category, stacklevel=stacklevel, source=source) | ||
|
||
|
||
def deprecated_method(method, cls, method_name, msg): | ||
"""Show deprecation warning about a magic method definition. | ||
Uses warn_explicit to bind warning to method definition instead of triggering code, | ||
which isn't relevant. | ||
""" | ||
warn_msg = "{classname}.{method_name} is deprecated in traitlets 4.1: {msg}".format( | ||
classname=cls.__name__, method_name=method_name, msg=msg | ||
) | ||
|
||
for parent in inspect.getmro(cls): | ||
if method_name in parent.__dict__: | ||
cls = parent | ||
break | ||
# limit deprecation messages to once per package | ||
package_name = cls.__module__.split(".", 1)[0] | ||
key = (package_name, msg) | ||
if not should_warn(key): | ||
return | ||
try: | ||
fname = inspect.getsourcefile(method) or "<unknown>" | ||
lineno = inspect.getsourcelines(method)[1] or 0 | ||
except (OSError, TypeError) as e: | ||
# Failed to inspect for some reason | ||
warn( | ||
warn_msg + ("\n(inspection failed) %s" % e), | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
else: | ||
warnings.warn_explicit(warn_msg, DeprecationWarning, fname, lineno) | ||
|
||
|
||
_deprecations_shown = set() | ||
|
||
|
||
def should_warn(key): | ||
"""Add our own checks for too many deprecation warnings. | ||
Limit to once per package. | ||
""" | ||
env_flag = os.environ.get("TRAITLETS_ALL_DEPRECATIONS") | ||
if env_flag and env_flag != "0": | ||
return True | ||
|
||
if key not in _deprecations_shown: | ||
_deprecations_shown.add(key) | ||
return True | ||
else: | ||
return False |