Skip to content

fix: Fix attribute parser for Python 3.9 #81

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

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/pytkdocs/parsers/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def node_to_annotation(node) -> Union[str, object]:
elif isinstance(node.annotation, (ast.Constant, ast.Str)):
return node.annotation.s
elif isinstance(node.annotation, ast.Subscript):
return f"{node.annotation.value.id}[{node_to_annotation(node.annotation.slice.value)}]" # type: ignore
value_id = node.annotation.value.id # type: ignore
if hasattr(node.annotation.slice, "value"):
value = node.annotation.slice.value # type: ignore
else:
value = node.annotation.slice
return f"{value_id}[{node_to_annotation(value)}]"
else:
return inspect.Signature.empty
elif isinstance(node, ast.Subscript):
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/parsing/annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Any, Dict, List


class C:
def __init__(self):
# https://github.com/pawamoy/pytkdocs/issues/73
self.dict_annotation: Dict[str, Any] = {}

# https://github.com/pawamoy/pytkdocs/issues/75
self.list_annotation: List[str] = []
20 changes: 20 additions & 0 deletions tests/test_parsers/test_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Tests for [the `parsers.attributes` module][pytkdocs.parsers.attributes] on annotations."""


from tests.fixtures.parsing import annotations

from pytkdocs.parsers.attributes import get_instance_attributes


class TestAnnotations:
def setup(self):
"""Setup reusable attributes."""
self.attributes = get_instance_attributes(annotations.C.__init__)

def test_parse_dict_annotation(self):
assert "dict_annotation" in self.attributes
assert self.attributes["dict_annotation"]["annotation"] == "Dict[str, Any]"

def test_parse_list_annotation(self):
assert "list_annotation" in self.attributes
assert self.attributes["list_annotation"]["annotation"] == "List[str]"