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

SIM103 suggestion negates the original conditional expression #9618

Closed
chunyang opened this issue Jan 23, 2024 · 5 comments · Fixed by #9619
Closed

SIM103 suggestion negates the original conditional expression #9618

chunyang opened this issue Jan 23, 2024 · 5 comments · Fixed by #9619
Assignees
Labels
bug Something isn't working

Comments

@chunyang
Copy link

Consider the following Python code:

def opposite(a: bool) -> bool:
    if a:
        return False
    else:
        return True

When I run ruff on the code with the SIM checks selected, the suggested replacement returns the opposite of the expected value.

% ruff check --isolated --select SIM --show-source sim103.py 
sim103.py:2:5: SIM103 Return the condition `a` directly
  |
1 |   def opposite(a: bool) -> bool:
2 |       if a:
  |  _____^
3 | |         return False
4 | |     else:
5 | |         return True
  | |___________________^ SIM103
  |
  = help: Replace with `return a`

If I switch False and True in the code, the suggested replacement remains the same, but is then correct.

% ruff --version
ruff 0.1.14

Playground link: https://play.ruff.rs/7d91158b-0d65-4cc1-95f3-435136f0d1e9

@charliermarsh charliermarsh added the bug Something isn't working label Jan 23, 2024
@chunyang
Copy link
Author

Seems related to #2037 but IIUC that only disabled the autofix, but didn't fix the suggestion.

@charliermarsh
Copy link
Member

Ah thanks -- that does look like a bug.

@charliermarsh
Copy link
Member

I'll re-enable the fix, and insert the correct code and suggestion.

@charliermarsh
Copy link
Member

Fixed in the next release, thanks!

@chunyang
Copy link
Author

Thanks for the quick fix!

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