Skip to content

Fix __class__ binding in class-body lambdas - #11541

Merged
Rich Chiodo (rchiodo) merged 1 commit into
microsoft:mainfrom
nnqbao:fix/issue-11511-class-cell-lambda
Jul 27, 2026
Merged

Rich Chiodo (rchiodo) merged 1 commit into
microsoft:mainfrom
nnqbao:fix/issue-11511-class-cell-lambda

Conversation

@nnqbao

Copy link
Copy Markdown
Contributor

Summary

  • bind the implicit PEP 3135 __class__ symbol in lambda scopes enclosed by a class
  • allow lambda nodes to back intrinsic declarations
  • add a regression sample for class A: get_class = lambda: __class__

Testing

  • targeted regression test
  • complete typeEvaluator1.test.ts (159 passed)
  • complete pyright-internal suite (62 suites, 2541 tests passed)
  • build, ESLint, Prettier, typecheck

Fixes #11511

@nnqbao

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@rchiodo

Rich Chiodo (rchiodo) commented Jul 13, 2026 •

Copy link
Copy Markdown
Collaborator

🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR.



class A:
get_class = lambda: __class__

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

📍 packages/pyright-internal/src/tests/samples/lambda16.py:7
The sample only asserts the happy path. Since the only real risk of this change is over-binding __class__, add a negative case — a module-level or free-function g = lambda: __class__ wired as a second sample expecting validateResults(..., 1) — so a future regression that over-broadens the binding is actually caught. The Skeptic verified this case is correct today (CPython raises NameError, and getEnclosingClass returns undefined), but it isn't locked by a test.

[verified]

@rchiodo

Copy link
Copy Markdown
Collaborator

Looks good — correct, minimal fix for the PEP 3135 __class__ binding in class-body lambdas. Approving. One optional suggestion: add a negative sample to guard against future over-binding.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved via Review Center.

@nnqbao
Bao Nguyen (Bob) (nnqbao) force-pushed the fix/issue-11511-class-cell-lambda branch from 11e80dd to 9ba9719 Compare July 19, 2026 06:36
@nnqbao

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion. I rebased onto the current main and added a negative regression case for a module-level lambda referencing __class__, asserting that it is still reported as undefined. I also updated the expected diagnostic count and ran the focused test:

  • npm run test:norebuild -- --runInBand typeEvaluator1.test.ts -t Lambda16 (passed)
  • git diff --check (passed)

The updated commit has been pushed to the PR branch.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved via Review Center.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sympy (https://github.com/sympy/sympy)
-   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:314:30 - error: Cannot access attribute "as_independent" for class "Basic"
-     Attribute "as_independent" is unknown (reportAttributeAccessIssue)
-   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:380:15 - error: Operator "*" not supported for "None" (reportOptionalOperand)
-   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:13 - error: No overloads for "__init__" match the provided arguments (reportCallIssue)
-   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:18 - error: Argument of type "list[Iterator[Unknown]]" cannot be assigned to parameter "iterable" of type "Iterable[list[bytes]]" in function "__init__" (reportArgumentType)
-   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:30 - error: Cannot access attribute "as_independent" for class "Basic"
-     Attribute "as_independent" is unknown (reportAttributeAccessIssue)
-   .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:78 - error: "args" is not a known attribute of "None" (reportOptionalMemberAccess)
- 38271 errors, 82 warnings, 0 informations
+ 38265 errors, 82 warnings, 0 informations

@rchiodo
Rich Chiodo (rchiodo) merged commit 729aabf into microsoft:main Jul 27, 2026
16 checks passed
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.

__class__ inside a lambda in class definition is reported as not defined

2 participants