sphinx-doc / sphinx

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

Autodoc fails on enums containing special characters #10322

Open mara004 opened 2 years ago

mara004 commented 2 years ago

Describe the bug

Consider the following enum:

Colorspace = Enum("Colorspace", "RGB RGBA L LA 1 CMYK CMYK;I P PA")

Autodoc'ing this enum will yield the following error:

WARNING: invalid signature for autoattribute ('Colorspace.CMYK;I')
WARNING: don't know which module to import for autodocumenting 'Colorspace.CMYK;I' (try placing a "module" or "currentmodule" directive in the document, or giving an explicit module name)

It looks like autodoc may be unable to handle enum attributes containing the ; character. (In Python, the attribute can be accessed by indexing: Colorspace['CMYK;I'])

How to Reproduce

(see the description)

Expected behavior

It should be possible to document enum attributes containing a semicolon character (;). Respectively any character that is not compatible with dotted access but valid in other notations (e.g. getitem indexing).

Your project

private, sorry

Screenshots

No response

OS

Linux Ubuntu 20.04

Python version

3.8.10

Sphinx version

4.5.0

Sphinx extensions

sphinx.ext.autodoc

Extra tools

No response

Additional context

No response

Gjgarr commented 2 years ago

I believe my error is relevant to this issue; I ran into the same error message when autodoc'ing some TypedDicts with spaces and hyphens.

A = TypedDict(
    "A",
    {
        "Game Mode": int,
    },
)
WARNING: invalid signature for autoattribute ('[...]::A.Game Mode')
WARNING: don't know which module to import for autodocumenting '[...]::A.Game Mode' (try placing a "module" or "currentmodule" directive in the document, or giving an explicit module name)

Cb-3 is also invalid.

benhoyt commented 1 year ago

Is there a way to work around this error (before being able to upgrade to a fixed version)? I'm having the same issue with a TypedDict that has hyphens in some of the keys, like invalid signature for autoattribute ('ops.pebble::ServiceDict.backoff-delay').

kamakazikamikaze commented 5 months ago

This is definitely an issue related to characters that normally aren't valid in attribute names. However I think that the situation provided by @Gjgarr is tangentially related but important for whatever fix is proposed (e.g. #11937). The TypedDict is a perfect example as it is valid syntax using the alternate format as outlined in PEP 589 and would be accessed using the index notation (ex somedict["spaced key"]) rather than __getattr__. I'm not certain that filtering out these attributes is ideal if they can be accessed normally by their intended way.