Repository navigation
Description
Describe the bug
There is a class PropertyDocumenter at https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/autodoc/__init__.py#L1380 which defines half of a Python role, the "property", which nonetheless cannot be independently accessed as it folds itself under the "method" directivetype. This causes Python descriptors which happen to use @property to be treated entirely differently from all other descriptors, which otherwise get classified as "attribute". It is based on a hardcoded check for "property" at https://github.com/sphinx-doc/sphinx/blob/master/sphinx/util/inspect.py#L220 . most importantly, it means that when I refer to an :attr: somewhere in my documentation, I now have to know that this particular attribute happened to be a @property and not any other kind of descriptor, and reference it using :method: rather than :attr:. Then, if I change the use of @property with my own caching descriptor, which occurs all the time, all my references to this attribute are now broken. It also generates an ugly "property" word in the output, which is again inconsistent from any other descriptor.
It's basically not pythonic to check the type of something using "isinstance()" when there is a widely known duck type available, that is, looking for __get__() like the descriptor protocol does. There should instead be a DescriptorDocumenter that looks for __get__() on the wrapper just like Python descriptor protocol, and then it needs to classify itself as "attribute" so that these descriptors can be swapped in and out for plain class attributes, which is the whole point of descriptors. a @property should never be considered as a "method".
I'm not sure if this object was introduced at some point and broke existing documentation, or if this was always the behavior once :attr: was supported.
To Reproduce
Steps to reproduce the behavior:
define two classes, as well as a custom descriptor class:
class Foo(object):
@property
def bar(self):
"""the bar attribute"""
return "the bar"
class _some_descriptor(object):
def __init__(self, fn):
self.fn = fn
def __get__(self, obj, cls):
if obj is None:
return self
return self.fn(obj)
class Bar(object):
@_some_descriptor
def bar(self):
"""the bar attribute"""
return "the bar"
sphinx will classify Foo.bar as a "method" which will not resolve for :attr:`.Foo.bar` and will classify Bar.bar as an "attribute" which will not resolve for :meth:`.Bar.bar`.
Expected behavior
obviously this is behavior that people are relying on by now, however it is IMO entirely wrong and I need a supported method of turning it off. I will be looking into hardcoded ways to work around it for now (this is for SQLAlchemy), likely just setting PropertyDocumenter.priority to zero. I know that :obj: is also a workaround but I have many thousands of methods and attributes and the last thing I need is more ambiguity in my documentation source.
Your project
https://docs.sqlalchemy.org/
Activity
- disable PropertyDocumenter
flying-sheep commentedon Jan 24, 2020
I totally agree, referring to properties via
:meth:makes no sense. Properties are there to support transparently changing a humble attribute to have behavior on getting/setting, without breaking code or documentation references.zzzeek commentedon Jan 24, 2020
also why is nobody from sphinx responding to this issue? it should at least be triaged
flying-sheep commentedon Jan 24, 2020
I recently got a quick answer in #7060, but I closed that one in favor of this one. Let’s wait for @tk0miya.
Damn, I’m really not prepared to deal with this as well right now. All the links are breaking.
Do you have any idea how to extend your workaround to also fix the objects.inv entries? They still end up as
std:method😢zzzeek commentedon Jan 24, 2020
sorry I deal w sphinx as infrequently as possible .... this bug was so egregious that I spent half a day figuring it out
tk0miya commentedon Jan 25, 2020
I think "property" is not same as an attribute of the instance. It sometimes allows only reading and disallows writing (assignment). This is one of reason why I separated properties from attributes.
But I agree properties are usually used as an attribute-like accessor for the instance. And clients of the object don't need to know how the accessor is implemented. So +0 for referable by
:py:attr:.Fix sphinx-doc#6785: py domain: :py:attr: is able to refer properties…
flying-sheep commentedon Jan 25, 2020
Yeah your second paragraph is exactly what I mean, thank you!
zzzeek commentedon Jan 25, 2020
that's not even the real problem here, the problem is that Python
@propertyis treated differently from all other Python descriptors. Whether or not you want to call@propertya method, an attribute, or a ":property:", it should apply to all descriptors, not just one arbitrary one that's present in the Python standard library.plain attributes can be "readonly" as well:
Sphinx's priority is to document attributes and methods based on how they are initially accessed and IMO it can't really get involved with guessing how they will behave or not. I'm glad you have made this change.
tk0miya commentedon Jan 25, 2020
I don't think so. It would be better to represent each descriptors more appropriate form. For example, method is consist of descriptor. But it is not good to describe method as an attribute. classmethods and staticmethods are also. I know it is hard to give "appropriate form" to all of descriptors. So other descriptors are described as attributes at this moment. But it would be better to improve them also (if possible).
zzzeek commentedon Jan 25, 2020
What makes
@propertydifferent from any other descriptor? What if I make a descriptor that matches what@propertydoes exactly? Why is one a method and the other is an attribute?flying-sheep commentedon Jan 27, 2020
Methods are implemented using descriptors, yes. That’s to make
selfwork, i.e. it works at the time when you access the method like an attribute:This means that we document the thing we get after invoking a descriptor:
In case of a method, we get and document a bound method, a callable thing where the call signature is interesting.
It makes sense to refer to this as
:meth:In case of a property, we get and document a value, something like an attribute which (usually) isn’t callable, where the type is interesting.
It does not make sense to refer to this as
:meth:Please make sure properties end up in
objects.invaspy:attribute.Document properties as attributes
tk0miya commentedon Feb 1, 2020
I think merging #7061 is enough to fix this. It allows to you to refer properties via
py:attrrole.Please let me know why do you want to change a type in objects.inv? I believe it is internal data and users don't need to mind about its form.
flying-sheep commentedon Feb 1, 2020
No offense, but
directivetype = 'method'in #6320 was simply a mistake that #7068 corrects.#7068 gets rid of cases of exceptional behavior, and fixes
get_index_textfor properties (where exceptional behavior would have been correct but was overlooked). #7068 therefore reduces technical debt.#7061 on the other hand is a hack that adds adds another exception. #7061 therefore increases technical debt.
Merge pull request #7061 from tk0miya/6785_attr_can_refer_props
Document properties as attributes
Document properties as attributes
Document properties as attributes