mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
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:
parent
150af75432
commit
507a574de3
8 changed files with 133 additions and 22 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue