Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] singledispatch classmethods are not detected by MethodDocumenter #11278

Closed
picnixz opened this issue Apr 1, 2023 · 0 comments · Fixed by #11284
Closed

[BUG] singledispatch classmethods are not detected by MethodDocumenter #11278

picnixz opened this issue Apr 1, 2023 · 0 comments · Fixed by #11284

Comments

@picnixz
Copy link
Member

picnixz commented Apr 1, 2023

Describe the bug

When using functools.singledispatchmethod together with classmethod, no :classmethod: directive option is added by sphinx.ext.autodoc.MethodDocumenter.

Using the example below, the reST directive result is

.. py:class:: MyClass()
   :module: class

   My class.


   .. py:method:: MyClass.dispatch(x)
      :module: class

      A singledispatch class method.


   .. py:method:: MyClass.method()
      :module: class
      :classmethod:

      A class method.

The issue lies in the following line:

if inspect.isclassmethod(obj):
self.add_line(' :classmethod:', sourcename)

The inspection of a classmethod when the latter is wrapped in a singledispatchmethod does not detect it as a classmethod. In particular, I would suggest replacing this test by the following line:

if inspect.isclassmethod(obj) or (
   inspect.is_singledispatch_method(obj)
   and inspect.isclassmethod(obj.func)
):
   ...

How to Reproduce

class.py

from functools import singledispatchmethod

class MyClass:
    """My class."""

    @singledispatchmethod  
    @classmethod
    def dispatch(cls, x):
      """A singledispatch class method."""
      return 0

    @classmethod
    def method(cls):
        """A class method."""
        pass

index.rst

Foo
===

.. autoclass:: class.MyClass
   :members:

conf.py

import os
import sys

sys.path.insert(0, os.path.abspath('.'))

project = 'Foo'
copyright = '2023, picnixz'
author = 'picnixz'

extensions = ['sphinx.ext.autodoc']

templates_path = ['_templates']

master_doc = 'index'

include_patterns = ['index.rst', 'class.py']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

html_theme = 'alabaster'
html_static_path = ['_static']

Environment Information

Platform:              linux; (Linux-5.3.18-lp152.106-default-x86_64-with-glibc2.26)
Python version:        3.10.3 (main, Jan 31 2023, 10:47:25) [GCC 7.5.0])
Python implementation: CPython
Sphinx version:        6.1.3
Docutils version:      0.18.1
Jinja2 version:        3.1.2
Pygments version:      2.14.0

Sphinx extensions

['sphinx.ext.autodoc']

Additional context

The same issue seems to happen with staticmethod: #10529 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants