2

I have a class Foo that supports subscripting, so if foo is an instance of Foo, foo[12] will find the 12th one. Foo is defined something like this:

class Foo:
    def __getitem__(self, i):
        """Get the ith element of self."""
        ....

The sphinx rst file looks something like this:

.. autoclass:: Foo
    :members: __getitem__, ...

When sphinx builds the documentation, Foo's subscript-implementing __getitem__ member function is documented as:

__getitem__(i)

Get the ith element of the Foo.

But that function signature will be confusing to a reader of the documentation who does not know about Python dunder functions, and who does not know that subscripting brackets are defined with __getitem__. It would be better to have __getitem__ documented like this:

foo[i]

Get the ith element of the Foo foo.

Is there a way to override the member function signature with foo[i], which does not look like a signature at all?

David Bridgeland
  • 525
  • 1
  • 9
  • 16
  • I'm not sure you're doing this right. Have you tried adding `__getitem__` with `:special-members:` as described [here](http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html)? – Nearoo Nov 19 '18 at 13:55
  • 1
    The behavior of `:special-members:` is curious. When `:members:` is used without a explicit list of members, `:special-members:` is needed to specify dunders. But when `:members:` has an explicit list (e.g. `:members: foo, bar`), `:special-members:` has no effect at all. It seems that an explicit list of members requires any dunders to be included in the list. (In my case that is not unreasonable, as I have only a few members that I want documented, and only two dunders.) And in any case, `:special-members:` seems only about whether a dunder is documented, not how the signature looks. – David Bridgeland Nov 20 '18 at 11:41
  • 1
    The buggy behaviour of `special-members` was fixed in Sphinx 1.8. See https://github.com/sphinx-doc/sphinx/issues/2401. – mzjn Nov 20 '18 at 19:21
  • 1
    Related proposal: https://github.com/sphinx-doc/sphinx/issues/5702 – mzjn Dec 07 '18 at 15:43

0 Answers0