FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-43682: @staticmethod inherits attributes by vstinner · Pull Request #25268 · python/cpython · GitHub

Repository navigation

bpo-43682: @staticmethod inherits attributes - #25268

Merged
vstinner merged 3 commits into
python:masterfrom
vstinner:wraps_staticmethod
Apr 9, 2021
Merged

vstinner merged 3 commits into
python:masterfrom
vstinner:wraps_staticmethod

Conversation

vstinner commented Apr 7, 2021 •
edited
Loading

Copy link
Copy Markdown
Member

Static methods (@staticmethod) and class methods (@classmethod) now
inherit the method attributes (module, name, qualname,
doc, annotations) and have a new wrapped attribute.

Changes:

  • Add a repr() method to staticmethod and classmethod types.
  • Add tests on the @classmethod decorator.

https://bugs.python.org/issue43682

vstinner commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

functools_wraps() call in cm_init() failed because Python didn't initialize PyClassMethod_Type type at startup. I created https://bugs.python.org/issue43770 to fix the issue: it's now fixed by df5dc1c

vstinner commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

I chose the following representation:

$ ./python -q
>>> def func(): pass
... 
>>> staticmethod(func)
<staticmethod(<function func at 0x7f39e8152c50>)>
>>> classmethod(func)
<classmethod(<function func at 0x7f39e8152c50>)>

Static methods (@staticmethod) and class methods (@classmethod) now
inherit the method attributes (__module__, __name__, __qualname__,
__doc__, __annotations__) and have a new __wrapped__ attribute.

Changes:

* Add a repr() method to staticmethod and classmethod types.
* Add tests on the @classmethod decorator.

vstinner commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka

Copy link
Copy Markdown
Contributor

@vstinner What’s this new __wrapped__ attribute? Is it the same as the existing __func__ attribute? If so, why did you introduce it?

carljm commented May 21, 2023

Copy link
Copy Markdown
Member

What’s this new __wrapped__ attribute? Is it the same as the existing __func__ attribute? If so, why did you introduce it?

It's a standard attribute added by functools.wraps to wrapper functions allowing unwrapping back to the wrapped function (eg by inspect.unwrap). So it's generally useful for wrappers to set it.

Copy link
Copy Markdown
Contributor

@carljm Thanks, so I guess in this case it’s a documented duplicate of __func__ which is an undocumented attribute.

Comment thread Objects/funcobject.c
Comment on lines +721 to +724
PyObject *value = PyObject_GetAttr(wrapped, name);
if (value == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It is better to use _PyObject_LookupAttr() here. See #106303.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL