Skip to content

Commit

Permalink
Fix WPS220 for top-level Ellipsis (#2897)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Mar 26, 2024
1 parent 3e0565b commit bdecb89
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Semantic versioning in our case means:
change the client facing API, change code conventions significantly, etc.


## 0.19.1

### Bugfixes

- Fixes `TooDeepNestingViolation` not to trigger on `...`


## 0.19.0

This minor version will be the last release with all the `flake8` plugins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
nested_if = """
def container():
if True:
x = 1
... # this needs to be an ellipsis for the test
"""

nested_if2 = """
Expand Down Expand Up @@ -85,6 +85,17 @@ async def update_control():
'point': 1})
"""

# Only ellipsis in the top level function definition is fine:

top_level_function_ellipsis = """
def function_with_really_long_name(): ...
"""

top_level_method_ellipsis = """
class MyClass:
def function_with_really_long_name(self): ...
"""


@pytest.mark.parametrize('code', [
nested_if,
Expand All @@ -108,6 +119,8 @@ async def update_control():
reason='Pattern matching was added in 3.10',
),
),
top_level_function_ellipsis,
top_level_method_ellipsis,
])
def test_nested_offset(
assert_errors,
Expand Down
11 changes: 11 additions & 0 deletions wemake_python_styleguide/visitors/ast/complexity/offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from typing_extensions import final

from wemake_python_styleguide.compat.aliases import FunctionNodes
from wemake_python_styleguide.logic.nodes import get_parent
from wemake_python_styleguide.violations.complexity import (
TooDeepNestingViolation,
)
Expand Down Expand Up @@ -55,6 +57,15 @@ def visit_line_expression(self, node: ast.AST) -> None:
self.generic_visit(node)

def _check_offset(self, node: ast.AST) -> None:
is_function_ellipsis = (
isinstance(get_parent(node), FunctionNodes) and
isinstance(node, ast.Expr) and
isinstance(node.value, ast.Constant) and
node.value.value is Ellipsis
)
if is_function_ellipsis:
return

offset = getattr(node, 'col_offset', 0)
baseline = self._max_offset_blocks * 4
if offset > baseline:
Expand Down

0 comments on commit bdecb89

Please sign in to comment.