Skip to main content

Donate via PayPal Discord Build Coverage Status PyPI Version PyPI - Python Version License

PEP 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__)

License

MIT License

Copyright (c) 2018 - 2021 Isaac Muse isaacmuse@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for pep562 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pep562 1.1
File Size Uploaded
pep562-1.1.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pep562 1.1
File Interpreter ABI Platform
pep562-1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 17.3 kB

Release files / pep562-1.1.tar.gz

Download URL pep562-1.1.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f8bc94a273dd45bd81a06a57c04bc57fd46f9c054c1354fa056044ceba2f0605
BLAKE2b-256 checksum
How to use checksums
dbbb5aa7b642696e3fffb6e532c94a3c00aca50e1c51f19a525882ba969a0a2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

Release files / pep562-1.1-py2.py3-none-any.whl

Download URL pep562-1.1-py2.py3-none-any.whl
Size 7.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ff2308f21f1fcdf0a539b1af4d5a070c5baf47a07f51ff95f5d311f0c60390a4
BLAKE2b-256 checksum
How to use checksums
8fb98a74b93c643b54cc7579087ff6a56aa1a9c52329be6846abfbaee4a7828b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page