sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

autodoc loses first parameter of staticmethod when global is pulled by autofunction

Open dimaqq opened this issue 1 year ago • 6 comments

Describe the bug

Similar to #4769 and #9342, in this specific circumstance, the first argument of staticmethod is lost:

How to Reproduce

ops/__init__.py

class Example:
    @staticmethod
    def __call__(foo: int, bar: str) -> float:
        return 42.

example = Example()
"""
Some description
"""

docs/index.rst

ops module
==========

.. automodule:: ops

.. autofunction:: ops.example

Generates this doc:

ops.example(bar: str) → float Some description

Environment Information

Sphinx 8.0.2

Sphinx extensions

No response

Additional context

No response

dimaqq avatar Aug 29 '24 01:08 dimaqq

Screenshot 2024-08-29 at 10 02 25

dimaqq avatar Aug 29 '24 01:08 dimaqq

This doesn't happen for me using .. automethod:: with a regular staticmethod that isn't a dunder. What might be happening is that autodoc truncates the 1st argument thinking it's self (it also does this for or cls).

@dimaqq it would be important to include what autodoc configurations you set in conf.py. In case you use napoleon its configurations should also be included in the bug report.

electric-coder avatar Aug 29 '24 06:08 electric-coder

napoleon: 'sphinx.ext.napoleon' is included, no config set. autodoc: included with following config:

  autodoc_typehints = 'signature'
  autoclass_content = 'class'
  autodoc_member_order = 'alphabetical'

  autodoc_default_options = {
      'members': None,  # None here means "yes"
      'undoc-members': None,
      'show-inheritance': None,
  }

dimaqq avatar Aug 29 '24 08:08 dimaqq

I'm not sure this is a bug @dimaqq - it seems that object.__call__ is documented as a dunder method available to override method calls to a class instance (specifically an instance, meaning that the first argument will be populated with a reference to the instance, typically named self by convention).

It's OK to adjust your code to use a different pattern based on your design choices, but I don't think that this implies a bug when other components do not recognize that non-standard pattern.

jayaddison avatar Sep 03 '24 12:09 jayaddison

Note the @staticmethod decorator that effectively strips self.

dimaqq avatar Sep 04 '24 07:09 dimaqq

Ok; true - I did notice that, but I failed to notice that the example does use an instance of Example -- and calling that is valid:

Python 3.12.5 (main, Aug 22 2024, 13:11:09) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Example:
...     @staticmethod
...     def __call__(foo: int, bar: str) -> float:
...         return 42.
... 
>>> example = Example()
>>> """
... Some description
... """
'\nSome description\n'
>>> example(1, 2)
42.0

jayaddison avatar Sep 04 '24 11:09 jayaddison