Skip to content

PropertyDocumenter causes inconsistent handling of Python descriptors #6785

Closed

Description

@zzzeek

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

  1. added a commit that references this issue on Nov 1, 2019
  2. flying-sheep commented on Jan 24, 2020

    @flying-sheep
    Contributor

    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.

  3. zzzeek commented on Jan 24, 2020

    @zzzeek
    Author

    also why is nobody from sphinx responding to this issue? it should at least be triaged

  4. flying-sheep commented on Jan 24, 2020

    @flying-sheep
    Contributor

    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 😢

  5. zzzeek commented on Jan 24, 2020

    @zzzeek
    Author

    sorry I deal w sphinx as infrequently as possible .... this bug was so egregious that I spent half a day figuring it out

  6. tk0miya commented on Jan 25, 2020

    @tk0miya
    Member

    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:.

  7. added a commit that references this issue on Jan 25, 2020
    33fcd39
  8. added
    type:enhancement
    and removed on Jan 25, 2020
  9. added this to the 2.4.0 milestone on Jan 25, 2020
  10. flying-sheep commented on Jan 25, 2020

    @flying-sheep
    Contributor

    Yeah your second paragraph is exactly what I mean, thank you!

  11. zzzeek commented on Jan 25, 2020

    @zzzeek
    Author

    I think "property" is not same as an attribute of the instance.

    that's not even the real problem here, the problem is that Python @property is treated differently from all other Python descriptors. Whether or not you want to call @property a 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.

    It sometimes allows only reading and disallows writing (assignment). This is one of reason why I separated properties from attributes.

    plain attributes can be "readonly" as well:

    
    class Foo(object):
        def __init__(self, bar):
            object.__setattr__(self, "bar", bar)
    
        def __setattr__(self, key, value):
            raise ValueError("Readonly object")
    
    f1 = Foo('bar')
    assert f1.bar =='bar'
    
    
    f1.bar = 'bat'
    

    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.

  12. tk0miya commented on Jan 25, 2020

    @tk0miya
    Member

    that's not even the real problem here, the problem is that Python @Property is treated differently from all other Python descriptors. Whether or not you want to call @Property a 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.

    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).

  13. zzzeek commented on Jan 25, 2020

    @zzzeek
    Author

    What makes @property different from any other descriptor? What if I make a descriptor that matches what @property does exactly? Why is one a method and the other is an attribute?

  14. flying-sheep commented on Jan 27, 2020

    @flying-sheep
    Contributor

    Methods are implemented using descriptors, yes. That’s to make self work, i.e. it works at the time when you access the method like an attribute:

    bound_method = instance.method  # the descriptor behavior happens here
    bound_method()  # the thing we document, the call signature of the bound method, is relevant here

    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.inv as py:attribute.

  15. added a commit that references this issue on Jan 27, 2020
    5d85237
  16. tk0miya commented on Feb 1, 2020

    @tk0miya
    Member

    I think merging #7061 is enough to fix this. It allows to you to refer properties via py:attr role.
    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.

  17. flying-sheep commented on Feb 1, 2020

    @flying-sheep
    Contributor

    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_text for 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.

  18. added a commit that references this issue on Feb 6, 2020
    4198219
  19. modified the milestones: 2.4.0, 3.0.0 on Feb 7, 2020
  20. added a commit that references this issue on Feb 13, 2020
    ab871f5
  21. added 2 commits that reference this issue on Mar 2, 2020
    9279192
    bc456bf
  22. locked as resolved and limited conversation to collaborators on Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    domains:pytype:enhancement

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions