Skip to content

Fix issue 1901#3643

Open
f1sherFM wants to merge 3 commits into
wemake-services:masterfrom
f1sherFM:fix-issue-1901
Open

Fix issue 1901#3643
f1sherFM wants to merge 3 commits into
wemake-services:masterfrom
f1sherFM:fix-issue-1901

Conversation

@f1sherFM
Copy link
Copy Markdown
Contributor

I have made things!

This PR implements the rule requested in #1901: forbidding the initialization of multiple variables on a single line.

Problem

Assignments like:

ab, cd = [], []
x, y = (1, 2)

reduce readability because it is harder to see where each variable is initialized. The issue asked to detect this pattern while keeping legitimate unpacking (function returns) and variable swaps intact.

Solution

Added WPS482 (MultipleVariablesInitializationViolation) and a new AST visitor MultipleVariablesInitializationVisitor.

The visitor checks ast.Assign nodes with a single Tuple target and a Tuple value. If both sides are tuple literals, it raises a violation, except for pure name-swapping (x, y = y, x), which is explicitly allowed because all elements on the right-hand side are ast.Name nodes.

What is forbidden

ab, cd = [], []        # tuple literal unpacking
x, y = (1, 2)          # tuple literal unpacking
a, b = (1, [])         # mixed literal unpacking

What is allowed

a, b = some_tuple()    # function return unpacking
x, y = y, x            # variable swap
first, *_, second = [1, 2, 3]  # spread unpacking

Why a separate visitor class?**

The existing WrongAssignmentVisitor already contains 7 public/protected methods. Adding one more would trigger WPS214 (too many methods). To keep the architecture clean and avoid mixing unrelated rules, I created MultipleVariablesInitializationVisitor as a standalone class and registered it in tree.py.

Files changed**

  • wemake_python_styleguide/violations/best_practices.py — new MultipleVariablesInitializationViolation (WPS482) with full docstring (Reasoning, Solution, Example, versionadded:: 1.7.0)
  • wemake_python_styleguide/visitors/ast/builtins.py — new MultipleVariablesInitializationVisitor
  • wemake_python_styleguide/presets/types/tree.py — registered the new visitor
  • tests/test_visitors/test_ast/test_builtins/test_assign/test_multiple_variables_initialization.py — test cases for allowed and forbidden patterns
  • CHANGELOG.md — added entry under ## Unreleased

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 #1901

🙏 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.

@f1sherFM
Copy link
Copy Markdown
Contributor Author

@sobolevn Please if you can, review my PR, Thanks!

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.

Forbid initializing 2 or more variables on one line

1 participant