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

[pyupgrade] Unwrap unary expressions correctly (UP018) #15919

Merged
merged 4 commits into from
Feb 14, 2025

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #15859.

The rule now adds parentheses if the original call wraps an unary expression and is:

  • The left-hand side of a binary expression where the operator is **.
  • The caller of a call expression.
  • The subscripted of a subscript expression.
  • The object of an attribute access.

The fix will also be marked as unsafe if there are any comments in its range.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Feb 4, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dscorbett
Copy link

Another case to consider is await int(-1).

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Thanks for looking into how to make this work with OperatorPrecedence

@MichaReiser MichaReiser added bug Something isn't working fixes Related to suggested fixes for violations labels Feb 14, 2025
@MichaReiser MichaReiser merged commit 3d0a58e into astral-sh:main Feb 14, 2025
21 checks passed
@InSyncWithFoo InSyncWithFoo deleted the UP018 branch February 14, 2025 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UP018 fix ignores operator precedence for numbers with unary operators
3 participants