Skip to content

Commit

Permalink
prevent dict/set rewrite directly in f-string placeholder
Browse files Browse the repository at this point in the history
this can change the syntax by doubling the curly braces
  • Loading branch information
asottile committed Oct 21, 2024
1 parent 4028fd1 commit b673db5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyupgrade/_plugins/dict_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def visit_Call(
parent: ast.AST,
) -> Iterable[tuple[Offset, TokenFunc]]:
if (
not isinstance(parent, ast.FormattedValue) and
isinstance(node.func, ast.Name) and
node.func.id == 'dict' and
len(node.args) == 1 and
Expand Down
1 change: 1 addition & 0 deletions pyupgrade/_plugins/set_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def visit_Call(
parent: ast.AST,
) -> Iterable[tuple[Offset, TokenFunc]]:
if (
not isinstance(parent, ast.FormattedValue) and
isinstance(node.func, ast.Name) and
node.func.id == 'set' and
len(node.args) == 1 and
Expand Down
4 changes: 4 additions & 0 deletions tests/features/dict_literals_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# Don't rewrite kwargd dicts
'dict(((a, b) for a, b in y), x=1)',
'dict(((a, b) for a, b in y), **kwargs)',
pytest.param(
'f"{dict((a, b) for a, b in y)}"',
id='directly inside f-string placeholder',
),
),
)
def test_fix_dict_noop(s):
Expand Down
8 changes: 8 additions & 0 deletions tests/features/set_literals_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
# Don't touch weird looking function calls -- use autopep8 or such
# first
'set ((1, 2))',
pytest.param(
'f"{set((1, 2))}"',
id='set directly inside f-string placeholder',
),
pytest.param(
'f"{set(x for x in y)}"',
id='set comp directly inside f-string placeholder',
),
),
)
def test_fix_sets_noop(s):
Expand Down

0 comments on commit b673db5

Please sign in to comment.