Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 12 additions & 10 deletions Lib/test/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,13 @@ def inner(cls):
# This should already work.
return 'spam'

@classmethod
@decorator
def outer(cls):
# Raised TypeError with a message saying that the 'Wrapper'
# object is not callable.
return 'eggs'
with self.assertWarns(FutureWarning):
@classmethod
@decorator
def outer(cls):
# Raised TypeError with a message saying that the 'Wrapper'
# object is not callable.
return 'eggs'

self.assertEqual(Class.inner(), 'spam')
self.assertEqual(Class.outer(), 'eggs')
Expand Down Expand Up @@ -385,10 +386,11 @@ def f2(cls):
def f3(cls):
return cls

@classmethod
@classmethod
def f4(cls):
return cls
with self.assertWarns(FutureWarning):
@classmethod
@classmethod
def f4(cls):
return cls

@myclassmethod
@MyClassMethod1
Expand Down
21 changes: 12 additions & 9 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import shutil
import types
import contextlib
import warnings


if not support.has_subprocess_support:
Expand Down Expand Up @@ -103,14 +104,16 @@ def a_classmethod(cls, v):

a_class_attribute = 42

@classmethod
@property
def a_classmethod_property(cls):
"""
>>> print(SampleClass.a_classmethod_property)
42
"""
return cls.a_class_attribute
with warnings.catch_warnings():
warnings.simplefilter('ignore', FutureWarning)
@classmethod
@property
def a_classmethod_property(cls):
"""
>>> print(SampleClass.a_classmethod_property)
42
"""
return cls.a_class_attribute

class NestedClass:
"""
Expand Down Expand Up @@ -460,7 +463,7 @@ def basics(): r"""
>>> tests = finder.find(sample_func)

>>> print(tests) # doctest: +ELLIPSIS
[<DocTest sample_func from test_doctest.py:33 (1 example)>]
[<DocTest sample_func from test_doctest.py:34 (1 example)>]

The exact name depends on how test_doctest was invoked, so allow for
leading path components.
Expand Down
18 changes: 10 additions & 8 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,23 @@ def test_refleaks_in___init__(self):
"Docstrings are omitted with -O2 and above")
def test_class_property(self):
class A:
@classmethod
@property
def __doc__(cls):
return 'A doc for %r' % cls.__name__
with self.assertWarns(FutureWarning):
@classmethod
@property
def __doc__(cls):
return 'A doc for %r' % cls.__name__
self.assertEqual(A.__doc__, "A doc for 'A'")

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_class_property_override(self):
class A:
"""First"""
@classmethod
@property
def __doc__(cls):
return 'Second'
with self.assertWarns(FutureWarning):
@classmethod
@property
def __doc__(cls):
return 'Second'
self.assertEqual(A.__doc__, 'Second')

def test_property_set_name_incorrect_args(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Emit :exc:`FutureWarning` when :func:`classmethod` wraps a non-callable
descriptor (such as :class:`property`).
12 changes: 11 additions & 1 deletion Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,9 @@ cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
}
if (type == NULL)
type = (PyObject *)(Py_TYPE(obj));
if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL &&
!PyCallable_Check(cm->cm_callable))
{
return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
type);
}
Expand All @@ -913,6 +915,14 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
return -1;
if (Py_TYPE(callable)->tp_descr_get != NULL && !PyCallable_Check(callable)) {
if (PyErr_WarnFormat(PyExc_FutureWarning, 1,
"chaining classmethod and %.200s is deprecated",
Py_TYPE(callable)->tp_name))
{
return -1;
}
}
Py_INCREF(callable);
Py_XSETREF(cm->cm_callable, callable);

Expand Down