sphinx
sphinx copied to clipboard
autodoc loses first parameter of staticmethod when global is pulled by autofunction
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
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.
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,
}
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.
Note the @staticmethod decorator that effectively strips self.
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