Skip to content

[flake8-simplify] Respect side effects in lambda defaults (SIM401) - #28000

Merged
AlexWaygood merged 1 commit into
mainfrom
alex/visit-lambda-defaults
Aug 24, 2026
Merged

AlexWaygood merged 1 commit into
mainfrom
alex/visit-lambda-defaults

Conversation

@AlexWaygood

Copy link
Copy Markdown
Member

Summary

Fix SIM401 false positives when a lambda fallback has side-effectful parameter defaults:

def get_callback(callbacks: dict, key, initialize):
    if key in callbacks:
        callback = callbacks[key]
    else:
        callback = lambda value=initialize(): value
    return callback

Replacing the conditional with callback = callbacks.get(key, lambda value=initialize(): value) calls initialize() even when the key exists. The conditional only calls it when the key is missing.

any_over_expr now visits lambda parameter defaults before the body. This extracts the helper change from #27634.

The existing SIM401 snapshot fixture now covers positional-only, positional-or-keyword, and keyword-only defaults, including a nested call and a literal-default case that still gets a diagnostic.

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 24, 2026 15:05
@AlexWaygood
AlexWaygood marked this pull request as draft August 24, 2026 15:07
@AlexWaygood

Copy link
Copy Markdown
Member Author

whoops, sorry, I haven't looked at this patch yet...

@astral-sh-bot

astral-sh-bot Bot commented Aug 24, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 24, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Large timing changes:

Project Old Time New Time Change
dd-trace-py 3.08s 0.72s -77%

Full report with detailed diff (timing results)

@AlexWaygood

Copy link
Copy Markdown
Member Author

Okay, I think this is good!

@AlexWaygood AlexWaygood added bug Something isn't working rule Implementing or modifying a lint rule labels Aug 24, 2026
@AlexWaygood
AlexWaygood marked this pull request as ready for review August 24, 2026 15:16
@astral-sh-bot

astral-sh-bot Bot commented Aug 24, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No mdtest? 😮 (just kidding, this is obviously fine)

@AlexWaygood
AlexWaygood merged commit 6b388d2 into main Aug 24, 2026
66 checks passed
@AlexWaygood
AlexWaygood deleted the alex/visit-lambda-defaults branch August 24, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants