From b673db5ac0ecda899a305d1e8c1969115eede004 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 21 Oct 2024 19:06:58 -0400 Subject: [PATCH] prevent dict/set rewrite directly in f-string placeholder this can change the syntax by doubling the curly braces --- pyupgrade/_plugins/dict_literals.py | 1 + pyupgrade/_plugins/set_literals.py | 1 + tests/features/dict_literals_test.py | 4 ++++ tests/features/set_literals_test.py | 8 ++++++++ 4 files changed, 14 insertions(+) diff --git a/pyupgrade/_plugins/dict_literals.py b/pyupgrade/_plugins/dict_literals.py index ca4a2860..1577dc8f 100644 --- a/pyupgrade/_plugins/dict_literals.py +++ b/pyupgrade/_plugins/dict_literals.py @@ -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 diff --git a/pyupgrade/_plugins/set_literals.py b/pyupgrade/_plugins/set_literals.py index e3369ae4..0b3b21fa 100644 --- a/pyupgrade/_plugins/set_literals.py +++ b/pyupgrade/_plugins/set_literals.py @@ -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 diff --git a/tests/features/dict_literals_test.py b/tests/features/dict_literals_test.py index e1b33bf1..880a01be 100644 --- a/tests/features/dict_literals_test.py +++ b/tests/features/dict_literals_test.py @@ -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): diff --git a/tests/features/set_literals_test.py b/tests/features/set_literals_test.py index 15251812..64827ba9 100644 --- a/tests/features/set_literals_test.py +++ b/tests/features/set_literals_test.py @@ -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):