GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43682: @staticmethod inherits attributes by vstinner · Pull Request #25268 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Apr 7, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Apr 7, 2021
@bedevere-bot bedevere-bot added the awaiting core review label Apr 7, 2021
@vstinner

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

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>)>
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
bpo-43682: @staticmethod inherits method attributes
b50eb07
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

vstinner commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner added 2 commits April 8, 2021 01:34
@vstinner
Factorize tests
1b1e12b
@vstinner
Fix tests
80469c9
@vstinner vstinner mentioned this pull request Apr 8, 2021
bpo-43682: Make staticmethod objects callable #25117
Merged
@vstinner
vstinner merged commit 507a574 into python:master Apr 9, 2021
@bedevere-bot bedevere-bot removed the awaiting core review label Apr 9, 2021
@vstinner
vstinner deleted the wraps_staticmethod branch April 9, 2021 15:51
AlexWaygood added a commit to AlexWaygood/typeshed that referenced this pull request Nov 12, 2021
@AlexWaygood
Add __name__/__qualname__/__wrapped__ to staticmethod/`classm…
0d22fe9
…ethod`

These attributes have all been added in Python 3.10+; see python/cpython#25268
@AlexWaygood AlexWaygood mentioned this pull request Nov 12, 2021
Add __name__/__qualname__/__wrapped__ to staticmethod/classmethod python/typeshed#6281
Merged
@scottcwang scottcwang mentioned this pull request Jan 27, 2022
AttributeError: 'staticmethod' object has no attribute '__name__' scottcwang/openssh_key_parser#1
Closed
@vstinner vstinner mentioned this pull request Jun 23, 2022
Make static methods created by @staticmethod callable #87848
Closed
@geryogam

geryogam commented May 21, 2023

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@carljm

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@geryogam

geryogam commented May 21, 2023

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.

carljm reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Jul 1, 2023
View reviewed changes
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();

@serhiy-storchaka serhiy-storchaka Jul 1, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@vstinner @geryogam @carljm @serhiy-storchaka @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.