sphinx-doc / sphinx

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

typing.restify fails on Literal - AttributeError: 'str' object has no attribute '__module__' #9245

Closed jantman closed 3 years ago

jantman commented 3 years ago

Describe the bug

I'm using sphinx-apidoc and autodoc to document a code base that makes extensive use of type hints, including typing.Literal which was introduced in Python 3.8. Unfortunately, this is being passed through sphinx.util.typing.restify which is throwing an AttributeError

To Reproduce

I've been able to reproduce this succinctly directly through the Python console:

$ .tox/docs/bin/python
Python 3.9.4 (default, Apr 20 2021, 15:51:38)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sphinx.util.typing import restify
>>> from typing import Literal
>>> FooBar = Literal['foo', 'bar']
>>> restify(FooBar)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jantman/manheim/git/MAN-Tooling/PanoParse/.tox/docs/lib/python3.9/site-packages/sphinx/util/typing.py", line 126, in restify
    return _restify_py37(cls)
  File "/home/jantman/manheim/git/MAN-Tooling/PanoParse/.tox/docs/lib/python3.9/site-packages/sphinx/util/typing.py", line 165, in _restify_py37
    text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__)
  File "/home/jantman/manheim/git/MAN-Tooling/PanoParse/.tox/docs/lib/python3.9/site-packages/sphinx/util/typing.py", line 165, in <genexpr>
    text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__)
  File "/home/jantman/manheim/git/MAN-Tooling/PanoParse/.tox/docs/lib/python3.9/site-packages/sphinx/util/typing.py", line 122, in restify
    elif cls.__module__ in ('__builtin__', 'builtins'):
AttributeError: 'str' object has no attribute '__module__'
>>> 

Alternatively, I've been able to reproduce this issue in multiple unrelated projects of mine, by creating a new module within my project called type_aliases.py with the following content:

from typing import Literal

#: Type alias for valid Locations
Location = Literal['shared', 'private', 'Predefined', '!!FAKE']

#: Type alias for valid AddressObject _type
AddressObjectType = Literal['ip-netmask', 'ip-range', 'fqdn']

#: Type alias for valid service protocols
ValidProtos = Literal['tcp', 'udp', 'any', 'icmp', 'scps']

#: Valid firewall rule types
RuleType = Literal['universal', 'interzone']

#: Valid firewall rule actions
RuleAction = Literal['Allow', 'Deny']

Expected behavior

The Literal would be properly documented, and at least would not raise an exception.

Your project

Unfortunately, proprietary code. If needed, I can push the problematic module to a branch in one of my unrelated open source projects.

Environment info

Additional context

sphinx-apidoc is run with -e -f -M

conf.py includes:

autoclass_content = 'both'

autodoc_default_options = {
    'members': None,
    'undoc-members': None,
    'private-members': None,
    'show-inheritance': None,
    'inherited-members': None
}
tk0miya commented 3 years ago

I think this is the same as #9195 and already fixed by #9196. Thank you for reporting.

jantman commented 3 years ago

I'm sorry I missed that in my search :( But thank you so much!!