module documentation

Functions for optimizing pytd syntax trees. pytd files come from various sources, and are typically redundant (duplicate functions, different signatures saying the same thing, overlong type disjunctions). The Visitors in this file remove various forms of these redundancies.

Class AbsorbMutableParameters Converts mutable parameters to unions. This is lossy.
Class AddInheritedMethods Copy methods and constants from base classes into their derived classes.
Class AdjustGenericType Changes the generic type from "object" to "Any".
Class AdjustReturnAndConstantGenericType Changes "object" to "Any" in return and constant types.
Class CollapseLongUnions Shortens long unions to object (or "?").
Class CombineContainers Change unions of containers to containers of unions.
Class CombineReturnsAndExceptions Group function signatures that only differ in exceptions or return values.
Class FindCommonSuperClasses Find common super classes. Optionally also uses abstract base classes.
Class MergeTypeParameters Remove all function type parameters in a union with a class type param.
Class NormalizeGenericSelfTypes Removes unwanted parameter types from the 'self' parameter.
Class PullInMethodClasses Simplifies classes with only a __call__ function to just a method.
Class RemoveDuplicates Remove duplicate function signatures.
Class RenameUnknowns Give unknowns that map to the same set of concrete types the same name.
Class SimplifyContainers Simplifies containers whose type parameters are all Any.
Class SimplifyUnions Remove duplicate or redundant entries in union types.
Class SimplifyUnionsWithSuperclasses Simplify Unions with superclasses.
Class SuperClassHierarchy Utility class for optimizations working with superclasses.
Class TypeParameterScope Common superclass for optimizations that track type parameters.
Function Optimize Optimize a PYTD tree.
Variable log Undocumented
Class _ReturnsAndExceptions Mutable class for collecting return types and exceptions of functions.
def Optimize(node, builtins=None, lossy=False, use_abcs=False, max_union=7, remove_mutable=False, can_do_lookup=True): (source)

Optimize a PYTD tree. Tries to shrink a PYTD tree by applying various optimizations. Arguments: node: A pytd node to be optimized. It won't be modified - this function will return a new node. builtins: Definitions of all of the external types in node. lossy: Allow optimizations that change the meaning of the pytd. use_abcs: Use abstract base classes to represent unions like e.g. "Union[float, int]" as "Real". max_union: How many types we allow in a union before we simplify it to just "object". remove_mutable: Whether to simplify mutable parameters to normal parameters. can_do_lookup: True: We're either allowed to try to resolve NamedType instances in the AST, or the AST is already resolved. False: Skip any optimizations that would require NamedTypes to be resolved. Returns: An optimized node.

Undocumented