Skip to content

Commit be5d190

Browse files
authored
Merge pull request #866 from asottile/weird-ws
fix weird-ws empty set literals
2 parents 80a6553 + 87426e2 commit be5d190

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

pyupgrade/_plugins/set_literals.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,19 @@
1111
from pyupgrade._data import register
1212
from pyupgrade._data import State
1313
from pyupgrade._data import TokenFunc
14+
from pyupgrade._token_helpers import find_closing_bracket
15+
from pyupgrade._token_helpers import find_op
1416
from pyupgrade._token_helpers import immediately_paren
15-
from pyupgrade._token_helpers import is_close
16-
from pyupgrade._token_helpers import is_open
1717
from pyupgrade._token_helpers import remove_brace
1818
from pyupgrade._token_helpers import victims
1919

2020
SET_TRANSFORM = (ast.List, ast.ListComp, ast.GeneratorExp, ast.Tuple)
2121

2222

2323
def _fix_set_empty_literal(i: int, tokens: list[Token]) -> None:
24-
# TODO: this could be implemented with a little extra logic
25-
if not immediately_paren('set', tokens, i):
26-
return
27-
28-
j = i + 2
29-
depth = 1
30-
while depth:
31-
if is_open(tokens[j]):
32-
depth += 1
33-
elif is_close(tokens[j]):
34-
depth -= 1
35-
j += 1
36-
37-
# Remove the inner tokens
38-
del tokens[i + 2:j - 1]
24+
i = find_op(tokens, i, '(')
25+
j = find_closing_bracket(tokens, i)
26+
del tokens[i + 1:j]
3927

4028

4129
def _fix_set_literal(i: int, tokens: list[Token], *, arg: ast.expr) -> None:

tests/features/set_literals_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'set()',
1414
# Don't touch weird looking function calls -- use autopep8 or such
1515
# first
16-
'set (())', 'set ((1, 2))',
16+
'set ((1, 2))',
1717
),
1818
)
1919
def test_fix_sets_noop(s):
@@ -26,6 +26,7 @@ def test_fix_sets_noop(s):
2626
# Take a set literal with an empty tuple / list and remove the arg
2727
('set(())', 'set()'),
2828
('set([])', 'set()'),
29+
pytest.param('set (())', 'set ()', id='empty, weird ws'),
2930
# Remove spaces in empty set literals
3031
('set(( ))', 'set()'),
3132
# Some "normal" test cases

0 commit comments

Comments
 (0)