Skip to content

Preserve generator scopes in lambda qualified names - #113

Merged
alexmojaki merged 2 commits into
alexmojaki:masterfrom
anticipate218:fix/generator-lambda-qualnames
Nov 26, 2026
Merged

alexmojaki merged 2 commits into
alexmojaki:masterfrom
anticipate218:fix/generator-lambda-qualnames

Conversation

@anticipate218

@anticipate218 anticipate218 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Problem and change

Source.code_qualname() omits the generator-expression scope from lambdas created in a generator body, filter, or later iterable. For example, list((lambda: None) for _ in range(1))[0] has the CPython qualname <genexpr>.<lambda>, but the current implementation reports <lambda>. This can mislabel frames in clients such as the Sentry executing integration.

Visit the outermost iterable in the enclosing scope and the remaining generator nodes inside <genexpr>. Preserve PyPy's additional <locals> component. Add 27 regression cases across module, function, and class scopes, including nested generators/lambdas and outer-iterable controls.

Validation

  • Original implementation: 24 regression failures, 3 controls pass. Fixed implementation: all 27 pass.
  • Windows / CPython 3.13.5, PYTHONUTF8=1 EXECUTING_SLOW_TESTS=1 python -m pytest -q tests: 281 passed, 6 skipped. Same environment on the original implementation: 254 passed, 6 skipped.
  • Mypy 2.1.0: no issues in 7 source files. Configured Ruff 0.15.17 format check: all 21 applicable files pass.
  • The 27 regression cases also pass when invoked directly on CPython 3.9.25, 3.11.18, 3.12.16 and PyPy 3.8.16; these are focused checks, not full suites on those interpreters.
  • Independent runtime comparison covered 130 callable observations on each of six interpreters (the above plus CPython 3.15.2): no previously correct result regressed. Pure-generator cases pass. Existing list/dict/set-comprehension scope omissions on older interpreters remain outside this change, as does the documented same-line nested-lambda ambiguity.

The initial Windows default-encoding run hit a GBK decoding failure in the existing source-file test; UTF-8 mode resolves that environment issue. Full tox interpreter coverage and the pre-commit wrapper were not run; the exact configured formatting hook was run directly. No final coverage percentage is claimed.

AI assistance

Developed and checked with OpenAI Codex, including AI-assisted implementation, tests, and independent review, under the account owner's authorization. No claim of a separate human code review is made.

Visit generator-expression scopes explicitly while evaluating the first iterable in its enclosing scope. Preserve PyPy locals and cover nested lambda and generator contexts.

Assisted-by: OpenAI Codex
Comment thread README.md Outdated

For a lambda created in a generator expression, the qualified name includes
its generator scope, such as `<genexpr>.<lambda>` on CPython. Lambdas in the outermost
iterable expression retain the enclosing scope.

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.

This pr fixes the implementation. Keeping a list of all the edge cases which are fixed in the readme is not a good idea.

just delete it.

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.

Makes sense, removed it in 5ef4949. The PR is now just the implementation change and the tests.

The change fixes the implementation; the README does not need to list the edge case.
@15r10nk

15r10nk commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

@alexmojaki this one looks ok to me. we can merge it when you agree.

@alexmojaki
alexmojaki merged commit ee8b714 into alexmojaki:master Nov 26, 2026
10 checks passed
@anticipate218

Copy link
Copy Markdown
Contributor Author

Thanks for merging, @alexmojaki, and thanks @15r10nk for the review.

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.

3 participants