Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes #20562

@github-actions
Copy link
Contributor

github-actions bot commented Sep 25, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you! The tests look good, but what do you think about moving the new code to visit_expr to mirror the function version a bit more closely?

@danparizher
Copy link
Contributor Author

Yeah, that approach makes sense to me. Thanks for the review!

@danparizher danparizher requested a review from ntBre September 26, 2025 01:54
@ntBre ntBre added the bug Something isn't working label Oct 21, 2025
@ntBre
Copy link
Contributor

ntBre commented Oct 21, 2025

Hmm, it looks like there are some new false positives in the ecosystem report. I was hoping it was something simple with the scope popping, which is why I pushed a commit, but that didn't end up helping. We're flagging a case like this with F401:

import uuid


class C:
    uuid = lambda: str(uuid.uuid4())

and I don't fully understand why. We'll need to figure it out before we can merge this, though. Would you mind taking a look?

Refactors the handling of implicit __class__ scopes in lambdas to only add the DunderClassCell scope when a lambda is nested within a class, not just any lambda. Adds a test case to ensure lambdas referencing module-level variables are not incorrectly flagged as F821.
@danparizher
Copy link
Contributor Author

The issue was that the DunderClassCell scope was being added during lambda definition processing instead of during lambda body analysis, which caused false positives when lambdas within classes referenced module-level variables like uuid.

I fixed it by moving the scope logic from visit_expr to visit_deferred_lambdas (similar to how function definitions work) and changing the scope detection to check the entire scope hierarchy rather than just the current scope, ensuring __class__ is accessible in lambdas within classes while preventing false positives for other variables.

@ntBre
Copy link
Contributor

ntBre commented Oct 24, 2025

Ah okay, I guess my earlier suggestion about moving it to visit_expr was wrong then. Thank you for debugging and sorry for the bad suggestion!

@ntBre ntBre merged commit 3e8685d into astral-sh:main Oct 24, 2025
37 checks passed
@danparizher danparizher deleted the fix-20562 branch October 24, 2025 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

F821 false positive for __class__ in lambda expression in class definition

2 participants