10000 bpo-24766: doc= argument to subclasses of property not handled correctly by embray · Pull Request #2487 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-24766: doc= argument to subclasses of property not handled correctly #2487

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 5 commits into from
Jun 10, 2024
Merged
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
34 changes: 34 additions & 0 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,40 @@ def getter3(self):
self.assertEqual(p.__doc__, "user")
self.assertEqual(p2.__doc__, "user")

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_prefer_explicit_doc(self):
# Issue 25757: subclasses of property lose docstring
self.assertEqual(property(doc="explicit doc").__doc__, "explicit doc")
self.assertEqual(PropertySub(doc="explicit doc").__doc__, "explicit doc")

class Foo:
spam = PropertySub(doc="spam explicit doc")

@spam.getter
def spam(self):
"""ignored as doc already set"""
return 1

def _stuff_getter(self):
"""ignored as doc set directly"""
stuff = PropertySub(doc="stuff doc argument", fget=_stuff_getter)

#self.assertEqual(Foo.spam.__doc__, "spam explicit doc")
self.assertEqual(Foo.stuff.__doc__, "stuff doc argument")

def test_property_no_doc_on_getter(self):
# If a property's getter has no __doc__ then the property's doc should
# be None; test that this is consistent with subclasses as well; see
# GH-2487
class NoDoc:
@property
def __doc__(self):
raise AttributeError

self.assertEqual(property(NoDoc()).__doc__, None)
self.assertEqual(PropertySub(NoDoc()).__doc__, None)

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_property_setter_copies_getter_docstring(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of ``doc`` argument to subclasses of ``property``.
19 changes: 4 additions & 15 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,22 +1783,9 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
/* if no docstring given and the getter has one, use that one */
else if (fget != NULL) {
int rc = PyObject_GetOptionalAttr(fget, &_Py_ID(__doc__), &prop_doc);
if (rc <= 0) {
if (rc < 0) {
return rc;
}
if (!Py_IS_TYPE(self, &PyProperty_Type) &&
prop_doc != NULL && prop_doc != Py_None) {
// This oddity preserves the long existing behavior of surfacing
// an AttributeError when using a dict-less (__slots__) property
// subclass as a decorator on a getter method with a docstring.
// See PropertySubclassTest.test_slots_docstring_copy_exception.
int err = PyObject_SetAttr(
(PyObject *)self, &_Py_ID(__doc__), prop_doc);
if (err < 0) {
Py_DECREF(prop_doc); // release our new reference.
return -1;
}
}
if (prop_doc == Py_None) {
prop_doc = NULL;
Py_DECREF(Py_None);
Expand Down Expand Up @@ -1826,7 +1813,9 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
Py_DECREF(prop_doc);
if (err < 0) {
assert(PyErr_Occurred());
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
if (!self->getter_doc &&
PyErr_ExceptionMatches(PyExc_AttributeError))
{
PyErr_Clear();
// https://github.com/python/cpython/issues/98963#issuecomment-1574413319
// Python silently dropped this doc assignment through 3.11.
Expand Down
0