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
Forbid assigning to a slice #1629 #1661
Forbid assigning to a slice #1629 #1661
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1661 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 107 107
Lines 5992 6005 +13
Branches 1343 1346 +3
=========================================
+ Hits 5992 6005 +13
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! 👍
tests/test_visitors/test_ast/test_subscripts/test_slice_assignment.py
Outdated
Show resolved
Hide resolved
Sorry to be late for the party, but why? Slice assignment is the only way for inplace array multiple replacement when you need that. Any other alternatives on the table? Shouldn't any forbid rule follow with howto one? And no, explicit index assignment isn't an alternative since you replace one shot operation which could be optimized by underlying structure (like numpy.array) via O(N) one which wouldn't much be optimal in most of cases. |
It can be switched off for |
The sense is quite the same: provide the way for multiple sequential items replacement in a single shot. Dummy example how it works:
So at least this rule forces to do not an optimal operations. Any other reasons to not do slice assignments? |
The quite common and useful example which violates this rule is inplace list replacement via |
@kxepal thanks a lot! This is a valuable addition. The main idea of this rule is that >>> l = [0, 1, 2, 3, 4]
>>> l[0:2]
[0, 1]
>>> l[0:2] = ['a', 'b', 'c']
>>> l
['a', 'b', 'c', 2, 3, 4] And this is really confusing. How can I tell the final size of the list after I think that this code is ugly. And I would be really unpleased to something like it in our production code. # We need this operation to be fast, because this is a hot path,
# so we use a hack to insert a part of another list in one bunch.
new_list_part = some()
assert len(new_list_part) == 500 - 100
my_list[100:500] = some_call() # noqa: WPS-whatever This code looks reasonable to me. |
@sudo-k-runner can you please
Thanks! |
@sobolevn Sure, apologies for the delay, got side tracked. Will updated the PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, @sudo-k-runner
Thanks!
Forbid assigning to a slice #1629
Checklist
CHANGELOG.md
Related issues
Closes #1629
🙏 Please, if you or your company is finding wemake-python-styleguide valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/wemake-python-styleguide. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.