From 87426e2462a88f5a6eb960a33e5cb5f30894f295 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 30 Jul 2023 19:45:03 -0400 Subject: [PATCH] fix weird-ws empty set literals --- pyupgrade/_plugins/set_literals.py | 22 +++++----------------- tests/features/set_literals_test.py | 3 ++- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/pyupgrade/_plugins/set_literals.py b/pyupgrade/_plugins/set_literals.py index d8233ab2..631dc88a 100644 --- a/pyupgrade/_plugins/set_literals.py +++ b/pyupgrade/_plugins/set_literals.py @@ -11,9 +11,9 @@ from pyupgrade._data import register from pyupgrade._data import State from pyupgrade._data import TokenFunc +from pyupgrade._token_helpers import find_closing_bracket +from pyupgrade._token_helpers import find_op from pyupgrade._token_helpers import immediately_paren -from pyupgrade._token_helpers import is_close -from pyupgrade._token_helpers import is_open from pyupgrade._token_helpers import remove_brace from pyupgrade._token_helpers import victims @@ -21,21 +21,9 @@ def _fix_set_empty_literal(i: int, tokens: list[Token]) -> None: - # TODO: this could be implemented with a little extra logic - if not immediately_paren('set', tokens, i): - return - - j = i + 2 - depth = 1 - while depth: - if is_open(tokens[j]): - depth += 1 - elif is_close(tokens[j]): - depth -= 1 - j += 1 - - # Remove the inner tokens - del tokens[i + 2:j - 1] + i = find_op(tokens, i, '(') + j = find_closing_bracket(tokens, i) + del tokens[i + 1:j] def _fix_set_literal(i: int, tokens: list[Token], *, arg: ast.expr) -> None: diff --git a/tests/features/set_literals_test.py b/tests/features/set_literals_test.py index c2ea301f..15251812 100644 --- a/tests/features/set_literals_test.py +++ b/tests/features/set_literals_test.py @@ -13,7 +13,7 @@ 'set()', # Don't touch weird looking function calls -- use autopep8 or such # first - 'set (())', 'set ((1, 2))', + 'set ((1, 2))', ), ) def test_fix_sets_noop(s): @@ -26,6 +26,7 @@ def test_fix_sets_noop(s): # Take a set literal with an empty tuple / list and remove the arg ('set(())', 'set()'), ('set([])', 'set()'), + pytest.param('set (())', 'set ()', id='empty, weird ws'), # Remove spaces in empty set literals ('set(( ))', 'set()'), # Some "normal" test cases