class documentation

class SuggestionEngine: (source)

View In Hierarchy

Engine for finding call sites and suggesting signatures.
Method __init__ Undocumented
Method add​_adjustments Undocumented
Method ensure​_loaded Make sure that the module represented by state is fully loaded.
Method extract​_from​_decorator Undocumented
Method filter​_options Apply any configured filters to the possible guesses.
Method find​_best From a list of possible function types, find the best one.
Method find​_node From a target name, return module/target names and the func def.
Method find​_node​_by​_file​_and​_line Find symbol node by path to file and line number.
Method find​_node​_by​_module​_and​_name Find symbol node by module id and qualified name.
Method format​_args Undocumented
Method format​_signature Format a callable type in a way suitable as an annotation... kind of
Method format​_type Undocumented
Method get​_args Produce a list of type suggestions for each argument type.
Method get​_callsites Find all call sites of a function.
Method get​_default​_arg​_types Undocumented
Method get​_guesses Compute a list of guesses for a function's type.
Method get​_guesses​_from​_parent Try to get a guess of a method type from a parent class.
Method get​_starting​_type Undocumented
Method get​_suggestion Compute a suggestion for a function.
Method get​_trivial​_type Generate a trivial callable type from a func def, with all Anys
Method json​_suggestion Produce a json blob for a suggestion suitable for application by pyannotate.
Method named​_type Undocumented
Method pyannotate​_signature Format a callable type as a pyannotate dict
Method reload Recheck the module given by state.
Method restore​_after Context manager that reloads a module after executing the body.
Method score​_callable Undocumented
Method score​_type Generate a score for a type that we use to pick which type to use.
Method suggest Suggest an inferred type for function.
Method suggest​_callsites Find a list of call sites of function.
Method try​_type Recheck a function while assuming it has type typ.
Method with​_export​_types Context manager that enables the export_types flag in the body.
Instance Variable fgmanager Undocumented
Instance Variable finder Undocumented
Instance Variable flex​_any Undocumented
Instance Variable give​_json Undocumented
Instance Variable graph Undocumented
Instance Variable manager Undocumented
Instance Variable max​_guesses Undocumented
Instance Variable no​_errors Undocumented
Instance Variable plugin Undocumented
Instance Variable try​_text Undocumented
Instance Variable use​_fixme Undocumented
def __init__(self, fgmanager, *, json, no_errors=False, no_any=False, try_text=False, flex_any=None, use_fixme=None, max_guesses=None): (source)

Undocumented

Parameters
fgmanager:FineGrainedBuildManagerUndocumented
json:boolUndocumented
no​_errors:boolUndocumented
no​_any:boolUndocumented
try​_text:boolUndocumented
flex​_any:Optional[float]Undocumented
use​_fixme:Optional[str]Undocumented
max​_guesses:Optional[int]Undocumented
def add_adjustments(self, typs): (source)

Undocumented

Parameters
typs:List[Type]Undocumented
Returns
List[Type]Undocumented
def ensure_loaded(self, state, force=False): (source)
Make sure that the module represented by state is fully loaded.
Parameters
state:StateUndocumented
force:boolUndocumented
Returns
MypyFileUndocumented
def extract_from_decorator(self, node): (source)

Undocumented

Parameters
node:DecoratorUndocumented
Returns
Optional[FuncDef]Undocumented
def filter_options(self, guesses, is_method, ignore_return): (source)

Apply any configured filters to the possible guesses.

Currently the only option is filtering based on Any prevalance.

Parameters
guesses:List[CallableType]Undocumented
is​_method:boolUndocumented
ignore​_return:boolUndocumented
Returns
List[CallableType]Undocumented
def find_best(self, func, guesses): (source)

From a list of possible function types, find the best one.

For best, we want the fewest errors, then the best "score" from score_callable.

Parameters
func:FuncDefUndocumented
guesses:List[CallableType]Undocumented
Returns
Tuple[CallableType, int]Undocumented
def find_node(self, key): (source)

From a target name, return module/target names and the func def.

The 'key' argument can be in one of two formats: * As the function full name, e.g., package.module.Cls.method * As the function location as file and line separated by column,

e.g., path/to/file.py:42
Parameters
key:strUndocumented
Returns
Tuple[str, str, FuncDef]Undocumented
def find_node_by_file_and_line(self, file, line): (source)

Find symbol node by path to file and line number.

Find the first function declared before or on the line number.

Return module id and the node found. Raise SuggestionFailure if can't find one.

Parameters
file:strUndocumented
line:intUndocumented
Returns
Tuple[str, SymbolNode]Undocumented
def find_node_by_module_and_name(self, modname, tail): (source)

Find symbol node by module id and qualified name.

Raise SuggestionFailure if can't find one.

Parameters
modname:strUndocumented
tail:strUndocumented
Returns
Optional[SymbolNode]Undocumented
def format_args(self, arg_kinds, arg_names, arg_types): (source)

Undocumented

