← Back to Pull requests

COD-44: Read Python symbols, imports and calls #94

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 22, 2026 20:16 UTC
Author

What

Code layer 4/5 (COD-44). @codechart/core 0.5.0 reads Python at the same level as TS/JS: symbols, resolved imports and calls.

  • Grammar. tree-sitter-python@0.25.0 (dev dependency, with MIT notice) joins the grammar assets and is loaded the same lazy way. .py and .pyi now get the Python grammar instead of no symbols.

  • Symbols. Upstream tags.scm already reads classes, functions and module constants; a def directly in a class body is turned into a method (decorated ones too), and a method's range starts at its def line so a self.m() call inside it belongs to the method, not the class.

  • Imports. A Python reader states import, from … import, from __future__, importlib.import_module("x") and __import__("x"), with the names each binds. Module-level definitions, assignments and import bindings are the module's exports, which is how a package's __init__.py re-exports through the existing tracer. if TYPE_CHECKING: and except ImportError: imports are typeOnly; __all__ narrows the list, and a star import, __getattr__ or computed __all__ makes it incomplete.

  • Calls. The shared resolved/name-only rule with Python's evidence: self.x() / cls.x() are this; a.b.f() keeps its receiver chain; import ns + ns.f() and from pkg import mod + mod.f() follow the module; service = PaymentService(…) + service.charge() follows the constructed class; a name both defined and imported in one file stays name-only because execution order decides it.
  • Resolution

    A Python spec is resolved against the repo's own layout, never a virtualenv:

  • Relative: leading dots walk up from the importing file's package.

  • Order at each step: pkg/__init__.py, then mod.py, then mod.pyi, then a namespace folder (a folder with Python files but no __init__.py).

  • Source roots: the importing file's package folder (topmost package, or a script's own folder) and its project's src/ folder — or the project root when there is no src/ layout. A project is the nearest folder with pyproject.toml, setup.py or setup.cfg. Another project's src/ is never searched.

  • sys.path edits: a file that writes sys.path gets unknown for every absolute import, and the flag survives incremental edits.

  • External vs unknown: stdlib names are external; a name declared in the project's pyproject.toml / setup.py / setup.cfg / Pipfile / requirements.txt is external; anything else that does not match is unknown.

  • Namespace packages (your call on the plan): the import itself stays unknown, but from nspkg import mod still records mod's submodule file on the name, so mod.f() resolves.

  • Incremental: a new __init__.py, module file or dependency file changes what an import means, and probes/re-resolution handle it like the TS config files.
  • One shared fix

    resolve/link.ts used to resolve calls against the imports from before the pass, so a call through a barrel or a submodule missed the definedIn its imports had just gained. The link pass now hands calls the freshened imports. This changes TS/JS behavior only for calls through a barrel in an incremental update (the case COD-43 already tested), and it is what Python's re-exports need.

    Spot-check on real repos

    Script (outside the repo) compares every resolved import against Python's own importer with the same sys.path, using pathlib/PathFinder semantics and the importing file's package:

    | Repo | Resolved imports checked | Wrong |
    |---|---|---|
    | pallets/flask | 264 | 0 |
    | encode/httpx | 125 | 0 |

    flask's tutorial examples resolve to unknown because they have no project file, which is correct: nothing proves their roots.

    Tests

  • python-symbols.test.ts: fixture outline, decorated/async methods, stubs, broken text, calls and callers.

  • python-imports.test.ts: src/ layout, relative imports one and two levels up, barrels, stdlib, declared dependencies, TYPE_CHECKING, namespace package, sys.path hack, module and constructed-class calls.

  • python-incremental.test.ts: single-file edit with a local ripple, new module, namespace → regular package, module appearing under a package, dependency file, sys.path flag across an edit.

  • New py-src fixture; languages.test.ts and symbols.test.ts updated where Python used to have no grammar.

  • Checks run: lint, typecheck, test (2434), build, and the dist build parses Python.
  • Known limits

  • a.b.f() where import a.b names the whole chain resolves; import a.b.c followed by a.b.f() where the receiver path is shorter than the spec does not.

  • A bare call to a name both defined and imported in one file is name-only; the parse does not keep statement order.

  • super().m() stays name-only.

  • Constructed-class calls need exactly one assignment and one constructor binding in reach.
  • Map _(key: + new ~ changed - removed ◆ config · unmarked = unchanged)_

    parse
    ~ languages.ts
    ~ grammars.ts ─Python wasm and tags─▶ grammar-assets.ts
                  ─Python imports query─▶ python/imports.ts
    ~ grammar-assets.ts
    ~ parse.ts ─extractPythonImports─▶ python/imports.ts
               ─call receiver chain──▶ model.ts
               ─extractSymbols───────▶ symbols.ts
               ─extractCalls─────────▶ calls.ts
               ─grammarOf────────────▶ languages.ts
    ~ model.ts
    ~ symbols.ts
    + python/imports.ts ─node helpers──▶ python/nodes.ts
                        ─moduleExports─▶ python/module-scope.ts
    + python/nodes.ts
    + python/module-scope.ts
    ~ calls.ts
    
    resolve
    + resolve/python.ts
    ~ resolver.ts ─Python importer─▶ resolve/python.ts
    ~ resolve/trace.ts
    ~ resolve/calls.ts ─trace─▶ resolve/trace.ts
    ~ resolve/link.ts ─fresh imports for calls─▶ resolve/calls.ts
    
    15 nodes · 4 new · 11 changed · 0 config · ✓ 15 checked
    gaps: spot-checked on flask and httpx only, with no virtualenv: stdlib import
          paths were not executed
    not on map: tests: packages/core/tests/code/python-*.test.ts,
                packages/core/tests/code/languages.test.ts,
                packages/core/tests/code/symbols.test.ts
                fixture: packages/core/tests/fixtures/code/py-src/
                packages/core/src/code/repo.ts,
                packages/core/src/code/code-index.ts, packages/core/package.json,
                THIRD_PARTY_NOTICES.md
    

    [Open full map](file:///Users/alex.maza/.cache/codechart/codechart-9266c63c/20261122-211546-cod-44-python-symbols-imports-and-calls.html)

    Flow _(key: + new ~ changed ? planned ◇ decision ⤷ shared step)_

    How a Python import resolves
    ────────────────────────────
    ◇ +leading dots?
    ├─ yes → +walk up from the package → ⤷ +one file finds it?
    └─ no  → ◇ +stdlib name?
             ├─ yes → ⤷ +external
             └─ no  → ◇ +file edits sys.path?
                      ├─ yes → ⤷ +unknown
                      └─ no  → +try the project's roots → ⤷ +one file finds it?
    
    ⤷ ◇ +one file finds it?
    ├─ yes → +resolved
    └─ no  → ◇ +declared dependency?
             ├─ yes → ⤷ +external
             └─ no  → ⤷ +unknown
    
    ⤷ +external
    
    ⤷ +unknown
    
    10 steps · 10 new · 0 changed · 0 planned · ✓ 22 anchors checked
    gaps: a namespace folder keeps the import unknown while its modules still
          resolve by name
    

    [Open full map](file:///Users/alex.maza/.cache/codechart/codechart-9266c63c/20261122-211634-how-a-python-import-resolves.flow.html)

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: greptile-apps[bot] Review duration: 9m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.52) × (1 − Review Confidence (0.37)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 60% · Reviewer familiarity: 35%
    36%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #94"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 36.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 36.9% · Lower = rushed or shallow review
    36.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.8% · Safe to merge
    32.8%