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

PLR1730 min / max reversed detected / auto-fix #10889

Closed
ilius opened this issue Apr 11, 2024 · 2 comments · Fixed by #10890
Closed

PLR1730 min / max reversed detected / auto-fix #10889

ilius opened this issue Apr 11, 2024 · 2 comments · Fixed by #10890
Assignees
Labels
bug Something isn't working

Comments

@ilius
Copy link

ilius commented Apr 11, 2024

Sample code:

def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
	if x < min_x:
		x = min_x
	if x > max_x:
		x = max_x
	return x

Command: ruff check --preview --select PLR test.py

Result:

test.py:3:2: PLR1730 [*] Replace `if` statement with `x = min(x, min_x)`
  |
2 |   def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
3 |       if x < min_x:
  |  _____^
4 | |         x = min_x
  | |_________________^ PLR1730
5 |       if x > max_x:
6 |           x = max_x
  |
  = help: Replace with `x = min(x, min_x)`

test.py:5:2: PLR1730 [*] Replace `if` statement with `x = max(x, max_x)`
  |
3 |       if x < min_x:
4 |           x = min_x
5 |       if x > max_x:
  |  _____^
6 | |         x = max_x
  | |_________________^ PLR1730
7 |       return x
  |
  = help: Replace with `x = max(x, max_x)`

Found 2 errors.
[*] 2 fixable with the `--fix` option.

The min / max are reversed.
The first one must be a max, second one min.
Passing --fix will break the code:

def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
	x = min(x, min_x)
	x = max(x, max_x)
	return x

The correct fix:

def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
	x = max(x, min_x)
	x = min(x, max_x)
	return x
@charliermarsh charliermarsh added the bug Something isn't working label Apr 11, 2024
@charliermarsh charliermarsh self-assigned this Apr 11, 2024
@charliermarsh
Copy link
Member

Thanks, this is my bad.

@charliermarsh
Copy link
Member

Even looking at this error report I thought x = min(x, min_x) was correct initially. The reversing logic is breaking my brain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants