Skip to content

Commit

Permalink
fix: Fix unparsing f-strings
Browse files Browse the repository at this point in the history
Issue #80: #80
  • Loading branch information
pawamoy committed Jun 3, 2022
1 parent bfad1cc commit 9ca74bd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
10 changes: 6 additions & 4 deletions src/griffe/agents/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,10 @@ def _get_constant_value(node: NodeConstant) -> str:
return repr(node.value)


def _get_constant_value_no_repr(node: NodeConstant) -> str:
return node.value
def _get_constant_value_no_string_repr(node: NodeConstant) -> str:
if isinstance(node.value, str):
return node.value
return repr(node.value)


def _get_dict_value(node: NodeDict) -> str:
Expand Down Expand Up @@ -940,8 +942,8 @@ def _get_isnot_value(node: NodeIsNot) -> str:


def _get_joinedstr_value(node: NodeJoinedStr) -> str:
_node_value_map[NodeConstant] = _get_constant_value_no_repr
result = repr("".join(_get_value(value) for value in node.values))
_node_value_map[NodeConstant] = _get_constant_value_no_string_repr
result = "f" + repr("".join(_get_value(value) for value in node.values))
_node_value_map[NodeConstant] = _get_constant_value
return result

Expand Down
59 changes: 58 additions & 1 deletion tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from griffe.agents.nodes import relative_to_absolute
from griffe.agents.nodes import get_value, relative_to_absolute
from tests.helpers import module_vtree, temporary_visited_module


Expand Down Expand Up @@ -98,3 +98,60 @@ def test_default_value_from_nodes(default):
params = module.members["f"].parameters
assert len(params) == 1
assert params[0].default == default


@pytest.mark.parametrize(
"expression",
[
# operations
"b + c",
"b - c",
"b * c",
"b / c",
"b // c",
"b ** c",
"b ^ c",
"b & c",
"b | c",
"b @ c",
"b % c",
"b >> c",
"b << c",
# unary operations
"+b",
"-b",
"~b",
# comparisons
"b == c",
"b >= c",
"b > c",
"b <= c",
"b < c",
"b != c",
# boolean logic
"b and c",
"b or c",
"not b",
# identify
"b is c",
"b is not c",
# membership
"b in c",
"b not in c",
# calls
"call()",
"call(something)",
"call(something=something)",
# strings
"f'{round(key, 2)}'",
],
)
def test_building_value_from_nodes(expression):
"""Test building value from AST nodes.
Parameters:
expression: An expression (parametrized).
"""
node = compile(expression, mode="exec", filename="<>", flags=PyCF_ONLY_AST).body[0].value
value = get_value(node)
assert value == expression
52 changes: 0 additions & 52 deletions tests/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,6 @@
# ...


# TODO: move this in test_nodes once hypothesmith is ready
@pytest.mark.parametrize(
"expression",
[
# operations
"b + c",
"b - c",
"b * c",
"b / c",
"b // c",
"b ** c",
"b ^ c",
"b & c",
"b | c",
"b @ c",
"b % c",
"b >> c",
"b << c",
# unary operations
"+b",
"-b",
"~b",
# comparisons
"b == c",
"b >= c",
"b > c",
"b <= c",
"b < c",
"b != c",
# boolean logic
"b and c",
"b or c",
"not b",
# identify
"b is c",
"b is not c",
# membership
"b in c",
"b not in c",
],
)
def test_building_value_from_nodes(expression):
"""Test building value from AST nodes.
Parameters:
expression: An expression (parametrized).
"""
with temporary_visited_module(f"a = {expression}") as module:
assert "a" in module.members
assert module["a"].value == expression


def test_not_defined_at_runtime():
"""Assert that objects not defined at runtime are not added to wildcards expansions."""
with temporary_pypackage("package", ["module_a.py", "module_b.py", "module_c.py"]) as tmp_package:
Expand Down

0 comments on commit 9ca74bd

Please sign in to comment.