module documentation

Mechanisms for inferring function types based on callsites. Currently works by collecting all argument types at callsites, synthesizing a list of possible function types from that, trying them all, and picking the one with the fewest errors that we think is the "best". Can return JSON that pyannotate can use to apply the annotations to code. There are a bunch of TODOs here: * Maybe want a way to surface the choices not selected?? * We can generate an exponential number of type suggestions, and probably want a way to not always need to check them all. * Our heuristics for what types to try are primitive and not yet supported by real practice. * More! Other things: * This is super brute force. Could we integrate with the typechecker more to understand more about what is going on? * Like something with tracking constraints/unification variables? * No understanding of type variables at *all*

Class ArgUseFinder Visitor for finding all the types of arguments that each arg is passed to.
Class Callsite Undocumented
Class MakeSuggestionAny Undocumented
Class PyAnnotateSignature Undocumented
Class ReturnFinder Visitor for finding all types returned from a function.
Class SuggestionEngine Engine for finding call sites and suggesting signatures.
Class SuggestionPlugin Plugin that records all calls to a given target.
Class TypeFormatter Visitor used to format types
Exception SuggestionFailure Undocumented
Function any_score_callable Undocumented
Function any_score_type Generate a very made up number representing the Anyness of a type.
Function count_errors Undocumented
Function dedup Undocumented
Function generate_type_combinations Generate possible combinations of a list of types.
Function get_arg_uses Find all the types of arguments that each arg is passed to.
Function get_return_types Find all the types returned by return statements in func.
Function is_explicit_any Undocumented
Function is_implicit_any Undocumented
Function is_tricky_callable Is t a callable that we need to put a ... in for syntax reasons?
Function make_suggestion_anys Make all anys in the type as coming from the suggestion engine.
Function refine_callable Refine a callable based on another.
Function refine_type Refine `ti` by replacing Anys in it with information taken from `si`
Function refine_union Refine a union type based on another type.
Type Variable T Undocumented
Type Variable TType Undocumented
def any_score_callable(t: CallableType, is_method: bool, ignore_return: bool) -> float: (source)

Undocumented

def any_score_type(ut: Type, arg_pos: bool) -> float: (source)

Generate a very made up number representing the Anyness of a type. Higher is better, 1.0 is max

def count_errors(msgs: list[str]) -> int: (source)

Undocumented

def dedup(old: list[T]) -> list[T]: (source)

Undocumented

def generate_type_combinations(types: list[Type]) -> list[Type]: (source)

Generate possible combinations of a list of types. mypy essentially supports two different ways to do this: joining the types and unioning the types. We try both.

def get_arg_uses(typemap: dict[Expression, Type], func: FuncDef) -> list[list[Type]]: (source)

Find all the types of arguments that each arg is passed to. For example, given def foo(x: int) -> None: ... def bar(x: str) -> None: ... def test(x, y): foo(x) bar(y) this will return [[int], [str]].

def get_return_types(typemap: dict[Expression, Type], func: FuncDef) -> list[Type]: (source)

Find all the types returned by return statements in func.

def is_explicit_any(typ: AnyType) -> bool: (source)

Undocumented

def is_implicit_any(typ: Type) -> bool: (source)

Undocumented

def is_tricky_callable(t: CallableType) -> bool: (source)

Is t a callable that we need to put a ... in for syntax reasons?

def make_suggestion_anys(t: TType) -> TType: (source)

Make all anys in the type as coming from the suggestion engine. This keeps those Anys from influencing constraint generation, which allows us to do better when refining types.

def refine_callable(t: CallableType, s: CallableType) -> CallableType: (source)

Refine a callable based on another. See comments for refine_type.

def refine_type(ti: Type, si: Type) -> Type: (source)

Refine `ti` by replacing Anys in it with information taken from `si` This basically works by, when the types have the same structure, traversing both of them in parallel and replacing Any on the left with whatever the type on the right is. If the types don't have the same structure (or aren't supported), the left type is chosen. For example: refine(Any, T) = T, for all T refine(float, int) = float refine(List[Any], List[int]) = List[int] refine(Dict[int, Any], Dict[Any, int]) = Dict[int, int] refine(Tuple[int, Any], Tuple[Any, int]) = Tuple[int, int] refine(Callable[[Any], Any], Callable[[int], int]) = Callable[[int], int] refine(Callable[..., int], Callable[[int, float], Any]) = Callable[[int, float], int] refine(Optional[Any], int) = Optional[int] refine(Optional[Any], Optional[int]) = Optional[int] refine(Optional[Any], Union[int, str]) = Optional[Union[int, str]] refine(Optional[List[Any]], List[int]) = List[int]

def refine_union(t: UnionType, s: ProperType) -> Type: (source)

Refine a union type based on another type. This is done by refining every component of the union against the right hand side type (or every component of its union if it is one). If an element of the union is successfully refined, we drop it from the union in favor of the refined versions.

Undocumented

Value
TypeVar('T')

Undocumented

Value
TypeVar('TType',
        bound=Type)