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

FURB110 may break code #11398

Closed
martinlehoux opened this issue May 13, 2024 · 6 comments · Fixed by #11464
Closed

FURB110 may break code #11398

martinlehoux opened this issue May 13, 2024 · 6 comments · Fixed by #11464
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@martinlehoux
Copy link
Contributor

  • Keywords: FURB110
  • Command: ruff --preview --select FURB110 --fix
  • Ruff version: 0.4.3

FURB110 autofix (with preview) changed code

place_id = (
    location_place_id
    if location_place_id
    else City.objects.get(pk=city_id).gmaps_place_id
    if city_id
    else None
)    

into

place_id = location_place_id or City.objects.get(pk=city_id).gmaps_place_id if city_id else None

With inputs location_place_id = "abc" and city_id = None, the code previously returned "abc" but now returns None

@VascoSch92
Copy link
Contributor

Could you provide a minimal code snippet to try?

@blueraft
Copy link
Contributor

I'm able to reproduce it using the following example.

❯ bat test.py
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: test.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ location_place_id = "abc"
   2   │ city_id = None
   3   │ place_id = (
   4   │     location_place_id
   5   │     if location_place_id
   6   │     else "ghi"
   7   │     if city_id
   8   │     else None
   9   │ )
  10   │
  11   │ print(place_id)
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
❯ python test.py
abc

After running ruff --fix.

❯ ruff check test.py --preview --select FURB110 --fix
Found 1 error (1 fixed, 0 remaining).
❯ bat test.py
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: test.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ location_place_id = "abc"
   2   │ city_id = None
   3   │ place_id = (
   4   │     location_place_id or "ghi"
   5   │     if city_id
   6   │     else None
   7   │ )
   8   │
   9   │ print(place_id)
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
❯ python test.py
None

The correct fix here should wrap the second or statement in parentheses.

❯ bat test.py
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: test.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ location_place_id = "abc"
   2   │ city_id = None
   3   │ place_id = location_place_id or ("ghi" if city_id else None)
   4   │ print(place_id)
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
❯ python test.py
abc

@martinlehoux
Copy link
Contributor Author

Thanks @blueraft , and yes that is exactly the fix I did ;)

@charliermarsh charliermarsh added bug Something isn't working fixes Related to suggested fixes for violations labels May 13, 2024
@charliermarsh
Copy link
Member

Probably a precedence issue.

@dhruvmanila
Copy link
Member

I think the edit content should be generated using the Generator to account for precedence as it currently uses raw string:

Edit::range_replacement(
format!(
"{} or {}",
checker.locator().slice(left),
checker.locator().slice(right),
),
if_expr.range(),
),

@charliermarsh
Copy link
Member

Raw strings are better right now IMO since they preserve more of the verbatim content. I think I'd rather augment the fix to parenthesize if needed.

@charliermarsh charliermarsh added the help wanted Contributions especially welcome label May 13, 2024
@charliermarsh charliermarsh self-assigned this May 18, 2024
@charliermarsh charliermarsh removed the help wanted Contributions especially welcome label May 18, 2024
charliermarsh added a commit that referenced this issue May 19, 2024
## Summary

Ensures that we parenthesize expressions (if necessary) to preserve
operator precedence in `FURB110`.

Closes #11398.
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 a pull request may close this issue.

5 participants