mkdocstrings / pytkdocs

Load Python objects documentation.
https://mkdocstrings.github.io/pytkdocs
ISC License
50 stars 32 forks source link

[BUG] Dict[] type annotations make pytkdocs explode #73

Closed nigelm closed 3 years ago

nigelm commented 3 years ago

Describe the bug If there is a type annotation of the form Dict[x, y] then the parser throws an exception.

To Reproduce

from typing import Any
from typing import Dict
...
my_thing: Dict[str, Any] = {}

Expected behavior For it not to throw an exception like this:

INFO    -  Building documentation...
INFO    -  Cleaning site directory
ERROR   -  mkdocstrings.handlers.python: Collection failed: 'Tuple' object has no attribute 'value'
Traceback (most recent call last):
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/cli.py", line 205, in main
    output = json.dumps(process_json(line))
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/cli.py", line 114, in process_json
    return process_config(json.loads(json_input))
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/cli.py", line 91, in process_config
    obj = loader.get_object_documentation(path, members)
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/loader.py", line 342, in get_object_documentation
    root_object = self.get_module_documentation(leaf, members)
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/loader.py", line 409, in get_module_documentation
    root_object.add_child(self.get_class_documentation(child_node))
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/loader.py", line 444, in get_class_documentation
    attributes_data.update(get_instance_attributes(class_.__init__))
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/parsers/attributes.py", line 155, in get_instance_attributes
    annotation = node_to_annotation(assignment)
  File "/Users/nigel/Library/Caches/pypoetry/virtualenvs/python-broadworks-tools-zQ4enKcE-py3.9/src/pytkdocs/src/pytkdocs/parsers/attributes.py", line 19, in node_to_annotation
    return f"{node.annotation.value.id}[{node_to_annotation(node.annotation.slice.value)}]"  # type: ignore
AttributeError: 'Tuple' object has no attribute 'value'

ERROR   -  mkdocstrings.extension: Could not collect 'python_broadworks_tools.enterprise'
ERROR   -  Error reading page 'api/enterprise.md': 'Tuple' object has no attribute 'value'

System (please complete the following information):

pawamoy commented 3 years ago

Hello, thank you for the report!

I'll try to replicate first, then look for a fix (PRs welcome!) :slightly_smiling_face:

phil65 commented 3 years ago

This only seems to happen on Python 3.9.x. (The bug report mentions the wrong version)

pawamoy commented 3 years ago

Thanks @phil65 for the note. It must then be related to #75: ast has changed in Python 3.9, and it breaks the current pytkdocs parser. These bugs are preventing the use of Python 3.9 so I'll mark this (in my head) as pretty urgent.

pawamoy commented 3 years ago

I've pushed a fix, could you try again with the latest commit @nigelm, @phil65?

pawamoy commented 3 years ago

Should be fixed in 0.10.1.