Skip to content

Pysa report: opt-in export of the inferred type of every expression #1

Open

Description

@espetro

DRAFT written by an LLM (Claude) for human review. Not for upstream as is: the owner rewrites it in their own words before anything is filed on facebook/pyrefly (see AI_POLICY.md).

Use case

A Python-to-native compiler wants the inferred type of every expression in a module as structured data, so it can lower unannotated locals and intermediate expressions.

What exists today (checked on origin/main 4b0d393 with a built binary)

pyrefly check --report-pysa writes type_of_expressions/<module>:<id>.json. The visitor in pyrefly/lib/report/pysa/type_of_expression.rs exports a type only for every Name, the base of every attribute access, and every call argument. Not exported: literals, binary and unary operations, call results, the attribute access itself, subscripts, comprehensions.

On a 14-line sample, a + b (6:13-6:18), the literal 42 (17:5-17:7) and the call use(Greeter()) (18:5-18:19) are absent from the report.

The types are already computed. Answers::get_type_trace has an entry for every expression the solver infers (populated by record_type_trace), and the trace is enabled whenever a Pysa reporter is present. The visitor just does not read it for most expressions.

Other surfaces checked: --report-cinderx is hidden and internal-only; --report-trace only records names and attribute accesses; pyrefly coverage is annotation-slot coverage; TSP getComputedType answers one position per request.

Related: pyrefly-org#4760 (expression-level type coverage metrics) would need the same raw data.

Proposal

An opt-in flag --report-pysa-all-expressions (requires --report-pysa) that makes the existing visitor export every expression. Default output is byte-for-byte unchanged and the capnp schema does not change. Patch with tests on branch types-report.

Questions for maintainers

  1. Is widening the existing report behind a flag acceptable, or would a separate output directory be preferred?
  2. Should the report record the expression kind, so a consumer can tell a callee from a call result? Callee and enclosing call have overlapping ranges, for example Overload[...] for message.upper and str for message.upper().
  3. Should expressions inside annotations be included? They are not today.
  4. Is exposing the flag on buck-check in scope for a GitHub PR?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions