[issue33380] Update module attribute on namedtuple methods for introspection.
Raymond Hettinger
report at bugs.python.org
Mon Apr 30 21:53:28 EDT 2018
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
ISTM collections is the correct module reference because that is where the code is actually defined. A debugging tool should look there instead of in the calling code. This is the way other tools work in Python as well:
>>> from collections.abc import Set
>>> class S(Set):
def __init__(self): pass
def __iter__(self): yield 0
def __len__(self): return 0
def __contains__(self, key): return False
>>> S.__or__.__module__
'collections.abc'
>>> from dataclasses import make_dataclass
>>> P = make_dataclass('P', ['x', 'y'])
>>> P.__repr__.__module__
'dataclasses'
Likewise, the various tools that use closures used to report the module where the closure was defined rather than the module of the caller's code (see functools.cmp_to_key or functools.lru_cache). I say "used to" because the pure Python code is now supplanted by C equivalents where the __module__ attribute__ is appropriately set to None:
# Running from PyPy
>>>> from functools import cmp_to_key
>>>> c = cmp_to_key(lambda x, y: 0)
>>>> c.__init__.__module__
'_functools'
The Data Model section of the Language Reference defines __module__ as follows, "__module__: The name of the module the function was defined in, or None if unavailable." In the prior version of namedtuple(), the function was defined in a virtual module (an execed string). Now, that the methods are defined in a real module, the __module__ attribute should name that real module.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33380>
_______________________________________
More information about the Python-bugs-list
mailing list