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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not report WPS440 when ... is the only node in the function defition #2898

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ Semantic versioning in our case means:

### Bugfixes

- Fixes `TooDeepNestingViolation` not to trigger on `...`
- Fixes `TooDeepNestingViolation` not to trigger
on `...` in functions and classes
- Fixes `StatementHasNoEffectViolation` not to trigger
on `...` in functions and classes, when it is the only node


## 0.19.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class MyClass:
def function_with_really_long_name(self): ...
"""

top_level_class_ellipsis = """
class MyClassWithReallyLongName: ...
"""


@pytest.mark.parametrize('code', [
nested_if,
Expand All @@ -121,6 +125,7 @@ def function_with_really_long_name(self): ...
),
top_level_function_ellipsis,
top_level_method_ellipsis,
top_level_class_ellipsis,
])
def test_nested_offset(
assert_errors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,25 @@ def function():
{0}
"""

function_extra_template = """
def function():
x = 1
...
"""

# Classes:

class_template = """
class Test:
{0}
"""

class_extra_template = """
class Test:
x = 1
{0}
"""

# Async:

async_function_template = """
Expand Down Expand Up @@ -390,21 +402,49 @@ def test_statement_with_docstring(
async_with_template,
async_for_template,
async_for_else_template,

function_extra_template,
class_extra_template,
])
@pytest.mark.parametrize('statement', [
'"docstring"',
'...',
])
def test_statement_with_useless_docstring(
def test_statement_useless_special_statements(
assert_errors,
parse_ast_tree,
code,
statement,
default_options,
):
"""Testing that docstring works."""
"""Testing that docstring and `...` work."""
tree = parse_ast_tree(code.format(statement))

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

assert_errors(visitor, [StatementHasNoEffectViolation])


@pytest.mark.parametrize('code', [
function_template,
class_template,
async_function_template,
])
@pytest.mark.parametrize('statement', [
'...'
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
])
def test_statement_ellipsis_has_effect_on_definition(
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
assert_errors,
parse_ast_tree,
code,
statement,
default_options,
):
"""Testing that `...` works."""
tree = parse_ast_tree(code.format(statement))

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

assert_errors(visitor, [])
4 changes: 4 additions & 0 deletions wemake_python_styleguide/violations/best_practices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,10 @@ def some_function():

.. versionadded:: 0.5.0
.. versionchanged:: 0.11.0
.. versionchanged:: 0.19.1
Do not report ``...`` when used in a
function or class body as a single node.

"""

error_template = 'Found statement that has no effect'
Expand Down
2 changes: 2 additions & 0 deletions wemake_python_styleguide/violations/complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ class TooDeepNestingViolation(ASTViolation):

.. versionadded:: 0.1.0
.. versionchanged:: 0.5.0
.. versionchanged:: 0.19.1
Do not report ``...`` when used in a function or class body.

"""

Expand Down
2 changes: 1 addition & 1 deletion wemake_python_styleguide/visitors/ast/complexity/offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def visit_line_expression(self, node: ast.AST) -> None:

def _check_offset(self, node: ast.AST) -> None:
is_function_ellipsis = (
isinstance(get_parent(node), FunctionNodes) and
isinstance(get_parent(node), (*FunctionNodes, ast.ClassDef)) and
isinstance(node, ast.Expr) and
isinstance(node.value, ast.Constant) and
node.value.value is Ellipsis
Expand Down
11 changes: 11 additions & 0 deletions wemake_python_styleguide/visitors/ast/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def _check_useless_node(
def _check_expression(
self,
node: ast.Expr,
*,
is_first: bool = False,
) -> None:
if isinstance(node.value, self._have_effect):
Expand All @@ -229,6 +230,16 @@ def _check_expression(
if isinstance(nodes.get_parent(node), self._have_doc_strings):
return

parent = nodes.get_parent(node)
is_only_ellipsis_node = (
isinstance(node.value, ast.Constant) and
node.value.value is Ellipsis and
isinstance(parent, (*FunctionNodes, ast.ClassDef)) and
len(parent.body) == 1
)
if is_only_ellipsis_node:
return

self.add_violation(StatementHasNoEffectViolation(node))

def _check_self_misrefactored_assignment(
Expand Down
Loading