Skip to content
openrewritePublic

About

Expose ty's type inference as a CLI tool and JSON-RPC server.

Resources

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

ty-types

A Rust CLI that exposes ty's Python type inference as structured JSON. It can infer types for one or more files in a single invocation, or run as a JSON-RPC server over stdio for multi-file sessions with cross-request type deduplication.

Building

Requires Rust 1.98+. The ruff/ submodule must be checked out first:

git submodule update --init
cargo build --release

The binary is at target/release/ty-types.

Usage

One-shot mode

Pass one or more Python files as arguments. The output is a single JSON object written to stdout:

ty-types app.py utils.py --project-root /path/to/project

If --project-root is omitted, it defaults to the parent directory of the first file. Pass --bindings to attach a BindingInfo to each name and attribute reference, or --symbols for its symbol form, which adds a symbols table to the output.

A file whose inference panics is reported on stderr and omitted from files; the remaining files are still analyzed, and the exit status is non-zero. The JSON on stdout is complete for the files that did resolve, so a caller reads the status to tell a partial run from a complete one.

Output format:

{
  "files": {
    "/absolute/path/to/app.py": [ <NodeAttribution>, ... ],
    "/absolute/path/to/utils.py": [ <NodeAttribution>, ... ]
  },
  "types": {
    "1": <TypeDescriptor>,
    "2": <TypeDescriptor>
  }
}

files maps each file path to its list of typed AST nodes. types is a shared registry — nodes reference types by ID, and the same type (e.g. int) gets a single entry even if it appears in multiple files.

JSON-RPC server mode

For processing many files or integrating with editors and tooling, run as a persistent server:

ty-types --serve

The server reads JSON-RPC requests from stdin (one per line) and writes responses to stdout. A session looks like:

→ {"jsonrpc":"2.0","method":"initialize","params":{"projectRoot":"/path/to/project"},"id":1}
← {"jsonrpc":"2.0","result":{"ok":true},"id":1}

→ {"jsonrpc":"2.0","method":"getTypes","params":{"file":"app.py"},"id":2}
← {"jsonrpc":"2.0","result":{"nodes":[...],"types":{"1":...,"2":...}},"id":2}

→ {"jsonrpc":"2.0","method":"getTypes","params":{"file":"utils.py"},"id":3}
← {"jsonrpc":"2.0","result":{"nodes":[...],"types":{"5":...}},"id":3}

→ {"jsonrpc":"2.0","method":"shutdown","id":99}
← {"jsonrpc":"2.0","result":{"ok":true},"id":99}

The type registry persists across getTypes requests within a session. Each getTypes response includes only the newly discovered types — types already sent in a previous response are not repeated. The client accumulates the registry as it goes.

To retrieve the full accumulated registry at any point, call getTypeRegistry.

Every table keyed by id, such as types and symbols, lists its entries in ascending id order. One request against the same files gives the same bytes in every process.

JSON-RPC methods

initialize

Must be the first call. Creates the project database.

Field Type Description
params.projectRoot string Absolute path to the Python project root
params.firstPartyRoot string Optional. Package root, absolute or relative to projectRoot. getTypes reports a class defined outside it as a classRef
params.firstPartyModules string[] Optional. Top-level module names, used when firstPartyRoot is absent: getTypes reports a class outside them as a classRef

Returns {"ok": true}.

getTypes

Infers types for a Python file and returns the typed AST nodes plus any new type descriptors.

Field Type Default Description
params.file string File path (absolute or relative to project root)
params.includeDisplay boolean true Include human-readable display strings on type descriptors
params.includeBindings boolean false Include a binding on each name and attribute reference. Costs roughly 10% inference time and 17% payload
params.includeSymbols boolean false Implies includeBindings, and reports each binding as a symbol id described in symbols. See Symbols

Returns:

{
  "nodes": [ <NodeAttribution>, ... ],
  "types": { "<TypeId>": <TypeDescriptor>, ... },
  "symbols": { "<SymbolId>": <SymbolInfo>, ... }
}

symbols is present only under includeSymbols, and then always, if only as {}.

ty's inference panics on some inputs (e.g. astral-sh/ty#4454). Such a file answers with a JSON-RPC error of code -32001 naming the file and the panic, and the session stays usable — later requests are unaffected, and types already registered reach the client with the next successful response.

getTypeRegistry

Returns the full accumulated type registry from all getTypes calls in the current session. Takes no parameters.

Returns:

{
  "types": { "<TypeId>": <TypeDescriptor>, ... }
}

getReadFiles

Returns every file whose content the session has read since initialize, including through requests that failed. Takes no parameters. The list holds:

  • the configuration files found — a path ty tried and found missing is not listed;
  • every Python file under the project root that the project's src settings and ignore files admit, each read when ty indexes the project for PEP 723 script metadata — a notebook is read only when a request names it;
  • each source, stub, py.typed and .pth file reached through the search roots.

Returns:

{
  "readFiles": [ "<absolute path>", ... ]
}

A file missing from the list can have affected a response only by existing — except the ignore files (.gitignore, .ignore) that decide which files under the project root are indexed, and, when TY_UV is set, the files uv reads to describe the workspace.

A path is spelled as ty reached it: a search root with its symlinks resolved, then the path below it as found, so a symlinked file is listed as the link and not its target. Configuration files keep the spelling of the directory ty searched.

shutdown

Ends the session and exits the server. Returns {"ok": true}.

getLibraryApi

Extracts the public API of one installed distribution. It has a registry of its own, so its type IDs are unrelated to those getTypes returns.

Field Type Default Description
params.roots string[] Package directories or module files the distribution installs, absolute or relative to projectRoot. A class defined outside all of them is a classRef
params.root string A single root, unioned with roots. One of the two is required
params.includePrivateModules boolean false Keep modules with an underscore-prefixed path component
params.includeNonExportedSymbols boolean false Keep module-level names __all__ omits, or, with no __all__, underscore-prefixed names and a stub's imports not re-exported as x as x
params.includeDisplay boolean true Include display strings on type descriptors

Returns:

{
  "modules": [ { "name": "pkg.mod", "file": "pkg/mod.py", "symbols": [ { "name": string, "typeId": integer } ] } ],
  "types": { "<TypeId>": <TypeDescriptor>, ... }
}

file is relative to the parent of the root holding the module. A module whose inference panics is left out and named in a warning on stderr.

getStdlibApi

Extracts the standard library's public API for the project's Python version, in the same shape as getLibraryApi. Its file is relative to the stdlib directory, e.g. os/path.pyi.

Field Type Default Description
params.modules string[] all Top-level module names to extract. A class in any other module is a classRef
params.includeDisplay boolean true Include display strings on type descriptors

Schema

NodeAttribution

Each entry in the nodes array represents a typed AST node:

{
  "start": 0,
  "end": 5,
  "nodeKind": "ExprName",
  "typeId": 1,
  "callSignature": null
}
Field Type Description
start integer Byte offset of the node start in the source file
end integer Byte offset of the node end
nodeKind string AST node kind (see below)
typeId integer | null Reference into the type registry
callSignature CallSignatureInfo | null Present only on ExprCall nodes
binding BindingInfo On ExprName and ExprAttribute nodes under includeBindings or includeSymbols, where the reference resolves, and on Parameter and ParameterWithDefault nodes under includeSymbols (omitted otherwise)

Node kinds: StmtFunctionDef, StmtClassDef, StmtAssign, StmtFor, StmtWith, ExprCall, ExprBoolOp, ExprBinOp, ExprUnaryOp, ExprLambda, ExprIf, ExprDict, ExprSet, ExprListComp, ExprSetComp, ExprDictComp, ExprGenerator, ExprAwait, ExprYield, ExprYieldFrom, ExprCompare, ExprFString, ExprTString, ExprStringLiteral, ExprBytesLiteral, ExprNumberLiteral, ExprBooleanLiteral, ExprNoneLiteral, ExprEllipsisLiteral, ExprAttribute, ExprSubscript, ExprStarred, ExprName, ExprList, ExprTuple, ExprSlice, Parameter, ParameterWithDefault, Alias

