0

I'd like to do very basic documentation of my python3 code with sphinx.

An example piece of my code looks like this:

class MyClass:
    """
    Class description
    """
    def function1(x, y, z):
        """
        function description
        """
        pass

and the corresponding part in my index.txt file for sphinx like this:

.. automodule:: code.module_name
.. autoclass:: MyClass
   :members:

As a result the documentation is otherwise what I'd expect but the function shows as:

function1(y,z)
function description

instead of the desired

function1(x,y,z)
function description

So to put it in short: How to make sphinx not ignore the first argument of python class methods?

I've searched a good while for an answer to this problem and found no answers that would've helped me overcome it. I appreciate any help.

Henri
  • 3
  • 3
  • Just looking at this, if your `function1` is not decorated with `staticmethod`, it could be just implicitly skipping `self`. Try putting that decorator above your function to see if that is indeed the behavior – C.Nivs Jul 06 '18 at 13:46
  • That works! Thanks so much! It must have been skipping the first argument assuming its just `self`. Repost that as an answer if you like and I'll mark it best answer. – Henri Jul 06 '18 at 14:21
  • Is it supposed to be a class method or a static method? See https://stackoverflow.com/q/12179271/407651 – mzjn Jul 06 '18 at 16:06
  • posted the answer. As a quick note, I would default to using method 1, since it will keep your method's access to `self` variables and won't change the behavior, whereas `staticmethod` strips that away – C.Nivs Jul 06 '18 at 16:38

1 Answers1

0

Your function function1 is not a staticmethod, therefore the first arg is self:

class myClass:

    def function1(x, y, z):
        # x in this case is an alias for self

To get x to show up, either add x as another arg or have the method be static

1:

class myClass:
    def function1(self, x, y, z):
        # Now x is an explicit var

2:

class myClass

    @staticmethod
    def function1(x,y,z):
        # Now your class does not have access to self, but x is an explicit arg

It will skip self since it is implicitly provided to the function, you should not have to specify it on the method call

C.Nivs
  • 12,353
  • 2
  • 19
  • 44