Repository navigation
Stop the Python lowering dropping the sub-expressions that read names - #5
Merged
Merged
Conversation
Many Python visitors built their IR node without visiting what was
inside it: list/tuple/dict/set literals, f-string slots, `*xs`, the
index of a subscript, the receiver of a method call, the target of
`xs[i] = v`, `del d[k]`, `assert`, `raise`, `yield`, `match` subjects,
`while ... else`, and the decorators, defaults and bases of a
definition. Every name read only in one of those places was reported
as "Assigned but never read", including `start` in the Python sample
the web UI loads by default, `logger` in `logger.info(msg)`, `i` in
`return xs[i]`, `name` in `f"hi {name}"`, and every callback parameter
called as `fn(x)`. Calls in the same places were missing from the call
graph, and `memo[n] = ...` was read as a new local shadowing `memo`.
The visitors now descend into those children. Decorators, defaults and
base classes are emitted before the definition, since they run in the
enclosing scope, so `@app.route(...)` is not a call `index` makes. A
subscript store no longer counts as a binding, and a plain call counts
as a read of its callee.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
ethanzhoucool
marked this pull request as ready for review
November 3, 2026 17:15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Many Python visitors in
adapters/python.pybuild their IR node but never visit what's inside it. A name that is read only inside one of those constructs never produces aNAMEnode, so the symbol table reports it as "Assigned but never read". Calls in the same places are missing from the call graph.It shows up on the default page: the Python sample the web UI loads first gets a false finding,
`start` is assigned here and not read again.startis used on the next two lines, in{start: 0}and[(0, start)].Each of these produced a false "unused" on
main(thetest_python_reads_are_not_reported_as_unusedcases):Two related problems:
memo[n] = ...inside a function counted as a new localmemo, which gave a false "Shadows an outer name" on the classic memoisation pattern.while ... else:dropped itselsebody entirely.Changes
adapters/python.pyStarred,Assert,Raise,Yieldandmatch(subject and guards) now visit their children.Subscriptvisits its index or slice.Callvisits the receiver of a method call, or the whole callee expression when it isn't a bare name (helper().strip(),handlers[name](x)).target_reads()returns what an assignment / aug-assign /deltarget reads (subscript containers and indexes, attribute objects). Plain names and unpacked names stay bindings.definition_time_reads()emits decorators, parameter defaults and base classes as siblings before thedef/class. They run in the enclosing scope, so@app.route("/")readsappat module level and is not reported as a call thatindexmakes.whilevisits itsorelse.analysis/symbols.pymemo[n] = ...) is no longer a binding.CALLwith no receiver counts as a read of its callee. Tree-sitter languages already emitted aNAMEfor this; Python keeps the callee only in the call's meta.Effect on output: the walkthrough text for the bundled samples is unchanged. Halstead counts go up, since they now see every operand (Python sample MI 45.2 → 44.1). At developer level only, a multi-line literal now annotates its element lines (e.g.
str literal 'host'). JS, Java and C++ already did that.Testing
pyteston Python 3.13, 3.12 and 3.11: 174 passed, 3 skipped.ruff check explain_my_code testsis clean.mypyreports the same 26 errors as before.tests/test_analysis.py. 26 of them fail onmain:test_python_reads_are_not_reported_as_unused: 21 constructs.test_storing_into_a_container_does_not_shadow_it.test_python_call_graph_sees_nested_calls: chained call, call in a list,while/else, raised call.test_decorator_calls_belong_to_the_enclosing_scope: passes onmaintoo. It guards against a naive fix that would hang decorator calls under the function.test_unused_local_is_reportedstill passes, so real unused locals are still reported.Notes
Not changed: tuple-unpacking assignments (
a, b = pair) still bind nothing in the symbol table. That's a missed report, not a false one, so I left it alone.Generated by Claude Code