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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce violation WPS358 #1259

Merged
merged 5 commits into from Oct 20, 2020
Merged

Conversation

skarzi
Copy link
Collaborator

@skarzi skarzi commented Mar 16, 2020

Add violation WPS358 forbidding unpacking iterables to lists.

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Closes #1256

馃檹 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.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Very quick fix! 馃殼

Can you please add more tests and a CHANGELOG entry?

@@ -12,9 +15,13 @@
tuple_assignment1 = 'first, {0} = (1, 2)'
tuple_assignment1 = '{0}, second = (1, 2)'

list_assignment = '[first, second] = (1, 2)'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about cases like these?

>>> [a, b], c = ((1, 2), 3)
>>> a, [b, c] = (1, (2, 3))

@@ -274,6 +276,12 @@ def _check_unpacking_targets(
best_practices.WrongUnpackingViolation(node),
)

def _check_unpacking_target_type(self, node: ast.Assign) -> None:
if isinstance(node.targets[0], ast.List):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we need to check all elements inside targets because of:

>>> [a, b], c = ((1, 2), 3)
>>> a, [b, c] = (1, (2, 3))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true. Should we stop detection on first spotted list or raise violations for all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unpacking to nested list actually raise WPS414 - WrongUnpackingViolation.
Should the second violation be raised?

I think we should do one of the following then:

  • update my PR to raise WPS414 and update WPS414 violations docstrings (I dislike this, because know WPS414 can be raised in 2 totally different cases)
  • change name of WPS414 to something less confusing and more compliant with its docstring title (Forbids to have tuple unpacking with side-effects.) and refactor check raising this violation, because unpacking to list doesn't have any side effects if it's not dynamic e.g a, b[0] = (1, 3) but b[0] is not parsed as ast.List by ast

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these two violations should be independent. And both of them can be raised at the same time.

@coveralls
Copy link

coveralls commented Mar 16, 2020

Pull Request Test Coverage Report for Build 2820

  • 15 of 15 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 2819: 0.0%
Covered Lines: 5308
Relevant Lines: 5308

馃挍 - Coveralls

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we can do this in Python! Insane!

spread_assignment1 = '{0}, *second = [1, 2, 3]'
spread_assignment2 = 'first, *{0} = [1, 2, 3]'

spread_assignment_in_list = '[first, *rest] = [1, 2, 3]'

nested_spread_assignment_in_list = 'first, [second, *rest] = [1, (2, 3, 4)]'

multiple_level_nested_spread_assign_in_list = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more case to test: z = (a, [b, c]) = (1, (2, 3))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added support for such cases and much more in new commits.
I am happy that unpacking iterables is not supported when using walrus operator 馃槃

Add violation WPS358 forbidding unpacking iterables to lists.
Check for WPS358 in all assignments targets and in following
nodes/expressions:

+ for loops
+ list comprehensions
+ generator expressions
+ set comprehensions
+ dict comprehensions
+ with statements
Rename WPS358 violation from ``IterableUnpackingToListViolation``
to ``UnpackingIterableToListViolation``.
@codecov
Copy link

codecov bot commented Oct 20, 2020

Codecov Report

Merging #1259 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1259   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          105       105           
  Lines         5485      5499   +14     
  Branches      1209      1213    +4     
=========================================
+ Hits          5485      5499   +14     
Impacted Files Coverage 螖
wemake_python_styleguide/violations/consistency.py 100.00% <100.00%> (酶)
wemake_python_styleguide/visitors/ast/builtins.py 100.00% <100.00%> (酶)

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update 3b79762...53099e3. Read the comment docs.

@sobolevn sobolevn merged commit 77940ce into wemake-services:master Oct 20, 2020
@skarzi skarzi deleted the issue-1256 branch October 30, 2020 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

List should be treated as invalid unpacking targets
3 participants