Skip to main content

Backport of PEP 562.

Project description

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 on versions greater than Python 3.7 it will do nothing allowing it to be used in projects that support Python 3.7 and below.

Usage

Here is a simple example where we deprecate the attribute version for the more standardized __version__.

from pep562 import Pep562
import sys
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 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.0b1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

pep562-1.0b1-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pep562-1.0b1.tar.gz.

File metadata

  • Download URL: pep562-1.0b1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for pep562-1.0b1.tar.gz
Algorithm Hash digest
SHA256 9ab7124d6a97b78bc23276feaf96521b87a1873901cee7896272ea76ed6ec834
MD5 76fe5ae5bd194d0e15a24bd49fa25b4d
BLAKE2b-256 964dc6c9602891a213d10230a4ae7b4794e011ad143270772e63518c578b8a8b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pep562-1.0b1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for pep562-1.0b1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1cd6bdd88cefb6a2979245da15a40c23d2c0a86a5cde8fb64819ae7d40a57863
MD5 ad57bdad9b09d5ef546afacbc74e5c7a
BLAKE2b-256 129471a323a231b0930ebb1cff8076e6e12d29698f71ca1807e171907d4e5f66

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