Pyre has a subcommand called
query allows you to hook into a Pyre server and get type-related
information without having to run a full type check.
This allows you, for instance, to get the type of an expression at a certain line and column, check whether a type is a subtype of the other, or get the list of methods for a class.
To get started, set up a server with
pyre start. The rest of this page goes through the various query options with examples. You can also run
pyre query help to see a full list of available queries to the Pyre server.
Note: The responses in the examples are prettified using the
pyre query <query> | python -m json.tool pattern.
attributes gives you the list of attributes for a class.
callees returns a list of all calls from a given function, including locations if using
defines returns all function and method definitions for a given module or class.
Dump call graph
dump_call_graph() returns a comprehensive JSON mapping each call to a list of callees.
Dump class hierarchy
dump_class_hierarchy() returns the entire class hierarchy as Pyre understands it; elides type variables.
join uses Pyre's type engine to find a common superclass for two types.
Less or equal
less_or_equal returns whether the type on the left can be used when the type on the right is expected.
meet uses Pyre's type engine to find a common subclass for two types.
methods returns the list of methods for a type, excluding inherited ones.
normalize_type resolves type aliases for a given type.
Path of module
path_of_module returns the full absolute path for a given module.
qualified_names returns a map from each given path to a list of all qualified names contained in that path. It can be called on multiple files at once with
qualified_names('path1', 'path2', ...).
Save server state
save_server_state saves the server's serialized state into the given
path, which can the be used to start up the identical server without re-analyzing all project files.
signature returns the type signature of a given function.
superclasses returns the superclasses of a given class name.
type evaluates the type of the given expression.
Type at position
type_at_position returns the type of the symbol at the provided position.
Types in file
types returns all the types for a file that Pyre has been able to resolve. It can be called on multiple files at once with
types('path1', 'path2', ...).
We determine locations for expressions using the following guidelines:
- Ignore leading and trailing whitespace, commas, comments, and wrapping parenthesis.
- Include whitespace, parenthesis or other noop tokens in the locations of compound expressions they are nested inside.
(a).bwill register two expressions, a at columns 1-2 (still following the guideline above), and
a.bat columns 0-5
- Similarly, compound expression locations must encompass the locations of all of its components.
a = b = 1will register the assignment
a = 1at columns 0-9, with
aat columns 0-1 and
1at columns 8-9
- The only exception are classes, which do not encompass their decorators
- All semantically meaningful tokens and reserved words are included in the node they define.
await awill register the awaitable node at columns 0-7, and the included identifier
aat columns 6-7
async def foo(): ...will register the define node at columns 0-20
foo(*args, **kwargs)will register args at columns 4-9 and kwargs at columns 11-19
"""string"""will register the string string at columns 0-12
- All implicit values in the AST contribute a length of 0 and point to the closest location to where an equivalent explicit value would live.
a: intwould register an Ellipsis object at columns 6-6
awould register a at columns 0-1 and
a.__getitem__at columns 0-1
a[:1]would register the first argument of slice to be
Noneat columns 2-2, the second argument to be
1at columns 3-4, and the third argument to be
Noneat columns 4-4.
batch command can be used to run several queries at once and return a map of responses. The list of queries to batch may include any combination of other valid queries except for
Pyre rechecks each file when queried to generate the location-type mapping, caching results for re-queries of the same file. If you anticipate a large codemod where significant portions of the codebase will be queried, you may increase incremental performance by starting a temporary server with the flag:
pyre --store-type-check-resolution start.