forked from python-trio/trio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request python-trio#296 from njsmith/deprecations
Add deprecation framework, and do some deprecations
- Loading branch information
Showing
17 changed files
with
347 additions
and
102 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
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,73 @@ | ||
from functools import wraps, partial | ||
import warnings | ||
|
||
__all__ = ["TrioDeprecationWarning"] | ||
|
||
|
||
# We want our warnings to be visible by default (at least for now), but we | ||
# also want it to be possible to override that using the -W switch. AFAICT | ||
# this means we cannot inherit from DeprecationWarning, because the only way | ||
# to make it visible by default then would be to add our own filter at import | ||
# time, but that would override -W switches... | ||
class TrioDeprecationWarning(FutureWarning): | ||
"""Warning emitted if you use deprecated Trio functionality. | ||
As a young project, Trio is currently quite aggressive about deprecating | ||
and/or removing functionality that we realize was a bad idea. If you use | ||
Trio, you should subscribe to `issue #1 | ||
<https://github.com/python-trio/trio/issues/1>`__ to get information about | ||
upcoming deprecations and other backwards compatibility breaking changes. | ||
Despite the name, this class currently inherits from | ||
:class:`FutureWarning`, not :class:`DeprecationWarning`, because while | ||
we're in young-and-aggressive mode we want these warnings to be visible by | ||
default. You can hide them by installing a filter or with the ``-W`` | ||
switch: see the :mod:`warnings` documentation for details. | ||
""" | ||
|
||
|
||
def _stringify(thing): | ||
if hasattr(thing, "__module__") and hasattr(thing, "__qualname__"): | ||
return "{}.{}".format(thing.__module__, thing.__qualname__) | ||
return str(thing) | ||
|
||
|
||
def warn_deprecated(thing, *, version, alternative, stacklevel=2): | ||
stacklevel += 1 | ||
msg = "{} is deprecated since Trio {}".format(_stringify(thing), version) | ||
if alternative is not None: | ||
msg = "{}; use {} instead".format(msg, _stringify(alternative)) | ||
warnings.warn(TrioDeprecationWarning(msg), stacklevel=stacklevel) | ||
|
||
|
||
# deprecated(version=..., alternative=...) | ||
def deprecated(*, thing=None, version, alternative): | ||
def do_wrap(fn): | ||
nonlocal thing | ||
|
||
@wraps(fn) | ||
def wrapper(*args, **kwargs): | ||
warn_deprecated(thing, version=version, alternative=alternative) | ||
return fn(*args, **kwargs) | ||
|
||
# If our __module__ or __qualname__ get modified, we want to pick up | ||
# on that, so we read them off the wrapper object instead of the (now | ||
# hidden) fn object | ||
if thing is None: | ||
thing = wrapper | ||
|
||
return wrapper | ||
|
||
return do_wrap | ||
|
||
|
||
def deprecated_alias(old_qualname, new_fn, *, version): | ||
@deprecated(version=version, alternative=new_fn) | ||
@wraps(new_fn) | ||
def wrapper(*args, **kwargs): | ||
return new_fn(*args, **kwargs) | ||
|
||
wrapper.__qualname__ = old_qualname | ||
wrapper.__name__ = old_qualname.rpartition(".")[-1] | ||
return wrapper |
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
Oops, something went wrong.