Skip to content

Commit

Permalink
fix weird-ws empty set literals
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jul 30, 2023
1 parent 80a6553 commit 87426e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
22 changes: 5 additions & 17 deletions pyupgrade/_plugins/set_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,19 @@
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

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


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:
Expand Down
3 changes: 2 additions & 1 deletion tests/features/set_literals_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit 87426e2

Please sign in to comment.