Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix weird-ws empty set literals #866

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix weird-ws empty set literals
  • Loading branch information
asottile committed Jul 30, 2023
commit 87426e2462a88f5a6eb960a33e5cb5f30894f295
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