Skip to content

Commit

Permalink
improve boolean non-keyword arguments validation (wemake-services#1114)
Browse files Browse the repository at this point in the history
Allow passing booleans as non-keyword argument when it is the only
one passed argument.
  • Loading branch information
skarzi authored and sobolevn committed Jan 18, 2020
1 parent 6024079 commit 70d4a51
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/fixtures/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def some_other_function():
except BaseException: # noqa: WPS424
anti_wps428 = 1

call_with_positional_bool(True) # noqa: WPS425
call_with_positional_bool(True, keyword=1) # noqa: WPS425


class MyInt(int): # noqa: WPS600
Expand Down
22 changes: 22 additions & 0 deletions tests/test_visitors/test_ast/test_functions/test_boolean_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

correct_argument = 'some(0, 1, keyword={0}, other={0})'
wrong_argument = 'some({0}, {0})'
single_argument = 'some({0})'


@pytest.mark.parametrize('argument', [
Expand Down Expand Up @@ -83,3 +84,24 @@ def test_wrong_boolean_argument(
BooleanPositionalArgumentViolation,
BooleanPositionalArgumentViolation,
])


@pytest.mark.parametrize('argument', [True, False])
def test_single_boolean_argument_allowed(
assert_errors,
parse_ast_tree,
argument,
default_options,
):
"""Test calls with single boolean argument.
Ensure boolean argument can be passed as positional argument when
it is the only call's argument.
"""
tree = parse_ast_tree(single_argument.format(argument))

visitor = WrongFunctionCallVisitor(default_options, tree=tree)
visitor.run()

assert_errors(visitor, [])
7 changes: 5 additions & 2 deletions wemake_python_styleguide/violations/best_practices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,8 @@ class BooleanPositionalArgumentViolation(ASTViolation):
It is almost impossible to tell, what does this parameter means.
And you almost always have to look up the implementation to tell
what is going on.
The only exception from this rule is passing booleans as
non-keyword argument when it is the only passed argument.
Solution:
Pass booleans as keywords only.
Expand All @@ -1033,10 +1035,11 @@ class BooleanPositionalArgumentViolation(ASTViolation):
Example::
# Correct:
UsersRepository.update(cache=True)
UserRepository.update(True)
UsersRepository.add(user, cache=True)
# Wrong:
UsersRepository.update(True)
UsersRepository.add(user, True)
.. versionadded:: 0.6.0
Expand Down
3 changes: 3 additions & 0 deletions wemake_python_styleguide/visitors/ast/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def _check_wrong_function_called(self, node: ast.Call) -> None:
)

def _check_boolean_arguments(self, node: ast.Call) -> None:
# Calls with single boolean argument are allowed
if len(node.args) == 1 and not node.keywords:
return
for arg in node.args:
if isinstance(arg, ast.NameConstant):
# We do not check for `None` values here:
Expand Down

0 comments on commit 70d4a51

Please sign in to comment.