Backport of PEP 562.
Likely abandoned. No release or commit for years (last release over 5 years ago). It brings a standard-library module to older Pythons: on current versions, use the built-in one.
pip install pep562PEP 562
Overview
A backport of PEP 562. Allows controlling a module's __dir__ and __getattr__. Useful for deprecating attributes.
Works for Python 2.7+. And while it works on Python 3.7, it is recommended to use the official Python 3.7 implementation
where applicable.
This module can be installed and used as a dependency, or if desired, it is easy to vendor as the license is quite permissible, and the code is contained in a single file.
Once Python 3.6 is end of life, this module will be irrelevant and will no longer receive active support.
Install
Installation is done with pip:
pip install pep562
Vendoring
Simply copy the file pep562/__init__.py to your project and rename to pep562. Import and use as needed.
Usage
Here is a simple example where we deprecate the attribute version for the more standardized __version__.
from pep562 import pep562
import warnings
__version__ = (1, 0, 0)
__all__ = ("__version__",)
__deprecated__ = {
"version": ("__version__", __version__)
}
PY37 = sys.version_info >= (3, 7)
def __getattr__(name):
"""Get attribute."""
deprecated = __deprecated__.get(name)
if deprecated:
stacklevel = 3 if PY37 else 4
warnings.warn(
"'{}' is deprecated. Use '{}' instead.".format(name, deprecated[0]),
category=DeprecationWarning,
stacklevel=stacklevel
)
return deprecated[1]
raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name))
def __dir__():
"""Module directory."""
return sorted(list(__all__) + list(__deprecated__.keys()))
pep562(__name__)