<div dir="ltr">I have written a short PEP as a complement/alternative to PEP 549.<br>I will be grateful for comments and suggestions. The PEP should<br>appear online soon.<br><br>--<br>Ivan<br><br>***********************************************************<br><br>PEP: 562<br>Title: Module __getattr__<br>Author: Ivan Levkivskyi <<a href="mailto:levkivskyi@gmail.com">levkivskyi@gmail.com</a>><br>Status: Draft<br>Type: Standards Track<br>Content-Type: text/x-rst<br>Created: 09-Sep-2017<br>Python-Version: 3.7<br>Post-History: 09-Sep-2017<br><br><br>Abstract<br>========<br><br>It is proposed to support ``__getattr__`` function defined on modules to<br>provide basic customization of module attribute access.<br><br><br>Rationale<br>=========<br><br>It is sometimes convenient to customize or otherwise have control over<br>access to module attributes. A typical example is managing deprecation<br>warnings. Typical workarounds are assigning ``__class__`` of a module object<br>to a custom subclass of ``types.ModuleType`` or substituting ``sys.modules``<br>item with a custom wrapper instance. It would be convenient to simplify this<br>procedure by recognizing ``__getattr__`` defined directly in a module that<br>would act like a normal ``__getattr__`` method, except that it will be defined<br>on module *instances*. For example::<br><br>  # lib.py<br><br>  from warnings import warn<br><br>  deprecated_names = ["old_function", ...]<br><br>  def _deprecated_old_function(arg, other):<br>      ...<br><br>  def __getattr__(name):<br>      if name in deprecated_names:<br>          warn(f"{name} is deprecated", DeprecationWarning)<br>          return globals()[f"_deprecated_{name}"]<br>      raise AttributeError(f"module {__name__} has no attribute {name}")<br><br>  # main.py<br><br>  from lib import old_function  # Works, but emits the warning<br><br>There is a related proposal PEP 549 that proposes to support instance<br>properties for a similar functionality. The difference is this PEP proposes<br>a faster and simpler mechanism, but provides more basic customization.<br>An additional motivation for this proposal is that PEP 484 already defines<br>the use of module ``__getattr__`` for this purpose in Python stub files,<br>see [1]_.<br><br><br>Specification<br>=============<br><br>The ``__getattr__`` function at the module level should accept one argument<br>which is a name of an attribute and return the computed value or raise<br>an ``AttributeError``::<br><br>  def __getattr__(name: str) -> Any: ...<br><br>This function will be called only if ``name`` is not found in the module<br>through the normal attribute lookup.<br><br>The reference implementation for this PEP can be found in [2]_.<br><br><br>Backwards compatibility and impact on performance<br>=================================================<br><br>This PEP may break code that uses module level (global) name ``__getattr__``.<br>The performance implications of this PEP are minimal, since ``__getattr__``<br>is called only for missing attributes.<br><br><br>References<br>==========<br><br>.. [1] PEP 484 section about ``__getattr__`` in stub files<br>   (<a href="https://www.python.org/dev/peps/pep-0484/#stub-files">https://www.python.org/dev/peps/pep-0484/#stub-files</a>)<br><br>.. [2] The reference implementation<br>   (<a href="https://github.com/ilevkivskyi/cpython/pull/3/files">https://github.com/ilevkivskyi/cpython/pull/3/files</a>)<br><br><br>Copyright<br>=========<br><br>This document has been placed in the public domain.</div>