Skip to content

Commit 3a6dc36

Browse files
fix: Add missing space in dictionary comprehension
A space was missing between the value and `for`. For example: ```python import tempfile import sys import griffe with tempfile.TemporaryDirectory() as tmp: sys.path.insert(0, tmp) with open(f"{tmp}/test_for_griffe.py", "x") as f: print("A = {key: value for key, value in []}", file=f) module = griffe.load("test_for_griffe") print(module.get_member("A").value) ``` ...displayed: ```python {key: valuefor (key, value) in []} ``` Add the missing space. Issue-mkdocstrings-python-311: mkdocstrings/python#311 PR-420: #420
1 parent 26e75df commit 3a6dc36

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/griffe/_internal/expressions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ def iterate(self, *, flat: bool = True) -> Iterator[str | Expr]:
473473
yield from _yield(self.key, flat=flat)
474474
yield ": "
475475
yield from _yield(self.value, flat=flat)
476+
yield " "
476477
yield from _join(self.generators, " ", flat=flat)
477478
yield "}"
478479

tests/test_expressions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,14 @@ def func[Z](arg1: T, arg2: Y): pass
231231

232232
assert module["C.func"].parameters["arg1"].annotation.canonical_path == "module.C[T]"
233233
assert module["C.func"].parameters["arg2"].annotation.canonical_path == "Y"
234+
235+
236+
@pytest.mark.xfail(reason="Tuple in comprehension target is not yet implicit")
237+
def test_render_dict_comprehension() -> None:
238+
"""Assert dict comprehensions are rendered correctly."""
239+
with temporary_visited_module(
240+
"""
241+
d = {k: v for k, v in items if k}
242+
""",
243+
) as module:
244+
assert str(module["d"].value) == "{k: v for k, v in items if k}"

0 commit comments

Comments
 (0)