Skip to content

Commit

Permalink
feat: Improve support for call nodes in annotations
Browse files Browse the repository at this point in the history
Issue #66: #66
  • Loading branch information
pawamoy committed Apr 22, 2022
1 parent f579431 commit 45e5bf5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/griffe/agents/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ def _get_bitor_annotation(node: NodeBitOr, parent: Module | Class) -> str:


def _get_call_annotation(node: NodeCall, parent: Module | Class) -> Expression:
posargs = Expression(*[_get_annotation(arg, parent) for arg in node.args])
kwargs = Expression(*[_get_annotation(kwarg, parent) for kwarg in node.keywords])
posargs = Expression(*_join([_get_annotation(arg, parent) for arg in node.args], ", "))
kwargs = Expression(*_join([_get_annotation(kwarg, parent) for kwarg in node.keywords], ", "))
args: Expression | str
if posargs and kwargs:
args = Expression(posargs, ", ", kwargs)
Expand Down Expand Up @@ -662,6 +662,10 @@ def _get_invert_annotation(node: NodeInvert, parent: Module | Class) -> str:
return "~"


def _get_keyword_annotation(node: NodeKeyword, parent: Module | Class) -> Expression:
return Expression(f"{node.arg}=", _get_annotation(node.value, parent))


def _get_list_annotation(node: NodeList, parent: Module | Class) -> Expression:
return Expression("[", *_join([_get_annotation(el, parent) for el in node.elts], ", "), "]")

Expand Down Expand Up @@ -694,6 +698,7 @@ def _get_unaryop_annotation(node: NodeUnaryOp, parent: Module | Class) -> Expres
NodeEllipsis: _get_ellipsis_annotation,
NodeIfExp: _get_ifexp_annotation,
NodeInvert: _get_invert_annotation,
NodeKeyword: _get_keyword_annotation,
NodeList: _get_list_annotation,
NodeName: _get_name_annotation,
NodeSubscript: _get_subscript_annotation,
Expand All @@ -712,13 +717,21 @@ def _get_index_annotation(node: NodeIndex, parent: Module | Class) -> str | Name
# TODO: remove once Python 3.7 support is dropped
if sys.version_info < (3, 8):

def _get_bytes_annotation(node: NodeBytes, parent: Module | Class) -> str:
return repr(node.s)

def _get_nameconstant_annotation(node: NodeNameConstant, parent: Module | Class) -> str | Name | Expression:
return repr(node.value)

def _get_num_annotation(node: NodeNum, parent: Module | Class) -> str:
return repr(node.n)

def _get_str_annotation(node: NodeStr, parent: Module | Class) -> str:
return node.s

_node_annotation_map[NodeBytes] = _get_bytes_annotation
_node_annotation_map[NodeNameConstant] = _get_nameconstant_annotation
_node_annotation_map[NodeNum] = _get_num_annotation
_node_annotation_map[NodeStr] = _get_str_annotation


Expand Down Expand Up @@ -772,8 +785,6 @@ def get_docstring(

# ==========================================================
# values


def _get_add_value(node: NodeAdd) -> str:
return "+"

Expand Down
1 change: 1 addition & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_relative_to_absolute_imports(code, path, is_package, expected):
"~A",
"A | B",
"A[[B, C], D]",
"A(b=c, d=1)",
],
)
def test_building_annotations_from_nodes(expression):
Expand Down

0 comments on commit 45e5bf5

Please sign in to comment.