Sphinx: None by by default for instance attributes?

Created on 15 Sep 2015  路  2Comments  路  Source: sphinx-doc/sphinx

When I'm included docstrings for instance attributes in the __init__ of a class then using autoclass, I always end up with

attr = None
    Documentation of attr

due to this line

https://github.com/sphinx-doc/sphinx/blob/add4c2467d9f98cadadff51593ca1727b4a81ca1/sphinx/ext/autodoc.py#L1465

Is there a use case for documenting the = None. Can it just always be suppressed? There's no way right now to pass a annotation option to autoclass AFAICT.

autodoc proposal

Most helpful comment

You can put a monkeypatch in the conf.py of your project that fixes this problem:

from sphinx.ext.autodoc import (
    ClassLevelDocumenter, InstanceAttributeDocumenter)

def iad_add_directive_header(self, sig):
    ClassLevelDocumenter.add_directive_header(self, sig)

InstanceAttributeDocumenter.add_directive_header = iad_add_directive_header

Currently, instance attributes are handled by the InstanceAttributeDocumenter class which inherits the method add_directive_header from AttributeDocumenter. It seems to me that instance attributes should never have an annotation set (it's always going to be 'None'). Therefore, it would be very easy to just add a method

def add_directive_header(self, sig):
    # type: (unicode) -> None
    ClassLevelDocumenter.add_directive_header(self, sig)

to InstanceAttributeDocumenter that overrides the parent method. Am I overlooking something? It seems like a fairly trivial change, so if people agree that this is an acceptable solution, I could probably make a pull request.

All 2 comments

You can put a monkeypatch in the conf.py of your project that fixes this problem:

from sphinx.ext.autodoc import (
    ClassLevelDocumenter, InstanceAttributeDocumenter)

def iad_add_directive_header(self, sig):
    ClassLevelDocumenter.add_directive_header(self, sig)

InstanceAttributeDocumenter.add_directive_header = iad_add_directive_header

Currently, instance attributes are handled by the InstanceAttributeDocumenter class which inherits the method add_directive_header from AttributeDocumenter. It seems to me that instance attributes should never have an annotation set (it's always going to be 'None'). Therefore, it would be very easy to just add a method

def add_directive_header(self, sig):
    # type: (unicode) -> None
    ClassLevelDocumenter.add_directive_header(self, sig)

to InstanceAttributeDocumenter that overrides the parent method. Am I overlooking something? It seems like a fairly trivial change, so if people agree that this is an acceptable solution, I could probably make a pull request.

Am i missing something or this does not seem to work anymore in the latest versions?

Was this page helpful?
0 / 5 - 0 ratings