You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Empty __module__ attribute for built-in static methods #115231
I'm not sure, maybe this should be treated rather as a feature request. Docs says about the attribute value: "The name of the module the function was defined in, or None if unavailable." But clearly, this value is available: the type_add_method() has type argument and we could query value of its __module__ attribute.
Static and class methods in Python classes started to inherit function attributes only since 3.10
Hmm, above code snippet works on 3.8+ as well, e.g.:
Python 3.9.19+ (heads/3.9:3f5d9d12c7, Aug 29 2024, 13:17:09)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> classSpam:
... @staticmethod
... deffoo():
... pass
...
>>> Spam.foo.__module__
'__main__'
>>>
Static and class methods in Python classes copy attributes from the wrapped methods. But unbound built-in methods do not have the __module__ attribute, so not copying it is okay. On other hand, bound built-in methods have __module__ equal to None:
>>> collections.deque.append.__module__
Traceback (most recent call last):
File "<python-input-15>", line 1, in <module>
collections.deque.append.__module__
AttributeError: 'method_descriptor' object has no attribute '__module__'. Did you mean: '__reduce__'?
>>> collections.deque().append.__module__isNone
True
We should first decide whether we need the __module__ attribute for regular builtin methods.
We should first decide whether we need the __module__ attribute for regular builtin methods.
That should be helpful for introspection. E.g. now the inspect._signature_fromstr() resolves module_dict to empty for e.g. bytes.maketrans() or int.from_bytes(). That essentially forbids e.g. using module constants as default values.
Activity
pythongh-115231: fill __module__ attribute for built-in staticmethods
pythongh-115231: fill __module__ for built-in class/staticmethods
Revert "pythongh-115231: fill __module__ for built-in class/staticmet…
serhiy-storchaka commentedon Nov 2, 2024
Built-in static and class methods has different types than static and class methods in Python classes. This is not a bug.
Static and class methods in Python classes started to inherit function attributes only since 3.10 (bpo-43682/gh-87848). cc @vstinner
skirpichev commentedon Nov 2, 2024
Hmm, above code snippet works on 3.8+ as well, e.g.:
serhiy-storchaka commentedon Nov 2, 2024
Static and class methods in Python classes copy attributes from the wrapped methods. But unbound built-in methods do not have the
__module__attribute, so not copying it is okay. On other hand, bound built-in methods have__module__equal to None:We should first decide whether we need the
__module__attribute for regular builtin methods.skirpichev commentedon Nov 2, 2024
That should be helpful for introspection. E.g. now the
inspect._signature_fromstr()resolvesmodule_dictto empty for e.g.bytes.maketrans()orint.from_bytes(). That essentially forbids e.g. using module constants as default values.gh-115231: Fill __module__ for built-in staticmethods (#115232)
pythongh-115231: Fill __module__ for built-in staticmethods (python#1…
pythongh-115231: Fill __module__ for built-in staticmethods (python#1…