def Optimize(node, builtins=None, lossy=False, use_abcs=False, max_union=7, remove_mutable=False, can_do_lookup=True):
    'Optimize a PYTD tree.\n\n  Tries to shrink a PYTD tree by applying various optimizations.\n\n  Arguments:\n    node: A pytd node to be optimized. It won\'t be modified - this function\n        will return a new node.\n    builtins: Definitions of all of the external types in node.\n    lossy: Allow optimizations that change the meaning of the pytd.\n    use_abcs: Use abstract base classes to represent unions like\n        e.g. "float or int" as "Real".\n    max_union: How many types we allow in a union before we simplify\n        it to just "object".\n    remove_mutable: Whether to simplify mutable parameters to normal\n        parameters.\n    can_do_lookup: True: We\'re either allowed to try to resolve NamedType\n        instances in the AST, or the AST is already resolved. False: Skip any\n        optimizations that would require NamedTypes to be resolved.\n\n  Returns:\n    An optimized node.\n  '
    node = node.Visit(RemoveDuplicates())
    node = node.Visit(SimplifyUnions())
    node = node.Visit(CombineReturnsAndExceptions())
    node = node.Visit(Factorize())
    node = node.Visit(ApplyOptionalArguments())
    node = node.Visit(CombineContainers())
    node = node.Visit(SimplifyContainers())
    if builtins:
        superclasses = builtins.Visit(visitors.ExtractSuperClassesByName())
        superclasses.update(node.Visit(visitors.ExtractSuperClassesByName()))
        if use_abcs:
            superclasses.update(abc_hierarchy.GetSuperClasses())
        hierarchy = SuperClassHierarchy(superclasses)
        node = node.Visit(SimplifyUnionsWithSuperclasses(hierarchy))
        if lossy:
            node = node.Visit(FindCommonSuperClasses(hierarchy))
    if max_union:
        node = node.Visit(CollapseLongUnions(max_union))
    node = node.Visit(AdjustReturnAndConstantGenericType())
    if remove_mutable:
        node = node.Visit(AbsorbMutableParameters())
        node = node.Visit(CombineContainers())
        node = node.Visit(MergeTypeParameters())
        node = node.Visit(visitors.AdjustSelf())
    node = node.Visit(SimplifyContainers())
    if (builtins and can_do_lookup):
        node = visitors.LookupClasses(node, builtins, ignore_late_types=True)
        node = node.Visit(RemoveInheritedMethods())
        node = node.Visit(RemoveRedundantSignatures(hierarchy))
    return node
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. "float or 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.
def Optimize(node, builtins=None, lossy=False, use_abcs=False, max_union=7, remove_mutable=False, can_do_lookup=True):
    'Optimize a PYTD tree.\n\n  Tries to shrink a PYTD tree by applying various optimizations.\n\n  Arguments:\n    node: A pytd node to be optimized. It won\'t be modified - this function\n        will return a new node.\n    builtins: Definitions of all of the external types in node.\n    lossy: Allow optimizations that change the meaning of the pytd.\n    use_abcs: Use abstract base classes to represent unions like\n        e.g. "float or int" as "Real".\n    max_union: How many types we allow in a union before we simplify\n        it to just "object".\n    remove_mutable: Whether to simplify mutable parameters to normal\n        parameters.\n    can_do_lookup: True: We\'re either allowed to try to resolve NamedType\n        instances in the AST, or the AST is already resolved. False: Skip any\n        optimizations that would require NamedTypes to be resolved.\n\n  Returns:\n    An optimized node.\n  '
    node = node.Visit(RemoveDuplicates())
    node = node.Visit(SimplifyUnions())
    node = node.Visit(CombineReturnsAndExceptions())
    node = node.Visit(Factorize())
    node = node.Visit(ApplyOptionalArguments())
    node = node.Visit(CombineContainers())
    node = node.Visit(SimplifyContainers())
    if builtins:
        superclasses = builtins.Visit(visitors.ExtractSuperClassesByName())
        superclasses.update(node.Visit(visitors.ExtractSuperClassesByName()))
        if use_abcs:
            superclasses.update(abc_hierarchy.GetSuperClasses())
        hierarchy = SuperClassHierarchy(superclasses)
        node = node.Visit(SimplifyUnionsWithSuperclasses(hierarchy))
        if lossy:
            node = node.Visit(FindCommonSuperClasses(hierarchy))
    if max_union:
        node = node.Visit(CollapseLongUnions(max_union))
    node = node.Visit(AdjustReturnAndConstantGenericType())
    if remove_mutable:
        node = node.Visit(AbsorbMutableParameters())
        node = node.Visit(CombineContainers())
        node = node.Visit(MergeTypeParameters())
        node = node.Visit(visitors.AdjustSelf())
    node = node.Visit(SimplifyContainers())
    if (builtins and can_do_lookup):
        node = visitors.LookupClasses(node, builtins, ignore_late_types=True)
        node = node.Visit(RemoveInheritedMethods())
        node = node.Visit(RemoveRedundantSignatures(hierarchy))
    return node