Quoted annotations: a string annotation's body contributes its own nodes, at their byte offsets in the file, so x: "list[int]" attributes as x: list[int] does. The string has its own node and type, and the body's parts sit within it:

[
  { "start": 3, "end": 14, "nodeKind": "ExprStringLiteral", "typeId": 1 },
  { "start": 4, "end": 13, "nodeKind": "ExprSubscript", "typeId": 1 },
  { "start": 4, "end": 8, "nodeKind": "ExprName", "typeId": 2 },
  { "start": 9, "end": 12, "nodeKind": "ExprName", "typeId": 3 }
]

A quote nested in a quote descends too, so "dict[str, 'Later']" yields a node for Later. A string ty does not read as an annotation stays a single node: a value such as Literal["a"], a raw prefix, an escape sequence, an implicit concatenation, or a syntax error.

BindingInfo

Where a referenced symbol is bound, following re-export chains to the original binding. pkg/__init__.py re-exporting LIMIT from pkg._impl gives the same answer at every reference:

{
  "definedIn": "pkg._impl",
  "qualifiedName": "pkg._impl.LIMIT"
}
Field Type Description
definedIn string Module holding the binding
qualifiedName string Dotted path through the enclosing scopes, e.g. app.Holder.MAX. definedIn says where the module part ends

A reference resolves when the binding is a function, class, type alias, or plain or annotated assignment. Parameters, loop and with and except targets, walrus and comprehension bindings, and attributes assigned in a method body all yield no binding. Scopes other than modules and classes appear in qualifiedName as ty spells them, so a function-local binding reads app.<locals of function 'f'>.x.

moduleName on a type descriptor is the module of the value's type — builtins for SEP: str = "/", and absent entirely for LIMIT = 5. Binding is a property of the reference, which is why it is reported here rather than on the type.

definedIn names the module that binds the symbol, not the one it is conventionally imported from: os.sep reports posixpath.sep. This is the convention descriptors already use — os.path.join reports moduleName: "posixpath".

A name bound more than once resolves to the binding that reaches the reference. Where module m imports f from a, calls it, then defines its own f and calls that, the first call reports a.f and the second m.f.

A symbol bound in more than one branch of a conditional import resolves to the branch whose type was inferred at the reference. Where the branches declare the same type, the choice among them is arbitrary.

Symbols

Under includeSymbols, a binding names a variable rather than describing it: {"symbolId": 7}. The symbols table of the response describes each id:

{
  "7": {
    "name": "a",
    "definedIn": "m",
    "qualifiedName": "m.<locals of function 'f'>.a"
  }
}
Field Type Description
name string The variable's name
definedIn string Module holding the variable, as in BindingInfo. Absent for a file that resolves to no module, such as my-script.py, and for an import ty cannot follow
qualifiedName string As in BindingInfo, with lambda and comprehension scopes spelled too, and a type param's own scope: m.<locals of lambda>.c, m.<locals of list comprehension>.x, m.<type params of class 'C'>.T. Absent with definedIn

An id stands for a variable, not an assignment: two references share an id exactly when they read the same variable, whichever assignment reaches each, except for an import ty cannot follow, described below. x = 1 and x = 2 in one scope are one variable. Two functions' locals named r are two, even where their qualifiedNames coincide, as for a property's getter and setter. Key a variable by its id, not by qualifiedName.

Ids hold for the session. Like a type descriptor, a symbol is described only in the first response that mentions it, so a client keeps the table it has received.

Every kind of variable resolves: parameters, loop and with and except targets, walrus and comprehension variables, and match captures, besides what BindingInfo covers. A parameter's declaration node carries the same id as its uses. A global or nonlocal name, and a closure's read of an enclosing variable, get the id of the scope that owns the variable. So does a walrus target inside a comprehension, which PEP 572 binds in the scope containing the comprehension: in def f(xs): [(last := x) for x in xs], last reads m.<locals of function 'f'>.last. An attribute assigned only in a method body (self.x = ...) is no variable and has no binding. Where the class body also binds x, self.x, obj.x and C.x all name that class variable.

