8000 bpo-43682: @staticmethod inherits attributes by vstinner · Pull Request #25268 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-43682: @staticmethod inherits attributes #25268

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.

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
merged 3 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix tests
  • Loading branch information
vstinner committed Apr 8, 2021
commit 80469c901d8a2efde429fed38dd7dfc08c8ab84e
18 changes: 13 additions & 5 deletions Lib/test/test_descr.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,9 @@ class D(C):
self.assertEqual(d.foo(1), (d, 1))
self.assertEqual(D.foo(d, 1), (d, 1))
# Test for a specific crash (SF bug 528132)
def f(cls, arg): return (cls, arg)
def f(cls, arg):
"f docstring"
return (cls, arg)
ff = classmethod(f)
self.assertEqual(ff.__get__(0, int)(42), (int, 42))
self.assertEqual(ff.__get__(0)(42), (int, 42))
Expand All @@ -1571,10 +1573,16 @@ def f(cls, arg): return (cls, arg)
self.fail("classmethod shouldn't accept keyword args")

cm = classmethod(f)
self.assertEqual(cm.__dict__, {})
cm_dict = {'__annotations__': {},
'__doc__': "f docstring",
'__module__': __name__,
'__name__': 'f',
'__qualname__': f.__qualname__}
self.assertEqual(cm.__dict__, cm_dict)

cm.x = 42
self.assertEqual(cm.x, 42)
self.assertEqual(cm.__dict__, {"x" : 42})
self.assertEqual(cm.__dict__, {"x" : 42, **cm_dict})
del cm.x
self.assertNotHasAttr(cm, "x")

Expand Down Expand Up @@ -1654,10 +1662,10 @@ class D(C):
self.assertEqual(d.foo(1), (d, 1))
self.assertEqual(D.foo(d, 1), (d, 1))
sm = staticmethod(None)
self.assertEqual(sm.__dict__, {})
self.assertEqual(sm.__dict__, {'__doc__': None})
sm.x = 42
self.assertEqual(sm.x, 42)
self.assertEqual(sm.__dict__, {"x" : 42})
self.assertEqual(sm.__dict__, {"x" : 42, '__doc__': None})
del sm.x
self.assertNotHasAttr(sm, "x")

Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,8 @@ def sm(x, y):
'''A static method'''
...
self.assertEqual(self._get_summary_lines(X.__dict__['sm']),
"<staticmethod object>")
'sm(...)\n'
' A static method\n')
self.assertEqual(self._get_summary_lines(X.sm), """\
sm(x, y)
A static method
Expand All @@ -1162,7 +1163,8 @@ def cm(cls, x):
'''A class method'''
...
self.assertEqual(self._get_summary_lines(X.__dict__['cm']),
"<classmethod object>")
'cm(...)\n'
' A class method\n')
self.assertEqual(self._get_summary_lines(X.cm), """\
cm(x) method of builtins.type instance
A class method
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ def test_descriptors(self):
class C:
def foo(cls): pass
x = staticmethod(C.foo)
self.assertTrue(repr(x).startswith('<staticmethod object at 0x'))
self.assertEqual(repr(x), f'<staticmethod({C.foo!r})>')
x = classmethod(C.foo)
self.assertTrue(repr(x).startswith('<classmethod object at 0x'))
self.assertEqual(repr(x), f'<classmethod({C.foo!r})>')

def test_unsortable(self):
# Repr.repr() used to call sorted() on sets, frozensets and dicts
Expand Down
0