Skip to main content

Backport of PEP 562.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pep562-1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

pep562-1.1-py2.py3-none-any.whl (7.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pep562-1.1.tar.gz.

File metadata

  • Download URL: pep562-1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pep562-1.1.tar.gz
Algorithm Hash digest
SHA256 f8bc94a273dd45bd81a06a57c04bc57fd46f9c054c1354fa056044ceba2f0605
MD5 ec28b311b30145c275defb33a8971b3d
BLAKE2b-256 dbbb5aa7b642696e3fffb6e532c94a3c00aca50e1c51f19a525882ba969a0a2b

See more details on using hashes here.

File details

Details for the file pep562-1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pep562-1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for pep562-1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ff2308f21f1fcdf0a539b1af4d5a070c5baf47a07f51ff95f5d311f0c60390a4
MD5 50625357e81363d4b36aedbf3bb25f3e
BLAKE2b-256 8fb98a74b93c643b54cc7579087ff6a56aa1a9c52329be6846abfbaee4a7828b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page