From 1169c51bd6ae04f491fa5e50cae93d99e8ce920d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Thu, 26 May 2022 12:09:48 +0200 Subject: [PATCH] fix: Support USub and UAdd nodes in annotations Issue #71: https://github.com/mkdocstrings/griffe/issues/71 --- src/griffe/agents/nodes.py | 10 ++++++++++ tests/test_nodes.py | 1 + 2 files changed, 11 insertions(+) diff --git a/src/griffe/agents/nodes.py b/src/griffe/agents/nodes.py index 542d4673..ae1d6a3b 100644 --- a/src/griffe/agents/nodes.py +++ b/src/griffe/agents/nodes.py @@ -688,6 +688,14 @@ def _get_unaryop_annotation(node: NodeUnaryOp, parent: Module | Class) -> Expres return Expression(_get_annotation(node.op, parent), _get_annotation(node.operand, parent)) +def _get_uadd_annotation(node: NodeUAdd, parent: Module | Class) -> str: + return "+" + + +def _get_usub_annotation(node: NodeUSub, parent: Module | Class) -> str: + return "-" + + _node_annotation_map: dict[Type, Callable[[Any, Module | Class], str | Name | Expression]] = { NodeAttribute: _get_attribute_annotation, NodeBinOp: _get_binop_annotation, @@ -704,6 +712,8 @@ def _get_unaryop_annotation(node: NodeUnaryOp, parent: Module | Class) -> Expres NodeSubscript: _get_subscript_annotation, NodeTuple: _get_tuple_annotation, NodeUnaryOp: _get_unaryop_annotation, + NodeUAdd: _get_uadd_annotation, + NodeUSub: _get_usub_annotation, } # TODO: remove once Python 3.8 support is dropped diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 6c18d694..f8a5ca97 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -58,6 +58,7 @@ def test_relative_to_absolute_imports(code, path, is_package, expected): "A | B", "A[[B, C], D]", "A(b=c, d=1)", + "A[-1, +2.3]", ], ) def test_building_annotations_from_nodes(expression):