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: Make staticmethod objects callable by vstinner · Pull Request #25117 · python/cpython

bpo-43682: Make staticmethod objects callable #25117

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.

Sign up for GitHub

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

Merged
vstinner merged 2 commits into python:master from vstinner:callable_staticmethod
Apr 11, 2021

Uh oh!

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

Merged

bpo-43682: Make staticmethod objects callable #25117

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
a43910e
bpo-43682: Make staticmethod objects callable
vstinner Mar 31, 2021
65c8a6a
Update test_pydoc
vstinner Apr 11, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

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

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

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

Diff view
Diff view

There are no files selected for viewing

15 changes: 10 additions & 5 deletions 15 Doc/library/functions.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1619,8 +1619,9 @@ are always available. They are listed here in alphabetical order.
The ``@staticmethod`` form is a function :term:`decorator` -- see
:ref:`function` for details.

A static method can be called either on the class (such as ``C.f()``) or on an instance (such
as ``C().f()``).
A static method can be called either on the class (such as ``C.f()``) or on
an instance (such as ``C().f()``). Moreover, they can be called as regular
functions (such as ``f()``).

Static methods in Python are similar to those found in Java or C++. Also see
:func:`classmethod` for a variant that is useful for creating alternate class
Expand All @@ -1632,15 +1633,19 @@ are always available. They are listed here in alphabetical order.
body and you want to avoid the automatic transformation to instance
method. For these cases, use this idiom::

def regular_function():
...

class C:
builtin_open = staticmethod(open)
method = staticmethod(regular_function)

For more information on static methods, see :ref:`types`.

.. versionchanged:: 3.10
Static methods now inherit the method attributes (``__module__``,
``__name__``, ``__qualname__``, ``__doc__`` and ``__annotations__``) and
have a new ``__wrapped__`` attribute.
``__name__``, ``__qualname__``, ``__doc__`` and ``__annotations__``),
have a new ``__wrapped__`` attribute, and are now callable as regular
functions.


.. index::
Expand Down
5 changes: 2 additions & 3 deletions 5 Doc/reference/datamodel.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,8 @@ Internal types
around any other object, usually a user-defined method object. When a static
method object is retrieved from a class or a class instance, the object actually
returned is the wrapped object, which is not subject to any further
transformation. Static method objects are not themselves callable, although the
objects they wrap usually are. Static method objects are created by the built-in
:func:`staticmethod` constructor.
transformation. Static method objects are also callable. Static method
objects are created by the built-in :func:`staticmethod` constructor.

Class method objects
A class method object, like a static method object, is a wrapper around another
Expand Down
1 change: 1 addition & 0 deletions 1 Doc/whatsnew/3.10.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ Other Language Changes
(:func:`@classmethod <classmethod>`) now inherit the method attributes
(``__module__``, ``__name__``, ``__qualname__``, ``__doc__``,
``__annotations__``) and have a new ``__wrapped__`` attribute.
Moreover, static methods are now callable as regular functions.
(Contributed by Victor Stinner in :issue:`43682`.)


Expand Down
12 changes: 8 additions & 4 deletions 12 Lib/test/test_decorators.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ def func(x):
getattr(func, attr))

self.assertEqual(repr(wrapper), format_str.format(func))

self.assertRaises(TypeError, wrapper, 1)
return wrapper

def test_staticmethod(self):
self.check_wrapper_attrs(staticmethod, '<staticmethod({!r})>')
wrapper = self.check_wrapper_attrs(staticmethod, '<staticmethod({!r})>')

# bpo-43682: Static methods are callable since Python 3.10
self.assertEqual(wrapper(1), 1)

def test_classmethod(self):
self.check_wrapper_attrs(classmethod, '<classmethod({!r})>')
wrapper = self.check_wrapper_attrs(classmethod, '<classmethod({!r})>')

self.assertRaises(TypeError, wrapper, 1)

def test_dotted(self):
decorators = MiscDecorators()
Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/test/test_pydoc.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ def sm(x, y):
'''A static method'''
...
self.assertEqual(self._get_summary_lines(X.__dict__['sm']),
'sm(...)\n'
'sm(x, y)\n'
' A static method\n')
self.assertEqual(self._get_summary_lines(X.sm), """\
sm(x, y)
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Core and Builtins/2021-03-31-16-32-57.bpo-43682.VSF3vg.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Static methods (:func:`@staticmethod <staticmethod>`) are now callable as
regular functions. Patch by Victor Stinner.
9 changes: 8 additions & 1 deletion 9 Objects/funcobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,13 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds)
return 0;
}

static PyObject*
sm_call(PyObject *callable, PyObject *args, PyObject *kwargs)
{
staticmethod *sm = (staticmethod *)callable;
return PyObject_Call(sm->sm_callable, args, kwargs);
}

static PyMemberDef sm_memberlist[] = {
{"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
{"__wrapped__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
Expand Down Expand Up @@ -1107,7 +1114,7 @@ PyTypeObject PyStaticMethod_Type = {
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
sm_call, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
Expand Down
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.