Parameters
arg​_kinds:List[List[ArgKind]]Undocumented
arg​_names:List[List[Optional[str]]]Undocumented
arg​_types:List[List[Type]]Undocumented
Returns
strUndocumented
def format_signature(self, sig): (source)
Format a callable type in a way suitable as an annotation... kind of
Parameters
sig:PyAnnotateSignatureUndocumented
Returns
strUndocumented
def format_type(self, cur_module, typ): (source)

Undocumented

Parameters
cur​_module:Optional[str]Undocumented
typ:TypeUndocumented
Returns
strUndocumented
def get_args(self, is_method, base, defaults, callsites, uses): (source)
Produce a list of type suggestions for each argument type.
Parameters
is​_method:boolUndocumented
base:CallableTypeUndocumented
defaults:List[Optional[Type]]Undocumented
callsites:List[Callsite]Undocumented
uses:List[List[Type]]Undocumented
Returns
List[List[Type]]Undocumented
def get_callsites(self, func): (source)
Find all call sites of a function.
Parameters
func:FuncDefUndocumented
Returns
Tuple[List[Callsite], List[str]]Undocumented
def get_default_arg_types(self, state, fdef): (source)

Undocumented

Parameters
state:StateUndocumented
fdef:FuncDefUndocumented
Returns
List[Optional[Type]]Undocumented
def get_guesses(self, is_method, base, defaults, callsites, uses): (source)

Compute a list of guesses for a function's type.

This focuses just on the argument types, and doesn't change the provided return type.

Parameters
is​_method:boolUndocumented
base:CallableTypeUndocumented
defaults:List[Optional[Type]]Undocumented
callsites:List[Callsite]Undocumented
uses:List[List[Type]]Undocumented
Returns
List[CallableType]Undocumented
def get_guesses_from_parent(self, node): (source)
Try to get a guess of a method type from a parent class.
Parameters
node:FuncDefUndocumented
Returns
List[CallableType]Undocumented
def get_starting_type(self, fdef): (source)

Undocumented

Parameters
fdef:FuncDefUndocumented
Returns
CallableTypeUndocumented
def get_suggestion(self, mod, node): (source)

Compute a suggestion for a function.

Return the type and whether the first argument should be ignored.

Parameters
mod:strUndocumented
node:FuncDefUndocumented
Returns
PyAnnotateSignatureUndocumented
def get_trivial_type(self, fdef): (source)
Generate a trivial callable type from a func def, with all Anys
Parameters
fdef:FuncDefUndocumented
Returns
CallableTypeUndocumented
def json_suggestion(self, mod, func_name, node, suggestion): (source)
Produce a json blob for a suggestion suitable for application by pyannotate.
Parameters
mod:strUndocumented
func​_name:strUndocumented
node:FuncDefUndocumented
suggestion:PyAnnotateSignatureUndocumented
Returns
strUndocumented
def named_type(self, s): (source)

Undocumented

Parameters
s:strUndocumented
Returns
InstanceUndocumented
def pyannotate_signature(self, cur_module, is_method, typ): (source)
Format a callable type as a pyannotate dict
Parameters
cur​_module:Optional[str]Undocumented
is​_method:boolUndocumented
typ:CallableTypeUndocumented
Returns
PyAnnotateSignatureUndocumented
def reload(self, state, check_errors=False): (source)

Recheck the module given by state.

If check_errors is true, raise an exception if there are errors.

Parameters
state:StateUndocumented
check​_errors:boolUndocumented
Returns
List[str]Undocumented
@contextmanager
def restore_after(self, module): (source)

Context manager that reloads a module after executing the body.

This should undo any damage done to the module state while mucking around.

Parameters
module:strUndocumented
Returns
Iterator[None]Undocumented
def score_callable(self, t): (source)

Undocumented

Parameters
t:CallableTypeUndocumented
Returns
intUndocumented
def score_type(self, t, arg_pos): (source)

Generate a score for a type that we use to pick which type to use.

Lower is better, prefer non-union/non-any types. Don't penalize optionals.

Parameters
t:TypeUndocumented
arg​_pos:boolUndocumented
Returns
intUndocumented
def suggest(self, function): (source)
Suggest an inferred type for function.
Parameters
function:strUndocumented
Returns
strUndocumented
def suggest_callsites(self, function): (source)
Find a list of call sites of function.
Parameters
function:strUndocumented
Returns
strUndocumented
def try_type(self, func, typ): (source)

Recheck a function while assuming it has type typ.

Return all error messages.

Parameters
func:FuncDefUndocumented
typ:ProperTypeUndocumented
Returns
List[str]Undocumented
@contextmanager
def with_export_types(self): (source)

Context manager that enables the export_types flag in the body.

This causes type information to be exported into the manager's all_types variable.

Returns
Iterator[None]Undocumented
fgmanager = (source)

Undocumented

finder = (source)

Undocumented

flex_any: float = (source)

Undocumented

give_json = (source)

Undocumented

graph = (source)

Undocumented

manager = (source)

Undocumented

max_guesses = (source)

Undocumented

no_errors = (source)

Undocumented

plugin = (source)

Undocumented

try_text = (source)

Undocumented

use_fixme = (source)

Undocumented