Skip to content

fix(python): salt module-level class ids that collide with a function or class (#2810) - #4467

Closed
hementhu wants to merge 1 commit into
Graphify-Labs:v8from
hementhu:fix/python-class-case-collision
Closed

hementhu wants to merge 1 commit into
Graphify-Labs:v8from
hementhu:fix/python-class-case-collision

Conversation

@hementhu

@hementhu hementhu commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What broke

ids.py casefolds and strips _, so distinct Python declarations in one module can mint the same node id. #3302 added a pre-scan that salts colliding module-level functions and class methods, but module-level class names were never recorded in it. So:

file v8 this PR
class Session + def session() only Session both
same, function declared first only session() both, same ids as above
class Foo + class FOO only Foo both
class Pair + class _Pair one both; Pair keeps its plain id
requests/sessions.py (2.32) 80 nodes, no session() 81 nodes

Which declaration survives depends on declaration order. explain session then answers with the class.

Fix

In _python_pre_scan_underscore_collisions:

  1. Record module-level class names in the same scope as module-level functions, and salt the class id at the class mint site with the existing _underscore_salted_nid rule (a unique public member keeps the plain id, others get a stable hash suffix, independent of order).
  2. Key method groups under the class's resolved id. A salted class mints its methods under the salted id, so keying them under the plain one would silently drop the existing _get/get protection inside that class.
  3. Look through decorated_definition, so @dataclass class Session and the _x / @property x method idiom are covered. The second one was also missed by Python: methods differing only by a leading underscore collide on node id — the public one is silently dropped from the graph #3302.

ensure_named_node applies the same salt for Python, as it already does for C#, so class Sub(Session) in the same file binds to the real class instead of minting a sourceless Session stub.

Tests

Ten tests are added to tests/test_python_underscore_resolution.py. Eight fail on v8. The other two (methods inside a salted class keep their protection; a same-file subclass binds to the salted class) guard against regressions a naive fix introduces.

They also cover: order independence, the constructor call session() → Session, and a cross-file from sessions import Session, session resolving to both.

  • uv run pytest tests/: 8059 passed, 15 skipped
  • uv run ruff check .: clean
  • uv run pyright: 642 errors, the same as unmodified v8

Notes

🤖 Generated with Claude Code

… or class (Graphify-Labs#2810)

The Python collision pre-scan (Graphify-Labs#3302) recorded module-level functions and
class methods but not module-level class names, so a class sharing an id
with a function or another class after normalization still lost one
declaration to first-wins add_node:

- `class Session` / `def session()` (requests/sessions.py): session() is
  absent from the graph, and which one survives depends on declaration order
- `class Foo` / `class FOO`, `class Pair` / `class _Pair`

Record module-level classes in the same scope as functions and salt the
class id with the existing _underscore_salted_nid rule. Methods of a salted
class are minted under the salted id, so the pre-scan now keys their groups
under the class's resolved id; keying them under the plain one would drop
the existing `_get`/`get` protection inside that class. Same-file base-class
references go through the same salt (as C# already does), so
`class Sub(Session)` binds to the real class instead of minting a sourceless
`Session` stub. The pre-scan also looks through decorated_definition, so
`@dataclass class Session` and the `_x` / `@property x` method idiom are
covered too.

Ids change only for files that contain such a collision; cached AST results
for them keep the collapsed ids until the AST cache schema is bumped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@hementhu
hementhu requested a review from safishamsi as a code owner October 15, 2026 08:41
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @hementhu. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 2 changed function(s) (2 not verified).

Formal verification. PR-changed functions: 0/2 verified (0 proven, 0 may-equivalent, 0 distinguished) · 2 not verified (1 vacuous, 1 unsupported).

Not verified on this run: \_extract\_generic (unsupported), \_python\_pre\_scan\_underscore\_collisions (vacuous: never exercised).


Graphify review — findings

Fixes silent node loss in the Python extractor when a module-level class collides on an id with a function or another class (Session / session(), Foo / FOO, Pair / _Pair). Previously first-wins add_node dropped one declaration, and which one survived depended on source order. The underscore/case collision pre-scan now records module-level class names alongside functions, sees through @decorator wrappers, and keys each class's methods under the class's resolved (possibly salted) id so _get/get inside a salted class stay distinct. Class and function id minting applies the same salting, so the public member keeps its plain id and the colliding one gets a salted id. Adds tests for order independence, constructor-call and subclass edges landing on the salted class (no phantom stub), cross-file import resolution, and decorated classes.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 952 functions depend on the 381 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • _extract_generic() — 20 callers, 42 callees (high)
  • extract_js() — 93 callers, 7 callees (high)
  • extract_objc() — 25 callers, 13 callees (high)
  • extract_xaml() — 19 callers, 17 callees (high)
  • extract_php() — 38 callers, 5 callees (high)
  • extract_cpp() — 37 callers, 5 callees (high)
  • extract_julia() — 22 callers, 8 callees (high)
  • extract_svelte() — 14 callers, 8 callees (high)
  • …and 2 more

Verification — 952 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 841 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

37 of 422 test file(s) selected (9%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_java_member_calls.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_languages.py — impact
  • tests/test_multilang.py — impact
  • tests/test_php_unparsed_casts.py — impact
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_property_reads.py — impact
  • tests/test_python_underscore_resolution.py — impact, changed-test
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_context_bounds.py — impact
  • tests/test_scala_given_instances.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact
  • tests/test_scala_type_definition.py — impact
  • tests/test_svelte_extraction.py — impact
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • tests/test_trailing_newline_not_a_syntax_error.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • tests/test_typescript_module_extensions.py — impact
  • tests/test_vue_extraction.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

Could not verify: Could not verify \_python\_pre\_scan\_underscore\_collisions.

The verifier did not have enough to check \_python\_pre\_scan\_underscore\_collisions, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 253 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

@hementhu

Copy link
Copy Markdown
Contributor Author

Full suite on this branch, per the checklist above:

Python result
3.10.21 8060 passed, 14 skipped
3.12 8059 passed, 15 skipped
3.13.15 8059 passed, 15 skipped

Ids are minted from the source alone (sha1 of the declared name, same _underscore_salted_nid rule as #3302), so extraction stays deterministic across runs and versions.

@safishamsi

Copy link
Copy Markdown
Member

@safishamsi safishamsi closed this Oct 16, 2026
@hementhu

Copy link
Copy Markdown
Contributor Author

Thanks @safishamsi, and thanks for the quick turnaround! Glad session() is back in the graph.

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.

2 participants