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

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

Open
trim21 opened this issue May 18, 2024 · 12 comments
Open
Labels
3.9 Affects Python 3.9 versions 3.10 Affects Python 3.10 versions extensions:autodoc type:bug

Comments

@trim21
Copy link

trim21 commented May 18, 2024

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
Copy link

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
Copy link
Author

trim21 commented May 21, 2024

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
Copy link
Member

picnixz commented May 22, 2024

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
Copy link
Author

trim21 commented May 22, 2024

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

@trim21
Copy link
Author

trim21 commented May 22, 2024

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
Copy link
Member

picnixz commented May 22, 2024

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
Copy link
Author

trim21 commented May 22, 2024

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

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

@picnixz picnixz added 3.9 Affects Python 3.9 versions 3.10 Affects Python 3.10 versions labels May 22, 2024
@trim21
Copy link
Author

trim21 commented May 22, 2024

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

@picnixz
Copy link
Member

picnixz commented May 22, 2024

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

@trim21
Copy link
Author

trim21 commented May 22, 2024

thanks for your answer.

@picnixz
Copy link
Member

picnixz commented May 22, 2024

I've found the issue:

if self.config.autodoc_class_signature == 'separated':
self.options = self.options.copy()
# show __init__() method
if self.options.special_members is None:
self.options['special-members'] = ['__new__', '__init__']
else:
self.options.special_members.append('__new__')
self.options.special_members.append('__init__')

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

for name in self.options.members:
if name in members:
selected.append(members[name])
else:
logger.warning(__('missing attribute %s in object %s'),
name, self.fullname, type='autodoc')

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
Copy link
Author

trim21 commented May 22, 2024

I currently just use workaround with adding :members:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 Affects Python 3.9 versions 3.10 Affects Python 3.10 versions extensions:autodoc type:bug
Projects
None yet
Development

No branches or pull requests

3 participants