GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing github.com — bpo-43682: Make staticmethod objects callable by vstinner · Pull Request #25117 · python/cpython Conversation
|
|
PR rebased to fix the enum doc fix from master. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I rebased my PR and reverted the io and_pyio changes (they should be made in a separated PR). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@serhiy-storchaka @gvanrossum @rhettinger: so, what do you think? this idea was rejected in 2015, but came back in 2021 and Guido likes the idea to make static methods callable. See also PR #25268 which makes staticmethod more "usable" to be used directly as a function. It's a tricky topic. We could also require to always use But sometimes, we need functions which can be used directly as method without staticmethod(), to mimick built-in function. Well see https://bugs.python.org/issue43682 and https://bugs.python.org/issue20309 discussions ;-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I like the idea. Why was it rejected in 2015? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Victor want to replace In any case |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I solved this in PR #25268, did you see my PR?
See also https://bugs.python.org/issue43682#msg389907:
It's a similar issue than PEP 570 (positional-only arguments) solved for Python re-implementation of a C extension. We should either prevent C extensions to behave than Python, or we should allow pure Python code to behave the same. Here the issue is complex (changing built-in functions or Python functions to add/remove descriptor), and I propose to only change staticmethod(). In the whole stdlib, I'm only aware of io.open which is used sometimes directly to define a method (without |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I merged my PR #25268 and rebased this PR on top of it. By the way, just for consistency, should we also make class methods callable? I have no use case for that :-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
By the way, just for consistency, should we also make class methods
callable? I have no use case for that :-)
I thought those are callable already.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Guido:
Me too, but hey, static methods and class methods are weird! It's only callable when it goes throught the descriptor! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Well, ignore my idea of making class methods callable. It makes no sense since it doesn't pass the class in this case. Example: The last call fails because it doesn't go trought the descriptor, and so the class method doesn't get the class argument. Output: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
You should pass that in explicitly in that case. I guess calling |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
But please first fix the comment about class methods being callable.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Class methods aren't callable yet.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Static methods (@staticmethod) are now callable as regular functions.
|
I fixed test_pydoc which shows a nice enhancement of this PR ;-) With this PR, inspect.signature() works on a static method, and returns the same signature than the wrapped callable object. On Python 3.9 (and in master without this change), inspect.signature(wrapper) fails with "TypeError: <staticmethod...> is not a callable object". |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for the review @gvanrossum. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It is possible to wrap a static method into a new static method, Moreover, I expect that most decorators have a similar issue. Maybe a linter or a debug check can warn on that, but I don't think that staticmethod() must raise an error or return the first (static method) wrapper unchanged. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.