Mercurial > repos > shellac > guppy_basecaller
annotate env/lib/python3.7/site-packages/humanfriendly/deprecation.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac | 
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 0 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 1 # Human friendly input/output in Python. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 2 # | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 3 # Author: Peter Odding <peter@peterodding.com> | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 4 # Last Change: March 2, 2020 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 5 # URL: https://humanfriendly.readthedocs.io | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 6 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 7 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 8 Support for deprecation warnings when importing names from old locations. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 9 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 10 When software evolves, things tend to move around. This is usually detrimental | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 11 to backwards compatibility (in Python this primarily manifests itself as | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 12 :exc:`~exceptions.ImportError` exceptions). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 13 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 14 While backwards compatibility is very important, it should not get in the way | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 15 of progress. It would be great to have the agility to move things around | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 16 without breaking backwards compatibility. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 17 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 18 This is where the :mod:`humanfriendly.deprecation` module comes in: It enables | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 19 the definition of backwards compatible aliases that emit a deprecation warning | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 20 when they are accessed. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 21 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 22 The way it works is that it wraps the original module in an :class:`DeprecationProxy` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 23 object that defines a :func:`~DeprecationProxy.__getattr__()` special method to | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 24 override attribute access of the module. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 25 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 26 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 27 # Standard library modules. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 28 import collections | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 29 import functools | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 30 import importlib | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 31 import inspect | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 32 import sys | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 33 import types | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 34 import warnings | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 35 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 36 # Modules included in our package. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 37 from humanfriendly.text import format | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 38 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 39 # Registry of known aliases (used by humanfriendly.sphinx). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 40 REGISTRY = collections.defaultdict(dict) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 41 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 42 # Public identifiers that require documentation. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 43 __all__ = ("DeprecationProxy", "define_aliases", "deprecated_args", "get_aliases", "is_method") | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 44 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 45 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 46 def define_aliases(module_name, **aliases): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 47 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 48 Update a module with backwards compatible aliases. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 49 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 50 :param module_name: The ``__name__`` of the module (a string). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 51 :param aliases: Each keyword argument defines an alias. The values | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 52 are expected to be "dotted paths" (strings). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 53 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 54 The behavior of this function depends on whether the Sphinx documentation | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 55 generator is active, because the use of :class:`DeprecationProxy` to shadow the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 56 real module in :data:`sys.modules` has the unintended side effect of | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 57 breaking autodoc support for ``:data:`` members (module variables). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 58 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 59 To avoid breaking Sphinx the proxy object is omitted and instead the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 60 aliased names are injected into the original module namespace, to make sure | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 61 that imports can be satisfied when the documentation is being rendered. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 62 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 63 If you run into cyclic dependencies caused by :func:`define_aliases()` when | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 64 running Sphinx, you can try moving the call to :func:`define_aliases()` to | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 65 the bottom of the Python module you're working on. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 66 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 67 module = sys.modules[module_name] | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 68 proxy = DeprecationProxy(module, aliases) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 69 # Populate the registry of aliases. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 70 for name, target in aliases.items(): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 71 REGISTRY[module.__name__][name] = target | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 72 # Avoid confusing Sphinx. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 73 if "sphinx" in sys.modules: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 74 for name, target in aliases.items(): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 75 setattr(module, name, proxy.resolve(target)) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 76 else: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 77 # Install a proxy object to raise DeprecationWarning. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 78 sys.modules[module_name] = proxy | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 79 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 80 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 81 def get_aliases(module_name): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 82 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 83 Get the aliases defined by a module. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 84 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 85 :param module_name: The ``__name__`` of the module (a string). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 86 :returns: A dictionary with string keys and values: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 87 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 88 1. Each key gives the name of an alias | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 89 created for backwards compatibility. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 90 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 91 2. Each value gives the dotted path of | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 92 the proper location of the identifier. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 93 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 94 An empty dictionary is returned for modules that | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 95 don't define any backwards compatible aliases. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 96 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 97 return REGISTRY.get(module_name, {}) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 98 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 99 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 100 def deprecated_args(*names): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 101 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 102 Deprecate positional arguments without dropping backwards compatibility. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 103 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 104 :param names: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 105 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 106 The positional arguments to :func:`deprecated_args()` give the names of | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 107 the positional arguments that the to-be-decorated function should warn | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 108 about being deprecated and translate to keyword arguments. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 109 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 110 :returns: A decorator function specialized to `names`. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 111 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 112 The :func:`deprecated_args()` decorator function was created to make it | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 113 easy to switch from positional arguments to keyword arguments [#]_ while | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 114 preserving backwards compatibility [#]_ and informing call sites | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 115 about the change. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 116 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 117 .. [#] Increased flexibility is the main reason why I find myself switching | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 118 from positional arguments to (optional) keyword arguments as my code | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 119 evolves to support more use cases. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 120 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 121 .. [#] In my experience positional argument order implicitly becomes part | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 122 of API compatibility whether intended or not. While this makes sense | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 123 for functions that over time adopt more and more optional arguments, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 124 at a certain point it becomes an inconvenience to code maintenance. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 125 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 126 Here's an example of how to use the decorator:: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 127 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 128 @deprecated_args('text') | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 129 def report_choice(**options): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 130 print(options['text']) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 131 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 132 When the decorated function is called with positional arguments | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 133 a deprecation warning is given:: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 134 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 135 >>> report_choice('this will give a deprecation warning') | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 136 DeprecationWarning: report_choice has deprecated positional arguments, please switch to keyword arguments | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 137 this will give a deprecation warning | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 138 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 139 But when the function is called with keyword arguments no deprecation | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 140 warning is emitted:: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 141 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 142 >>> report_choice(text='this will not give a deprecation warning') | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 143 this will not give a deprecation warning | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 144 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 145 def decorator(function): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 146 def translate(args, kw): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 147 # Raise TypeError when too many positional arguments are passed to the decorated function. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 148 if len(args) > len(names): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 149 raise TypeError( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 150 format( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 151 "{name} expected at most {limit} arguments, got {count}", | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 152 name=function.__name__, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 153 limit=len(names), | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 154 count=len(args), | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 155 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 156 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 157 # Emit a deprecation warning when positional arguments are used. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 158 if args: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 159 warnings.warn( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 160 format( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 161 "{name} has deprecated positional arguments, please switch to keyword arguments", | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 162 name=function.__name__, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 163 ), | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 164 category=DeprecationWarning, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 165 stacklevel=3, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 166 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 167 # Translate positional arguments to keyword arguments. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 168 for name, value in zip(names, args): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 169 kw[name] = value | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 170 if is_method(function): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 171 @functools.wraps(function) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 172 def wrapper(*args, **kw): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 173 """Wrapper for instance methods.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 174 args = list(args) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 175 self = args.pop(0) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 176 translate(args, kw) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 177 return function(self, **kw) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 178 else: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 179 @functools.wraps(function) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 180 def wrapper(*args, **kw): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 181 """Wrapper for module level functions.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 182 translate(args, kw) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 183 return function(**kw) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 184 return wrapper | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 185 return decorator | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 186 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 187 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 188 def is_method(function): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 189 """Check if the expected usage of the given function is as an instance method.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 190 try: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 191 # Python 3.3 and newer. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 192 signature = inspect.signature(function) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 193 return "self" in signature.parameters | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 194 except AttributeError: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 195 # Python 3.2 and older. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 196 metadata = inspect.getargspec(function) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 197 return "self" in metadata.args | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 198 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 199 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 200 class DeprecationProxy(types.ModuleType): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 201 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 202 """Emit deprecation warnings for imports that should be updated.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 203 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 204 def __init__(self, module, aliases): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 205 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 206 Initialize an :class:`DeprecationProxy` object. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 207 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 208 :param module: The original module object. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 209 :param aliases: A dictionary of aliases. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 210 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 211 # Initialize our superclass. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 212 super(DeprecationProxy, self).__init__(name=module.__name__) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 213 # Store initializer arguments. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 214 self.module = module | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 215 self.aliases = aliases | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 216 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 217 def __getattr__(self, name): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 218 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 219 Override module attribute lookup. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 220 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 221 :param name: The name to look up (a string). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 222 :returns: The attribute value. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 223 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 224 # Check if the given name is an alias. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 225 target = self.aliases.get(name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 226 if target is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 227 # Emit the deprecation warning. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 228 warnings.warn( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 229 format("%s.%s was moved to %s, please update your imports", self.module.__name__, name, target), | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 230 category=DeprecationWarning, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 231 stacklevel=2, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 232 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 233 # Resolve the dotted path. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 234 return self.resolve(target) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 235 # Look up the name in the original module namespace. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 236 value = getattr(self.module, name, None) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 237 if value is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 238 return value | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 239 # Fall back to the default behavior. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 240 raise AttributeError(format("module '%s' has no attribute '%s'", self.module.__name__, name)) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 241 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 242 def resolve(self, target): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 243 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 244 Look up the target of an alias. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 245 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 246 :param target: The fully qualified dotted path (a string). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 247 :returns: The value of the given target. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 248 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 249 module_name, _, member = target.rpartition(".") | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 250 module = importlib.import_module(module_name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 251 return getattr(module, member) | 
