bpo-43682: @staticmethod inherits attributes (GH-25268)

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.
This commit is contained in:
Victor Stinner 2021-04-09 17:51:22 +02:00 committed by GitHub
parent 150af75432
commit 507a574de3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 133 additions and 22 deletions

View file

@ -1,3 +1,4 @@
from test import support
import unittest
def funcattrs(**kwds):
@ -76,11 +77,28 @@ class TestDecorators(unittest.TestCase):
self.assertEqual(C.foo(), 42)
self.assertEqual(C().foo(), 42)
def test_staticmethod_function(self):
@staticmethod
def notamethod(x):
def check_wrapper_attrs(self, method_wrapper, format_str):
def func(x):
return x
self.assertRaises(TypeError, notamethod, 1)
wrapper = method_wrapper(func)
self.assertIs(wrapper.__func__, func)
self.assertIs(wrapper.__wrapped__, func)
for attr in ('__module__', '__qualname__', '__name__',
'__doc__', '__annotations__'):
self.assertIs(getattr(wrapper, attr),
getattr(func, attr))
self.assertEqual(repr(wrapper), format_str.format(func))
self.assertRaises(TypeError, wrapper, 1)
def test_staticmethod(self):
self.check_wrapper_attrs(staticmethod, '<staticmethod({!r})>')
def test_classmethod(self):
self.check_wrapper_attrs(classmethod, '<classmethod({!r})>')
def test_dotted(self):
decorators = MiscDecorators()