-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
PIE810 gives wrong suggestions #8906
Labels
bug
Something isn't working
Comments
I guess you can do like this? if msg.startswith(x + (y,)):
sys.exit(1) |
I can, but the point is ruff suggests invalid code that will throw |
Makes sense, thanks! |
I guess one solution would be to avoid raising the violation if we're sure that it's a collection type like the mentioned code: x = ["a", "b"]
y = "c"
msg.startswith(x) or msg.startswith(y) But, if there are more than 1 x = ["a", "b"]
y = "c"
z = "d"
msg.startswith(x) or msg.startswith(y) or msg.startswith(z)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Raise here |
Closed by #9661. |
charliermarsh
pushed a commit
that referenced
this issue
Jan 28, 2024
…h` (#9661) ## Summary This review contains a fix for [PIE810](https://docs.astral.sh/ruff/rules/multiple-starts-ends-with/) (multiple-starts-ends-with) The problem is that ruff suggests combining multiple startswith/endswith calls into a single call even though there might be a call with tuple of strs. This leads to calling startswith/endswith with tuple of tuple of strs which is incorrect and violates startswith/endswith conctract and results in runtime failure. However the following will be valid and fixed correctly => ```python x = ("hello", "world") y = "h" z = "w" msg = "hello world" if msg.startswith(x) or msg.startswith(y) or msg.startswith(z) : sys.exit(1) ``` ``` ruff --fix --select PIE810 --unsafe-fixes ``` => ```python if msg.startswith(x) or msg.startswith((y,z)): sys.exit(1) ``` See: #8906 ## Test Plan ```bash cargo test ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Given the code:
Ruff suggests (
ruff check x.py
):The problem is
x
is a tuple and the expression cannot be combined tomsg.startswith((x, y))
Ruff settings
Ruff version
ruff 0.1.6 (f460f9c 2023-11-17)
The text was updated successfully, but these errors were encountered: