Skip to content

Fix WPS430 false positive on whitelisted deep nested functions#3591

Merged
sobolevn merged 3 commits intowemake-services:masterfrom
albertedwardson:issue-3589
Feb 14, 2026
Merged

Fix WPS430 false positive on whitelisted deep nested functions#3591
sobolevn merged 3 commits intowemake-services:masterfrom
albertedwardson:issue-3589

Conversation

@albertedwardson
Copy link
Copy Markdown
Contributor

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Closes #3589

Actually, there was already a test case for this /wemake-python-styleguide/tests/test_visitors/test_ast/test_complexity/test_nested/test_nested_functions.py, but it was wrong and assumed that deep nested whitelisted functions must be reported.

@albertedwardson
Copy link
Copy Markdown
Contributor Author

albertedwardson commented Jan 31, 2026

@sobolevn hi! I opened a new PR with correct branch name and other edits

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e1b5f7b) to head (84f61a3).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #3591   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          369       369           
  Lines        12297     12308   +11     
  Branches       849       850    +1     
=========================================
+ Hits         12297     12308   +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's only allow def in a single function-level if. Nothing more.

Please, also write unit-tests :)

Comment thread tests/fixtures/noqa/noqa.py Outdated
def nested(): # noqa: WPS430
...

def factory(): # ok
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to put this into noqa.py. Here we test that # noqa comments work correctly ;)

'WPS428': 0, # disabled since 1.0.0
'WPS429': 1,
'WPS430': 1,
'WPS430': 3,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert these changes.

Suggested change
'WPS430': 3,
'WPS430': 1,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added minimal reproducible example as part of integration tests (if I didn't misunderstood what CONTRIBUTING.md states)

@albertedwardson
Copy link
Copy Markdown
Contributor Author

Let's only allow def in a single function-level if. Nothing more.

Ok, sure, didn't catch that from the issue itself, thought that it is ok to define whitelisted functions regardless of nesting and what causes that nesting

Please, also write unit-tests :)

Don't tests/test_visitors/test_ast/test_complexity/test_nested/test_nested_functions.py already cover that case? Or do you mean I should extend it with a case where whitelisted name in, e.g. try/except or for block, that will be reported as violation, and separate them from single if block, where it is allowed? If so, should I also explicitly document this behavior in documentation and add an example with inspect.iscoroutinefunction?

@sobolevn
Copy link
Copy Markdown
Member

sobolevn commented Feb 2, 2026

Don't tests/test_visitors/test_ast/test_complexity/test_nested/test_nested_functions.py already cover that case?

Nope, we modified code and none tests failed :)
We need to test this corner case, mention it in the rule's doc (just a short sentence).


assert_errors(visitor, [NestedFunctionViolation])
assert_error_text(visitor, whitelist_name)
assert_errors(visitor, [])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sobolevn

Nope, we modified code and none tests failed :)

Not because I actually changed assertion in that case in this PR? As I meantioned before, this test assumed that all deep nested function definitions with whitelisted names should be reported as violation, regardless what causes that indentation

Did you mean that we just need exclude

into a separate test case that asserts there is no violation with if and if/else blocks, while keeping other deep nesting cases reported as violations?

Sorry for asking so many questions and not understanding what needed to be done from the first try :(

small edit

added integration test case and updated CHANGELOG.md
self.add_violation(NestedFunctionViolation(node, text=node.name))
is_direct = isinstance(parent, FunctionNodes)

is_single_if = isinstance(parent, ast.If) and isinstance(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_parent(parent) is get_context(node) that's the correct check :)

get_parent(parent), FunctionNodes
)

if node.name not in NESTED_FUNCTIONS_WHITELIST or not (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use early returns here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this?

        if node.name not in NESTED_FUNCTIONS_WHITELIST:
            self.add_violation(
                NestedFunctionViolation(node, text=node.name),
            )
            return

        if not is_direct and not is_single_if:
            self.add_violation(
                NestedFunctionViolation(node, text=node.name),
            )

Copy link
Copy Markdown
Member

@sobolevn sobolevn Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if node.name in NESTED_FUNCTIONS_WHITELIST and (is_direct or is_single_if): return :)

Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks!

@sobolevn sobolevn merged commit 8720afc into wemake-services:master Feb 14, 2026
9 checks passed
@albertedwardson albertedwardson deleted the issue-3589 branch February 28, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive for WPS430 with whitelisted name

2 participants