-
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
New rule: Prefer in-place operators #8877
Comments
Makes sense to me, I'm surprised this does not exist already. |
I just thought of a special case with string interpolation that idk if should be included: # from
base_value = f"{base_value} end of string"
# to
base_value += " end of string" Because it would break if |
We could support that conditional on the ability to determine that the type of |
We could first implement the simple cases of |
Maybe this should be a separate rule, but there might be an opportunity for some more micro optimizations here. For example, if it were faster, these: a_list += ["to concat"]
a_list += ["to concat 1", "to concat 2"]
some_set |= {"to concat"}
some_set |= {"to concat 1", "to concat 2"}
# ect. could potenchally be further rewritten as: a_list.append("to concat")
a_list.extend(("to concat 1", "to concat 2"))
some_set.add("to concat")
some_set.update(("to concat 1", "to concat 2"))
# ect. |
Increased complexity, may require type information, and I'm not asking for this for a first version / as part of my original feature request, but worth considering: dosisod/refurb#310 (comment)
Different rule imo, and may require type information. But worth investigating. Even if the performance is negligible or too variant between Python versions, it could become a readability/preference rule. |
Hi, I've made an attempt implementing this rule. Please help to have a review and let me know your thoughts. Thanks. |
…9932) ## Summary Implement new rule: Prefer augmented assignment (#8877). It checks for the assignment statement with the form of `<expr> = <expr> <binary-operator> …` with a unsafe fix to use augmented assignment instead. ## Test Plan 1. Snapshot test is included in the PR. 2. Manually test with playground.
Closed by #9932. |
…stral-sh#9932) ## Summary Implement new rule: Prefer augmented assignment (astral-sh#8877). It checks for the assignment statement with the form of `<expr> = <expr> <binary-operator> …` with a unsafe fix to use augmented assignment instead. ## Test Plan 1. Snapshot test is included in the PR. 2. Manually test with playground.
I don't think any linter currently checks for this, although I did open a feature request in those I knew of where this feature might be in scope (MartinThoma/flake8-simplify#188 and dosisod/refurb#310). Whether they accept it, (and who knows when it would be implemented), this is still a check I'd like to see in Ruff as I don't directly use those other linters and autofixing should be possible. If you think this is a viable rule.
In-place operators lead to more concise code that is still readable. I'm not sure if there's any objective drawbacks (like a common pitfall for certain types). And performance-wise my understanding is that it should be faster (due to the in-place nature) or equivalent (thanks to Python duck-typing that will fallback to
__add__
if__iadd__
is not implemented).Any of the following:
Could be re-written as:
The text was updated successfully, but these errors were encountered: