Skip to content
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

Fix spaces around keyvalues in dict comprehensions #133

Merged
merged 1 commit into from
Jan 29, 2025
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
11 changes: 6 additions & 5 deletions rewrite/rewrite/python/format/spaces_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,6 @@ def _process_kv_pair(c: JRightPadded[KeyValue], idx: int, arg_size) -> JRightPad
else:
c = space_after_right_padded(c, self._style.other.before_comma)
c = space_before_right_padded_element(c, self._style.other.after_comma)
# Handle space before and after colon in key-value pair e.g. {1 : 2} <-> {1: 2}
kv = cast(KeyValue, c.element)
kv = kv.with_value(space_before(kv.value, self._style.other.after_colon))
kv = kv.padding.with_key(space_after_right_padded(kv.padding.key, self._style.other.before_colon))
c = c.with_element(kv)

# Handle trailing comma space for last argument e.g. {1: 2, 3: 4, } <-> {1: 2, 3: 4,}
if idx == arg_size - 1:
Expand Down Expand Up @@ -517,6 +512,12 @@ def visit_comprehension_expression(self, comprehension_expression: Comprehension

return ce

def visit_key_value(self, key_value: KeyValue, p: P) -> J:
# Handle space before and after colon in key-value pair e.g. {1 : 2} <-> {1: 2}
kv = cast(KeyValue, super().visit_key_value(key_value, p))
kv = kv.with_value(space_before(kv.value, self._style.other.after_colon))
return kv.padding.with_key(space_after_right_padded(kv.padding.key, self._style.other.before_colon))

def visit_comprehension_condition(self, condition: ComprehensionExpression.Condition, p: P) -> J:
cond = cast(ComprehensionExpression.Condition, super().visit_comprehension_condition(condition, p))
# Set single space before and after comprehension 'if' keyword.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_spaces_with_dict_comprehension(within_braces):
"""\
a = {i: i*2 for i in range(0, 10)}
a = {i: i for i in [1, 2, 3]}
a = {k: v*2 for k,v in { "a": 2, "b": 4}.items( ) }
a = {k: v*2 for k,v in { "a": 2, "b": 4}.items( ) }
""",
"""\
a = {i: i * 2 for i in range(0, 10)}
Expand All @@ -116,3 +116,34 @@ def test_spaces_with_dict_comprehension(within_braces):
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)


def test_spaces_with_dict_comprehension():
style = IntelliJ.spaces()
rewrite_run(
# language=python
python(
"""\
a = {k: 1 }
a = {k: 1 ** 2}
def dict_comprehension_test(self):
keys = ['a', 'b', 'c']
values = [1, 2, 3]
return {k : v ** 2
for k, v in zip(keys, values)
}
""",
"""\
a = {k: 1}
a = {k: 1 ** 2}
def dict_comprehension_test(self):
keys = ['a', 'b', 'c']
values = [1, 2, 3]
return {k: v ** 2
for k, v in zip(keys, values)
}
"""
),
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)
Loading