Fix issue 1901#3643
Open
f1sherFM wants to merge 3 commits into
Open
Conversation
Contributor
Author
|
@sobolevn Please if you can, review my PR, Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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 visitorMultipleVariablesInitializationVisitor.The visitor checks
ast.Assignnodes with a singleTupletarget and aTuplevalue. 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 areast.Namenodes.What is forbidden
What is allowed
Why a separate visitor class?**
The existing
WrongAssignmentVisitoralready contains 7 public/protected methods. Adding one more would triggerWPS214(too many methods). To keep the architecture clean and avoid mixing unrelated rules, I createdMultipleVariablesInitializationVisitoras a standalone class and registered it intree.py.Files changed**
wemake_python_styleguide/violations/best_practices.py— newMultipleVariablesInitializationViolation(WPS482) with full docstring (Reasoning,Solution,Example,versionadded:: 1.7.0)wemake_python_styleguide/visitors/ast/builtins.py— newMultipleVariablesInitializationVisitorwemake_python_styleguide/presets/types/tree.py— registered the new visitortests/test_visitors/test_ast/test_builtins/test_assign/test_multiple_variables_initialization.py— test cases for allowed and forbidden patternsCHANGELOG.md— added entry under## UnreleasedChecklist
CHANGELOG.mdRelated 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.