Skip to content

Commit

Permalink
Do not report WPS440 when ... is the only node in the function de…
Browse files Browse the repository at this point in the history
…fition (#2898)
  • Loading branch information
sobolevn authored Mar 26, 2024
1 parent bdecb89 commit 8670471
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 8 deletions.
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
2 changes: 1 addition & 1 deletion tests/fixtures/noqa/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def some(): # noqa: WPS110
from my_module import some_import # noqa: WPS433

class Nested: # noqa: WPS431
... # noqa: WPS428, WPS604
... # noqa: WPS604

def nested(): # noqa: WPS430
anti_wps428 = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/test_checker/test_noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
'WPS425': 1,
'WPS426': 1,
'WPS427': 1,
'WPS428': 2,
'WPS428': 1,
'WPS429': 1,
'WPS430': 1,
'WPS431': 2,
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 @@ -353,15 +365,19 @@ def test_statement_with_await_effect(
])
@pytest.mark.parametrize('statement', [
'"docstring"',
'...',
])
def test_statement_with_docstring(
def test_statement_with_special_definition(
assert_errors,
parse_ast_tree,
code,
statement,
default_options,
):
"""Testing that docstring works."""
"""Testing that docstring and `...` work."""
if code == module_template and statement == '...':
pytest.skip('This should not work')

tree = parse_ast_tree(code.format(statement))

visitor = StatementsWithBodiesVisitor(default_options, tree=tree)
Expand Down Expand Up @@ -390,18 +406,22 @@ 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)
Expand Down
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

0 comments on commit 8670471

Please sign in to comment.