A reference to an imported name gets the symbol of the definition the import names. Where ty cannot follow the import (from lib import gone with lib declaring no gone, or import nosuchmod), the import is a symbol of its own, described by name alone, since it declares nothing in the scope it stands in. A name that scope also binds some other way is a second symbol, with definedIn and qualifiedName. In module m, from lib import gone / gone() / gone = None / gone() gives the first call the import's id, and the assignment target and the second call m.gone's. A reference both reach, as after try: from lib import gone / except ImportError: def gone(): ..., gets the one BindingInfo resolves to, which is the one first in the source.

Over a 71-file project, a symbol binding with its share of the table costs about 63 bytes against 120 for a BindingInfo. includeSymbols reports 75% more bindings than includeBindings and still adds slightly less payload: 19% over no bindings, against 20%.

CallSignatureInfo

Attached to ExprCall nodes. Contains the resolved signature at the call site, including any generic specialization:

{
  "parameters": [ <ParameterInfo>, ... ],
  "returnTypeId": 3,
  "typeArguments": [4]
}
Field Type Description
parameters ParameterInfo[] Resolved parameters of the called function
returnTypeId integer | null Return type (specialized if generic)
typeArguments integer[] Type arguments inferred for generic calls (e.g. T=int)

For an overloaded callee the signature is the first overload whose parameters accept the call's arguments, or the first overload when none does.

ParameterInfo

{
  "name": "x",
  "typeId": 2,
  "kind": "positionalOrKeyword",
  "hasDefault": true,
  "defaultTypeId": 5
}
Field Type Description
name string Parameter name
typeId integer | null Annotated type
kind string One of positionalOnly, positionalOrKeyword, keywordOnly, variadic, keywordVariadic
hasDefault boolean Whether the parameter has a default value
defaultTypeId integer | null Type of the default value (e.g. Literal[42])
concatenatePrefix boolean true on the leading positional parameters of a Concatenate[T1, ..., Tn, P] or Concatenate[T1, ..., Tn, ...] signature (omitted when false)
paramSpecName string Set on the *args / **kwargs entries that stand in for a ParamSpec tail, carrying that ParamSpec's name (e.g. "P") (omitted when absent)

TypeDescriptor

Every type in the registry is a tagged object with a "kind" discriminator. All variants include an optional display field with ty's human-readable representation (omit with includeDisplay: false).

Fields marked with "omitted when empty" are not present in the JSON when their value is empty or null.

qualifiedName, wherever it appears below, is the dotted path of the enclosing scopes followed by the item's own name (e.g. a.b.C.D). It is the field to key a class by: moduleName and className together cannot tell a.b.C.D apart from a.b.D. Scopes other than modules and classes appear as ty spells them, so a class defined inside a function reads a.<locals of function 'f'>.C — split it on . only if that spelling is accounted for. It is omitted where it would not identify the class: ty names a class built from a runtime name <unknown>, and two such classes in one scope render identically.

instance

An object of a class (e.g. int, str, MyClass()).

Field Type Description
className string Class name
moduleName string Defining module (omitted when empty)
qualifiedName string Fully qualified class name; absent for synthesized protocols (omitted when empty)
supertypes integer[] Resolved base class type IDs (omitted when empty)
typeArgs integer[] Specialization args, e.g. list[int] → [<int>] (omitted when empty)
classId integer Type ID of the corresponding classLiteral (omitted when empty)
tupleElements TupleElement[] Element types, on tuples and their subclasses (omitted otherwise)

Each TupleElement is {"typeId": <integer>, "kind": <string>}, where kind is fixed for a single element, homogeneous for the ... segment of tuple[int, ...], or typeVarTuple for the *Ts of tuple[int, *Ts]. The latter two each stand for an unknown number of elements.

classLiteral

A class object itself (the value of type[MyClass]).

Field Type Description
className string Class name
moduleName string Defining module (omitted when empty)
qualifiedName string Fully qualified class name (omitted when empty)
typeParameters integer[] Generic type parameters (T, U, ...) (omitted when empty)
supertypes integer[] Explicit base classes (omitted when empty)
members ClassMemberInfo[] Members the class defines itself, one per name (omitted when empty)

ClassMemberInfo: { "name": string, "typeId": integer, "instanceAttribute": true }. instanceAttribute marks an attribute assigned through self in one of the class's methods and not defined in its body, and is omitted otherwise. An attribute a base class assigns stays on the base.

classRef

A class defined outside a boundary — the roots of getLibraryApi, the modules of getStdlibApi, or the first-party root or modules initialize sets for getTypes. Identity only, no members, supertypes or type parameters.

Field Type Description
className string Class name
moduleName string Defining module (omitted when empty)
qualifiedName string Fully qualified class name (omitted when empty)

qualifiedName is what rejoins a ref to the classLiteral carrying the same class's body.

Every field holding the type ID of a class reports classRef for a class outside the boundary, so instance.classId, subclassOf.base, super.pivotClassId and declaringClassId each read as this kind rather than the classLiteral documented for them.

subclassOf

A type[C] constraint (subclass relationship).

Field Type Description
base integer Type ID of the base: a classLiteral for a class or a protocol declared as one, an instance for a synthesized protocol, otherwise dynamic or typeVar

super

A bound super object: super() or super(C, obj).

Field Type Description
pivotClassId integer Type ID of the class the attribute lookup starts after in the receiver's MRO — C for super(C, obj), the enclosing class for a bare super()
receiverId integer Type ID of super()'s second argument, implicit inside a method body (a Self typeVar there)

Neither field names the class that declares what a super() attribute resolves to, because that is a property of the attribute rather than of the receiver: with f declared on A and on C, super().f inside class C(B), class B(A) resolves to A.f, past a pivot of C and a next-in-MRO of B. Read declaringClassId off the attribute's own boundMethod for that.

super(C) without a second argument is an instance of builtins.super rather than a bound super object, and reports instance.

typeForm

A TypeForm[T] value wrapping a type expression (PEP 747).

Field Type Description
typeArgument integer Type ID of the wrapped type expression

knownInstance

A well-known singleton instance ty tracks specially, such as TypeVar, typing.Callable, functools.partial(...) or range(...).

Field Type Description
className string Class name
moduleName string Defining module (omitted when empty)
knownInstanceKind string Which known instance this is
isNonEmpty boolean Whether the instance is known to be non-empty (omitted when unknown)
wrappedType integer Type ID of the wrapped type, where the kind wraps one (omitted otherwise)
parameters ParameterInfo[] Parameters, where the kind is callable (omitted when empty)
returnType integer Return type ID, where the kind is callable (omitted when empty)

enumComplement

An enum instance with one or more canonical members excluded, e.g. Color & ~Literal[Color.RED].

Field Type Description
className string Enum class name
moduleName string Defining module (omitted when empty)
qualifiedName string Fully qualified enum class name (omitted when empty)
classId integer Type ID of the enum's classLiteral
excludedNames string[] Member names excluded from the enum
rest integer[] Type IDs of the members that remain

union

A union type (X | Y).

Field Type Description
members integer[] Type IDs of the union members

intersection

A narrowed type from control flow (e.g. isinstance checks).

Field Type Description
positive integer[] Types that must all be satisfied
negative integer[] Types that must not be satisfied

function

A named function.

Field Type Description
name string Function name
moduleName string Defining module (omitted when empty)
declaringClassId integer Type ID of the classLiteral whose body declares the function (omitted for a function that is not a method)
typeParameters integer[] Generic type parameters (omitted when empty)
parameters ParameterInfo[] Full signature
returnType integer | null Return type ID

boundMethod

A method bound to an instance.

Field Type Description
name string | null Method name (omitted when empty)
className string | null The receiver's class, set when the receiver is a nominal or protocol instance (a literal or Self receiver reports none, though declaringClassId still resolves: "".join names builtins.str)
moduleName string | null Defining module (omitted when empty)
declaringClassId integer Type ID of the classLiteral whose body declares the method (omitted when there is no such class)
typeParameters integer[] Generic type parameters (omitted when empty)
parameters ParameterInfo[] Full signature, self included
returnType integer | null Return type ID

className and declaringClassId answer different questions and disagree on an inherited method: Child().greet(), with greet declared on Base, reports className: "Child" and a declaringClassId naming Base. The declaring class is the one a method pattern matches.

A classmethod can bind a callable instance rather than a Python function. There is no definition behind such a method, so name, moduleName, declaringClassId and parameters are all absent and display is what identifies it.

callable

A generic callable with unknown signature.

No additional fields beyond display.

intLiteral

Field Type Description
value integer The literal value

boolLiteral

Field Type Description
value boolean true or false

stringLiteral

Field Type Description
value string The literal string value

bytesLiteral

Field Type Description
value string Display representation (e.g. Literal[b"data"])

enumLiteral

Field Type Description
className string Enum class name
qualifiedName string Fully qualified name of the enum class, not the member (omitted when empty)
memberName string Member name

literalString

The typing.LiteralString special form. No additional fields.

typeVar

A generic type variable.

Field Type Description
name string Variable name (e.g. T)
variance string | null covariant, contravariant, or invariant (omitted when empty)
upperBound integer | null Bound type ID (from T: bound=int) (omitted when empty)
constraints integer[] Constraint type IDs (from T(int, str)) (omitted when empty)

module

Field Type Description
moduleName string Fully qualified module name

typeAlias

Field Type Description
name string Alias name
qualifiedName string Fully qualified alias name (omitted when empty)

typedDict

Field Type Description
name string TypedDict name
qualifiedName string Fully qualified TypedDict name (omitted when empty)
fields TypedDictFieldInfo[] Typed fields (omitted when empty)

TypedDictFieldInfo: { "name": string, "typeId": integer, "required": boolean, "readOnly": boolean }

typeIs

A TypeIs[T] return type for type narrowing functions.

Field Type Description
narrowedType integer The narrowed type ID

typeGuard

A TypeGuard[T] return type for type guard functions.

Field Type Description
guardedType integer The guarded type ID

newType

Field Type Description
name string NewType name
qualifiedName string Fully qualified NewType name (omitted when empty)
baseType integer Underlying type ID

specialForm

Typing special forms like Any, Never, ClassVar, etc.

Field Type Description
name string Form name

dynamic

Unknown or dynamically typed values.

Field Type Description
dynamicKind string e.g. Unknown

never

The bottom type (unreachable code). No additional fields.

truthy / falsy

Narrowed truthiness. No additional fields.

property

A property descriptor.

Field Type Description
getter integer Type ID of the getter. For @property, the decorated function, whose returnType is the property's type (omitted when the property has none)
setter integer Type ID of the setter (omitted for a read-only property)
deleter integer Type ID of the deleter (omitted when the property has none)

other

Fallback for ty-internal types not yet mapped to a structured descriptor. No additional fields beyond display.

Example

Given example.py:

x: int = 42

One-shot:

ty-types example.py
{
  "files": {
    "/path/to/example.py": [
      { "start": 0, "end": 1, "nodeKind": "ExprName", "typeId": 1 },
      { "start": 9, "end": 11, "nodeKind": "ExprNumberLiteral", "typeId": 2 }
    ]
  },
  "types": {
    "1": { "kind": "instance", "display": "int", "className": "int" },
    "2": { "kind": "intLiteral", "display": "Literal[42]", "value": 42 }
  }
}

Server mode (processing two files across requests):

echo '{"jsonrpc":"2.0","method":"initialize","params":{"projectRoot":"/path/to/project"},"id":1}
{"jsonrpc":"2.0","method":"getTypes","params":{"file":"example.py"},"id":2}
{"jsonrpc":"2.0","method":"getTypes","params":{"file":"other.py"},"id":3}
{"jsonrpc":"2.0","method":"shutdown","id":99}' | ty-types --serve

The response for id:2 includes type descriptors for int and Literal[42]. If other.py also uses int, the response for id:3 will not repeat the int descriptor — it was already sent. Call getTypeRegistry at any point to get the full accumulated registry.

About

Expose ty's type inference as a CLI tool and JSON-RPC server.

Resources

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages