-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Change adds conditional visitor that verifies that if statement isn't using True, False, None as it's case * Change updates the error message for using NamedConstants * Change adds ChangeLog feature line * Change removes unnecessary line in test * Change implements PR feedback. This adds extra checks for Num, List, Set or Str types used in either an If or IfExp node in the AST. * Change implements PR feedback and merges two functions and adds Dict type to not supported in conditional
- Loading branch information
1 parent
4f6d7e2
commit 5b5ab6e
Showing
6 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
79 changes: 79 additions & 0 deletions
79
tests/test_visitors/test_ast/test_comparisons/test_conditionals.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.violations.consistency import ( | ||
WrongConditionalViolation, | ||
) | ||
from wemake_python_styleguide.visitors.ast.comparisons import ( | ||
WrongConditionalVisitor, | ||
) | ||
|
||
create_variable = """ | ||
variable = 1 | ||
{0} | ||
""" | ||
|
||
if_statement = 'if {0}: ...' | ||
ternary = 'ternary = 0 if {0} else 1' | ||
if_statement_in_comprehension = '[x for x in [1, 2, 3] if {0}]' | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
if_statement, | ||
ternary, | ||
if_statement_in_comprehension, | ||
]) | ||
@pytest.mark.parametrize('comparators', [ | ||
'variable < 3', | ||
'variable', | ||
'variable is True', | ||
'variable is False', | ||
'[1,2,3].size > 3', | ||
'variable is None', | ||
'variable is int or not None', | ||
]) | ||
def test_valid_conditional( | ||
assert_errors, | ||
parse_ast_tree, | ||
code, | ||
comparators, | ||
default_options, | ||
): | ||
"""Testing that conditionals work well.""" | ||
tree = parse_ast_tree(create_variable.format(code.format(comparators))) | ||
|
||
visitor = WrongConditionalVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
if_statement, | ||
ternary, | ||
]) | ||
@pytest.mark.parametrize('comparators', [ | ||
'True', | ||
'False', | ||
'None', | ||
'{variable}', | ||
'[variable]', | ||
'4', | ||
'"test"', | ||
'{test : "1"}', | ||
]) | ||
def test_redundant( | ||
assert_errors, | ||
parse_ast_tree, | ||
code, | ||
comparators, | ||
default_options, | ||
): | ||
"""Testing that violations are when using invalid conditional.""" | ||
tree = parse_ast_tree(create_variable.format(code.format(comparators))) | ||
|
||
visitor = WrongConditionalVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [WrongConditionalViolation]) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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