-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Subclass of property doesn't preserve instance __doc__ when using doc= argument #68954
Comments
This issue is directly related to http://bugs.python.org/issue5890, the solution to which I think was incomplete. The examples below use a trivial subclass of property (but apply as well to a less trivial one): >>> class myproperty(property): pass
... When using myproperty with the decorator syntax, or simply without specifying a doc= argument, the docstring is properly inherited from the getter, as was fixed by bpo-5890: >>> class A:
... @myproperty
... def foo(self):
... """The foo."""
... return 1
...
>>> A.foo.__doc__
'The foo.' However, when using the doc= argument, this behavior is broken: >>> class B:
... def _get_foo(self): return 1
... foo = myproperty(_get_foo, doc="The foo.")
...
>>> B.foo.__doc__
>>> B.foo.__doc__ is None
True The attached patch resolves the issue by applying the special case for subclasses more generally. If this looks good I'll add a test as well. One thing I went back and forth on in the "if (Py_TYPE(self) != &PyProperty_Type)" block was whether or not to then deref prop->prop_doc and set it to NULL, since I don't think it's needed anymore at this point. But I decided it was ultimately harmless to leave it. |
Looks good so far. Let's get some tests in place. |
Sorry for the hold up. Attached is another diff providing a test. I think this is all that's really needed (since this is just a special case of the issue already tested for in this particular test class. |
Larry, can we get this into 3.5? I'll create a pull-request in a couple days. |
ethan@code:~/source/python/issue25147$ ./python -m test.regrtest -R3:3 test_property Eggs # from print() inside test beginning 6 repetitions Spam # the new value from the previous run seems stuck test test_property failed -- Traceback (most recent call last):
File "/home/ethan/source/python/issue25147/Lib/test/test_property.py", line 175, in test_property_decorator_doc_writable
self.assertEqual(sub.__class__.spam.__doc__, 'Eggs')
AssertionError: 'Spam' != 'Eggs'
- Spam
+ Eggs 1 test failed: |
Interesting--in fairness to myself, that test seems to fail without my patch too (without the -R it passes, but with -R3:3 it fails). So it looks like a pre-existing issue. But I'll see if I can do something about that while I'm at it. |
Actually, of course that test would fail when run repeatedly--it sets the property docstring from 'Eggs' to 'Spam' on the first run, but then doesn't set it back to its original value. Since the PropertyWritableDocs class used in that test is module-level it doesn't get reset. I'd just update that test to return the docstring to its original value if you want it to pass under such a condition. |
Attached an additional patch to test_property_decorator_doc_writable so that it can pass on repeated runs. |
Thanks for checking that out, Erik. I was hoping it was a testing issue, but I ran out of time to verify. |
test_property_decorator_doc_writable fix has already been committed in cc1aa0e88626. Here's an updated patch:
|
Thanks for the updated patch. LGTM. |
Reproduced on 3.11. |
@serhiy-storchaka thanks for helping get this done. I've been totally inactive in core Python development for some years now and forgot about this. Always fun to see some very minor bug reported 9 years ago fixed 🙃 |
You are welcome. I'm working on pushing old forgotten PRs. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: