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

improve boolean non-keyword arguments validation #1114

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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