Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty __module__ attribute for built-in static methods #115231

Open
skirpichev opened this issue Feb 10, 2024 · 4 comments
Open

Empty __module__ attribute for built-in static methods #115231

skirpichev opened this issue Feb 10, 2024 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@skirpichev
Copy link
Member

skirpichev commented Feb 10, 2024

Bug report

Bug description:

An example:

>>> bytes.maketrans.__module__ is None
True

c.f. pure-python staticmethod:

>>> class Spam:
...     @staticmethod
...     def foo():
...         pass
... 
>>> Spam.foo.__module__
'__main__'

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.

Edit: BTW, same happens for class methods, e.g.:

>>> int.from_bytes.__module__ is None
True

Probably this should be fixed as well.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@skirpichev skirpichev added the type-bug An unexpected behavior, bug, or error label Feb 10, 2024
skirpichev added a commit to skirpichev/cpython that referenced this issue Feb 26, 2024
skirpichev added a commit to skirpichev/cpython that referenced this issue Jul 19, 2024
@serhiy-storchaka
Copy link
Member

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

@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement 3.14 new features, bugs and security fixes and removed type-bug An unexpected behavior, bug, or error labels Nov 2, 2024
@skirpichev
Copy link
Member Author

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.
>>> class Spam:
...     @staticmethod
...     def foo():
...         pass
... 
>>> Spam.foo.__module__
'__main__'
>>> 

@serhiy-storchaka
Copy link
Member

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__ is None
True

We should first decide whether we need the __module__ attribute for regular builtin methods.

@skirpichev
Copy link
Member Author

skirpichev commented Nov 2, 2024

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.

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed 3.14 new features, bugs and security fixes labels Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants