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

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

Closed
jantman opened this issue May 17, 2021 · 2 comments

Comments

@jantman
Copy link

jantman commented May 17, 2021

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

  • OS: Linux
  • Python version: 3.9.4
  • Sphinx version: 4.0.1
  • Sphinx extensions: sphinx.ext.autodoc

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

tk0miya commented May 19, 2021

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

@jantman
Copy link
Author

jantman commented May 19, 2021

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants