Facilities to analyze entire programs, including imported modules.
Parse and analyze the source files of a program in the correct order (based on file dependencies), and collect the results.
This module only directs a build, which is performed in multiple passes per file. The individual passes are implemented in separate modules.
The function build() is the main interface to this module.
Class | BuildManager |
This class holds shared state for building a mypy program. |
Class | BuildResult |
The result of a successful build. |
Class | BuildSourceSet |
Efficiently test a file's membership in the set of build sources. |
Class | ModuleNotFound |
Control flow exception to signal that a module was not found. |
Class | NodeInfo |
Some info about a node in the graph of SCCs. |
Class | State |
The state for a module. |
Function | add_catch_all_gitignore |
Add catch-all .gitignore to an existing directory. |
Function | build |
Analyze a program. |
Function | cache_meta_from_dict |
Build a CacheMeta object from a json metadata dictionary |
Function | compute_hash |
Undocumented |
Function | create_metastore |
Create the appropriate metadata store. |
Function | default_data_dir |
Returns directory containing typeshed directory. |
Function | delete_cache |
Delete cache files for a module. |
Function | deps_filtered |
Filter dependencies for id with pri < pri_max. |
Function | deps_to_json |
Undocumented |
Function | dispatch |
Undocumented |
Function | dump_graph |
Dump the graph as a JSON string to stdout. |
Function | exclude_from_backups |
Exclude the directory from various archives and backups supporting CACHEDIR.TAG. |
Function | exist_added_packages |
Find if there are any newly added packages that were previously suppressed. |
Function | find_cache_meta |
Find cache data for a module. |
Function | find_config_file_line_number |
Return the approximate location of setting_name within mypy config file. |
Function | find_module_and_diagnose |
Find a module by name, respecting follow_imports and producing diagnostics. |
Function | find_module_simple |
Find a filesystem path for module id or None if not found. |
Function | find_module_with_reason |
Find a filesystem path for module id or the reason it can't be found. |
Function | generate_deps_for_cache |
Generate fine-grained dependencies into a form suitable for serializing. |
Function | get_cache_names |
Return the file names for the cache files. |
Function | import_priority |
Compute import priority from an import node. |
Function | in_partial_package |
Check if a missing module can potentially be a part of a package. |
Function | invert_deps |
Splits fine-grained dependencies based on the module of the trigger. |
Function | json_dumps |
Undocumented |
Function | load_graph |
Given some source files, load the full dependency graph. |
Function | load_plugins |
Load all configured plugins. |
Function | load_plugins_from_config |
Load all configured plugins. |
Function | log_configuration |
Output useful configuration information to LOG and TRACE |
Function | missing_stubs_file |
Undocumented |
Function | module_not_found |
Undocumented |
Function | normpath |
Convert path to absolute; but to relative in bazel mode. |
Function | order_ascc |
Come up with the ideal processing order within an SCC. |
Function | process_fresh_modules |
Process the modules in one group of modules from their cached data. |
Function | process_graph |
Process everything in dependency order. |
Function | process_stale_scc |
Process the modules in one SCC from source code. |
Function | read_deps_cache |
Read and validate the fine-grained dependencies cache. |
Function | read_plugins_snapshot |
Read cached snapshot of versions and hashes of plugins from previous run. |
Function | read_quickstart_file |
Undocumented |
Function | record_missing_stub_packages |
Write a file containing missing stub packages. |
Function | skipping_ancestor |
Produce an error for an ancestor ignored due to --follow_imports=error |
Function | skipping_module |
Produce an error for an import ignored due to --follow_imports=error |
Function | sorted_components |
Return the graph's SCCs, topologically sorted by dependencies. |
Function | strongly_connected_components |
Compute Strongly Connected Components of a directed graph. |
Function | take_module_snapshot |
Take plugin module snapshot by recording its version and hash. |
Function | topsort |
Topological sort. |
Function | validate_meta |
Checks whether the cached AST of this module can be used. |
Function | write_cache |
Write cache files for a module. |
Function | write_deps_cache |
Write cache files for fine-grained dependencies. |
Function | write_plugins_snapshot |
Write snapshot of versions and hashes of currently active plugins. |
Constant | CORE_BUILTIN_MODULES |
Undocumented |
Constant | DEBUG_FINE_GRAINED |
Undocumented |
Constant | DEPS_META_FILE |
Undocumented |
Constant | DEPS_ROOT_FILE |
Undocumented |
Constant | FAKE_ROOT_MODULE |
Undocumented |
Constant | PLUGIN_SNAPSHOT_FILE |
Undocumented |
Constant | PRI_ALL |
Undocumented |
Constant | PRI_HIGH |
Undocumented |
Constant | PRI_INDIRECT |
Undocumented |
Constant | PRI_LOW |
Undocumented |
Constant | PRI_MED |
Undocumented |
Constant | PRI_MYPY |
Undocumented |
Constant | T |
Undocumented |
Variable | CacheMeta |
Undocumented |
Variable | FgDepMeta |
Undocumented |
Variable | Graph |
Undocumented |
Function | _build |
Undocumented |
Function | _cache_dir_prefix |
Get current cache directory (or file if id is given). |
Function | _load_json_file |
A simple helper to read a JSON file with logging. |
Add catch-all .gitignore to an existing directory.
No-op if the .gitignore already exists.
Parameters | |
target_dir:str | Undocumented |
Analyze a program.
A single call to build performs parsing, semantic analysis and optionally type checking for the program and all imported modules, recursively.
Return BuildResult if successful or only non-blocking errors were found; otherwise raise CompileError.
If a flush_errors callback is provided, all error messages will be passed to it and the errors and messages fields of BuildResult and CompileError (respectively) will be empty. Otherwise those fields will report any error messages.
sources: list of sources to build options: build options alt_lib_path: an additional directory for looking up library modules
(takes precedence over other directories)
flush_errors: optional function to flush errors after a file is processed fscache: optionally a file-system cacher
Parameters | |
sources:List[ | Undocumented |
options:Options | Undocumented |
alt_lib_path:Optional[ | Undocumented |
flush_errors:Optional[ | Undocumented |
fscache:Optional[ | Undocumented |
stdout:Optional[ | Undocumented |
stderr:Optional[ | Undocumented |
extra_plugins:Optional[ | Undocumented |
Returns | |
BuildResult | Undocumented |
Build a CacheMeta object from a json metadata dictionary
Parameters | |
meta:Dict[ | Undocumented |
data_json:str | Undocumented |
Returns | |
CacheMeta | Undocumented |
Parameters | |
options:Options | Undocumented |
Returns | |
MetadataStore | Undocumented |
Delete cache files for a module.
The cache files for a module are deleted when mypy finds errors there. This avoids inconsistent states with cache files from different mypy runs, see #4043 for an example.
Parameters | |
id:str | Undocumented |
path:str | Undocumented |
manager:BuildManager | Undocumented |
Parameters | |
graph:Graph | Undocumented |
vertices:AbstractSet[ | Undocumented |
id:str | Undocumented |
pri_max:int | Undocumented |
Returns | |
List[ | Undocumented |
Undocumented
Parameters | |
sources:List[ | Undocumented |
manager:BuildManager | Undocumented |
stdout:TextIO | Undocumented |
Returns | |
Graph | Undocumented |
Dump the graph as a JSON string to stdout.
This copies some of the work by process_graph() (sorted_components() and order_ascc()).
Parameters | |
graph:Graph | Undocumented |
stdout:Optional[ | Undocumented |
Exclude the directory from various archives and backups supporting CACHEDIR.TAG.
If the CACHEDIR.TAG file exists the function is a no-op.
Parameters | |
target_dir:str | Undocumented |
Find if there are any newly added packages that were previously suppressed.
Exclude everything not in build for follow-imports=skip.
Parameters | |
suppressed:List[ | Undocumented |
manager:BuildManager | Undocumented |
options:Options | Undocumented |
Returns | |
bool | Undocumented |
Find cache data for a module.
Parameters | |
id:str | Undocumented |
path:str | Undocumented |
manager:BuildManager | Undocumented |
Returns | |
Optional[ | Undocumented |
Return the approximate location of setting_name within mypy config file.
Return -1 if can't determine the line unambiguously.
Parameters | |
path:str | Undocumented |
section:str | Undocumented |
setting_name:str | Undocumented |
Returns | |
int | Undocumented |
Find a module by name, respecting follow_imports and producing diagnostics.
If the module is not found, then the ModuleNotFound exception is raised.
id: module to find options: the options for the module being loaded caller_state: the state of the importing module, if applicable caller_line: the line number of the import ancestor_for: the child module this is an ancestor of, if applicable root_source: whether this source was specified on the command line skip_diagnose: skip any error diagnosis and reporting (but ModuleNotFound is
still raised if the module is missing)
The specified value of follow_imports for a module can be overridden if the module is specified on the command line or if it is a stub, so we compute and return the "effective" follow_imports of the module.
Returns a tuple containing (file path, target's effective follow_imports setting)
Parameters | |
manager:BuildManager | Undocumented |
id:str | Undocumented |
options:Options | Undocumented |
caller_state:Optional[ | Undocumented |
caller_line:int | Undocumented |
ancestor_for:Optional[ | Undocumented |
root_source:bool | Undocumented |
skip_diagnose:bool | Undocumented |
Returns | |
Tuple[ | Undocumented |
id
or None
if not found.Parameters | |
id:str | Undocumented |
manager:BuildManager | Undocumented |
Returns | |
Optional[ | Undocumented |
id
or the reason it can't be found.Parameters | |
id:str | Undocumented |
manager:BuildManager | Undocumented |
Returns | |
ModuleSearchResult | Undocumented |
Generate fine-grained dependencies into a form suitable for serializing.
This does a couple things: 1. Splits fine-grained deps based on the module of the trigger 2. For each module we generated fine-grained deps for, load any previous
deps and merge them in.
Returns a dictionary from module ids to all dependencies on that module. Dependencies not associated with a module in the build will be associated with the nearest parent module that is in the build, or the fake module FAKE_ROOT_MODULE if none are.
Parameters | |
manager:BuildManager | Undocumented |
graph:Graph | Undocumented |
Returns | |
Dict[ | Undocumented |
Return the file names for the cache files.
Parameters | |
id:str | Undocumented |
path:str | Undocumented |
options:Options | Undocumented |
Returns | |
Tuple[ | Undocumented |
Parameters | |
imp:ImportBase | Undocumented |
toplevel_priority:int | Undocumented |
Returns | |
int | Undocumented |
Check if a missing module can potentially be a part of a package.
This checks if there is any existing parent __init__.pyi stub that defines a module-level __getattr__ (a.k.a. partial stub package).
Parameters | |
id:str | Undocumented |
manager:BuildManager | Undocumented |
Returns | |
bool | Undocumented |
Splits fine-grained dependencies based on the module of the trigger.
Returns a dictionary from module ids to all dependencies on that module. Dependencies not associated with a module in the build will be associated with the nearest parent module that is in the build, or the fake module FAKE_ROOT_MODULE if none are.
Parameters | |
deps:Dict[ | Undocumented |
graph:Graph | Undocumented |
Returns | |
Dict[ | Undocumented |
Given some source files, load the full dependency graph.
If an old_graph is passed in, it is used as the starting point and modified during graph loading.
If a new_modules is passed in, any modules that are loaded are added to the list. This is an argument and not a return value so that the caller can access it even if load_graph fails.
As this may need to parse files, this can raise CompileError in case there are syntax errors.
Parameters | |
sources:List[ | Undocumented |
manager:BuildManager | Undocumented |
old_graph:Optional[ | Undocumented |
new_modules:Optional[ | Undocumented |
Returns | |
Graph | Undocumented |
Load all configured plugins.
Return a plugin that encapsulates all plugins chained together. Always at least include the default plugin (it's last in the chain). The second return value is a snapshot of versions/hashes of loaded user plugins (for cache validation).
Parameters | |
options:Options | Undocumented |
errors:Errors | Undocumented |
stdout:TextIO | Undocumented |
extra_plugins:Sequence[ | Undocumented |
Returns | |
Tuple[ | Undocumented |
Load all configured plugins.
Return a list of all the loaded plugins from the config file. The second return value is a snapshot of versions/hashes of loaded user plugins (for cache validation).
Parameters | |
options:Options | Undocumented |
errors:Errors | Undocumented |
stdout:TextIO | Undocumented |
Returns | |
Tuple[ | Undocumented |
Parameters | |
manager:BuildManager | Undocumented |
sources:List[ | Undocumented |
Undocumented
Parameters | |
manager:BuildManager | Undocumented |
line:int | Undocumented |
caller_state:State | Undocumented |
target:str | Undocumented |
reason:ModuleNotFoundReason | Undocumented |
Convert path to absolute; but to relative in bazel mode.
(Bazel's distributed cache doesn't like filesystem metadata to end up in output files.)
Parameters | |
path:str | Undocumented |
options:Options | Undocumented |
Returns | |
str | Undocumented |
Come up with the ideal processing order within an SCC.
Using the priorities assigned by all_imported_modules_in_file(), try to reduce the cycle to a DAG, by omitting arcs representing dependencies of lower priority.
In the simplest case, if we have A <--> B where A has a top-level "import B" (medium priority) but B only has the reverse "import A" inside a function (low priority), we turn the cycle into a DAG by dropping the B --> A arc, which leaves only A --> B.
If all arcs have the same priority, we fall back to sorting by reverse global order (the order in which modules were first encountered).
The algorithm is recursive, as follows: when as arcs of different priorities are present, drop all arcs of the lowest priority, identify SCCs in the resulting graph, and apply the algorithm to each SCC thus found. The recursion is bounded because at each recursion the spread in priorities is (at least) one less.
In practice there are only a few priority levels (less than a dozen) and in the worst case we just carry out the same algorithm for finding SCCs N times. Thus the complexity is no worse than the complexity of the original SCC-finding algorithm -- see strongly_connected_components() below for a reference.
Parameters | |
graph:Graph | Undocumented |
ascc:AbstractSet[ | Undocumented |
pri_max:int | Undocumented |
Returns | |
List[ | Undocumented |
Process the modules in one group of modules from their cached data.
This can be used to process an SCC of modules This involves loading the tree from JSON and then doing various cleanups.
Parameters | |
graph:Graph | Undocumented |
modules:List[ | Undocumented |
manager:BuildManager | Undocumented |
Parameters | |
graph:Graph | Undocumented |
manager:BuildManager | Undocumented |
Process the modules in one SCC from source code.
Exception: If quick_and_dirty is set, use the cache for fresh modules.
Parameters | |
graph:Graph | Undocumented |
scc:List[ | Undocumented |
manager:BuildManager | Undocumented |
Read and validate the fine-grained dependencies cache.
See the write_deps_cache documentation for more information on the details of the cache.
Returns None if the cache was invalid in some way.
Parameters | |
manager:BuildManager | Undocumented |
graph:Graph | Undocumented |
Returns | |
Optional[ | Undocumented |
Parameters | |
manager:BuildManager | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
options:Options | Undocumented |
stdout:TextIO | Undocumented |
Returns | |
Optional[ | Undocumented |
Write a file containing missing stub packages.
This allows a subsequent "mypy --install-types" run (without other arguments) to install missing stub packages.
Parameters | |
cache_dir:str | Undocumented |
missing_stub_packages:Set[ | Undocumented |
Parameters | |
manager:BuildManager | Undocumented |
id:str | Undocumented |
path:str | Undocumented |
ancestor_for:State | Undocumented |
Parameters | |
manager:BuildManager | Undocumented |
line:int | Undocumented |
caller_state:Optional[ | Undocumented |
id:str | Undocumented |
path:str | Undocumented |
Return the graph's SCCs, topologically sorted by dependencies.
The sort order is from leaves (nodes without dependencies) to roots (nodes on which no other nodes depend).
This works for a subset of the full dependency graph too; dependencies that aren't present in graph.keys() are ignored.
Parameters | |
graph:Graph | Undocumented |
vertices:Optional[ | Undocumented |
pri_max:int | Undocumented |
Returns | |
List[ | Undocumented |
Compute Strongly Connected Components of a directed graph.
From http://code.activestate.com/recipes/578507/.
Parameters | |
vertices:AbstractSet[ | Undocumented |
edges:Dict[ | Undocumented |
Returns | |
Iterator[ | Undocumented |
Take plugin module snapshot by recording its version and hash.
We record _both_ hash and the version to detect more possible changes (e.g. if there is a change in modules imported by a plugin).
Parameters | |
module:types.ModuleType | Undocumented |
Returns | |
str | Undocumented |
Topological sort.
Suppose the input has the following structure:
{A: {B, C}, B: {D}, C: {D}}
This is normalized to:
{A: {B, C}, B: {D}, C: {D}, D: {}}
The algorithm will yield the following values:
{D} {B, C} {A}
From http://code.activestate.com/recipes/577413/.
Parameters | |
data:Dict[ | Undocumented |
Returns | |
Iterable[ | Undocumented |
Checks whether the cached AST of this module can be used.
Parameters | |
meta:Optional[ | Undocumented |
id:str | Undocumented |
path:Optional[ | Undocumented |
ignore_all:bool | Undocumented |
manager:BuildManager | Undocumented |
Returns | |
Optional[ | Undocumented |
Write cache files for a module.
Note that this mypy's behavior is still correct when any given write_cache() call is replaced with a no-op, so error handling code that bails without writing anything is okay.
Parameters | |
id:str | Undocumented |
path:str | Undocumented |
tree:MypyFile | Undocumented |
dependencies:List[ | Undocumented |
suppressed:List[ | Undocumented |
dep_prios:List[ | Undocumented |
dep_lines:List[ | Undocumented |
old_interface_hash:str | Undocumented |
source_hash:str | Undocumented |
ignore_all:bool | Undocumented |
manager:BuildManager | Undocumented |
Returns | |
Tuple[ | Undocumented |
Write cache files for fine-grained dependencies.
Serialize fine-grained dependencies map for fine grained mode.
Dependencies on some module 'm' is stored in the dependency cache file m.deps.json. This entails some spooky action at a distance: if module 'n' depends on 'm', that produces entries in m.deps.json. When there is a dependency on a module that does not exist in the build, it is stored with its first existing parent module. If no such module exists, it is stored with the fake module FAKE_ROOT_MODULE.
This means that the validity of the fine-grained dependency caches are a global property, so we store validity checking information for fine-grained dependencies in a global cache file:
- We take a snapshot of current sources to later check consistency between the fine-grained dependency cache and module cache metadata
- We store the mtime of all of the dependency files to verify they haven't changed
Parameters | |
rdeps:Dict[ | Undocumented |
manager:BuildManager | Undocumented |
graph:Graph | Undocumented |
Parameters | |
manager:BuildManager | Undocumented |
Undocumented
Value |
|
Undocumented
Parameters | |
sources:List[ | Undocumented |
options:Options | Undocumented |
alt_lib_path:Optional[ | Undocumented |
flush_errors:Callable[ | Undocumented |
fscache:Optional[ | Undocumented |
stdout:TextIO | Undocumented |
stderr:TextIO | Undocumented |
extra_plugins:Sequence[ | Undocumented |
Returns | |
BuildResult | Undocumented |
Parameters | |
options:Options | Undocumented |
Returns | |
str | Undocumented |
Parameters | |
file:str | Undocumented |
manager:BuildManager | Undocumented |
log_success:str | Undocumented |
log_error:str | Undocumented |
Returns | |
Optional[ | Undocumented |