sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.46k stars 2.1k forks source link

`autodoc_class_signature = "separated"` cause a warning for enum with no `__init__` #12384

Open trim21 opened 4 months ago

trim21 commented 4 months ago

Describe the bug

autodoc_class_signature = "separated" option will cause class without __init__ raise a warning and can't build with -W

image

How to Reproduce

use this repo: https://github.com/trim21/sphinx-autodoc_class_signature-bug

git cl https://github.com/trim21/sphinx-autodoc_class_signature-bug sphinx-autodoc_class_signature-bug
cd sphinx-autodoc_class_signature-bug
pip install -e .
sphinx-build -W docs dist

Environment Information

Platform:              win32; (Windows-10-10.0.19045-SP0)
Python version:        3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)])
Python implementation: CPython
Sphinx version:        7.3.2
Docutils version:      0.21.2
Jinja2 version:        3.1.4
Pygments version:      2.18.0

Sphinx extensions

extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.viewcode",
    "sphinx.ext.napoleon",
]

Additional context

No response

electric-coder commented 4 months ago

Your repo doesn't have any Python class and no Enum as said in the title. This bug report is missing a minimal reproducer.

trim21 commented 4 months ago

Your repo doesn't have any Python class and no Enum as said in the title. This bug report is missing a minimal reproducer.

Which repo are you talking about?

https://github.com/trim21/sphinx-autodoc_class_signature-bug/blob/ee160815ed1c6217e1e4b9751c7d362453ee4c8a/lib/__init__.py#L4

lib/__init__.py

import enum

class Status(str, enum.Enum):
    """enum for status"""
picnixz commented 4 months ago

I can confirm the bug but I'm not sure if it's a bug because of the .. autoclass directive or not... Without the directive and by using :members: for .. automodule, the enumeration is being shown normally but using autoclass on the enumeration, it appears that there is an issue...

I can try to fix it but I don't like specializing the behaviours for enums in general because it's an API that is constantly changing... I'll try to see what I can do.

trim21 commented 4 months ago

interesting, now I can't re-produce this...

trim21 commented 4 months ago

OK, I'm guessing this only happed on old version of python. I can reproduce this with py 3.9 but not 3.12

picnixz commented 4 months ago

Oh yes, I was using the same version as you (3.10) and could confirm the bug. That's why I said I hate working with the enum API since it changes every version...

trim21 commented 4 months ago

I more generic way could be make :exclude-members: __init__ work? I guess?

I don't know how sphinx works internally...

trim21 commented 4 months ago

Normal class with __new__ only doesn't have this problem...

picnixz commented 4 months ago

The exclude-members wouldn't work because we are always picking all members before excluding them later. The rationale is because the filtering logic can be changed by the user after. The flow is more or less:

However, the warnings happens before the exclusion is being made (it's trying to pick up every possible members).

trim21 commented 4 months ago

thanks for your answer.

picnixz commented 4 months ago

I've found the issue:

https://github.com/sphinx-doc/sphinx/blob/9cc0ea14088f6796237872e8d7443ff30993f762/sphinx/ext/autodoc/__init__.py#L1490-L1498

It appears we are forcibly assuming that __new__ and __init__ exist, which may not necessarily be the case and you get the warning here:

https://github.com/sphinx-doc/sphinx/blob/9cc0ea14088f6796237872e8d7443ff30993f762/sphinx/ext/autodoc/__init__.py#L1755-L1760

So, I should add some if-logic for this warning if the __new__ and __init__ are missing but I won't have much time today.

trim21 commented 4 months ago

I currently just use workaround with adding :members: