sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

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

Open trim21 opened this issue 1 year ago • 12 comments

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

trim21 avatar May 18 '24 04:05 trim21

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

electric-coder avatar May 21 '24 11:05 electric-coder

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"""

trim21 avatar May 21 '24 11:05 trim21

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.

picnixz avatar May 22 '24 08:05 picnixz

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

trim21 avatar May 22 '24 08:05 trim21

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

trim21 avatar May 22 '24 08:05 trim21

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...

picnixz avatar May 22 '24 08:05 picnixz

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

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

trim21 avatar May 22 '24 08:05 trim21

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

trim21 avatar May 22 '24 08:05 trim21

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:

  • Pick everything.
  • Filter when needed.
  • Ask extensions if this should really be filtered.
  • Do your things.

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

picnixz avatar May 22 '24 08:05 picnixz

thanks for your answer.

trim21 avatar May 22 '24 08:05 trim21

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.

picnixz avatar May 22 '24 08:05 picnixz

I currently just use workaround with adding :members:

trim21 avatar May 22 '24 08:05 trim21

this is not a issue anymore

trim21 avatar Mar 07 '25 00:03 trim21

https://github.com/sphinx-doc/sphinx/blob/8962398b761c3d85a7c74b6f789b3ffb127bde0c/pyproject.toml#L20

trim21 avatar Mar 07 '25 00:03 trim21