How to use the pytype.pytd.visitors function in pytype

To help you get started, weā€™ve selected a few pytype examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / pytype / pytype / pytd / parse / parser_test_base.py View on Github external
def Parse(self, src, name=None, version=None, platform=None):
    version = version or self.PYTHON_VERSION
    tree = parser.parse_string(
        textwrap.dedent(src), name=name, python_version=version,
        platform=platform)
    tree = tree.Visit(visitors.NamedTypeToClassType())
    tree = tree.Visit(visitors.AdjustTypeParameters())
    # Convert back to named types for easier testing
    tree = tree.Visit(visitors.ClassTypeToNamedType())
    tree.Visit(visitors.VerifyVisitor())
    return tree
github google / pytype / pytype / pytd / parse / parser_test_base.py View on Github external
def ToAST(self, src_or_tree):
    if isinstance(src_or_tree, six.string_types):
      # Put into a canonical form (removes comments, standard indents):
      return self.Parse(src_or_tree + "\n")
    else:  # isinstance(src_or_tree, tuple):
      src_or_tree.Visit(visitors.VerifyVisitor())
      return src_or_tree
github google / pytype / pytype / pyi / parser.py View on Github external
def _type_macro(self, base_type, template, parameters):
    if parameters is None:
      mapping = {t: pytd.AnythingType() for t in template}
    elif len(template) != len(parameters):
      raise ParseError("%s expected %d parameters, got %s" % (
          pytd_utils.Print(base_type), len(template), len(parameters)))
    else:
      mapping = dict(zip(template, parameters))
    return base_type.Visit(visitors.ReplaceTypes(mapping))
github google / pytype / pytype / pytd / serialize_ast.py View on Github external
def FillLocalReferences(serializable_ast, module_map):
  """Fill in local references."""
  local_filler = visitors.FillInLocalPointers(module_map)
  if (serializable_ast.class_type_nodes is None or
      serializable_ast.function_type_nodes is None):
    serializable_ast.ast.Visit(local_filler)
    return serializable_ast.Replace(
        class_type_nodes=None, function_type_nodes=None)
  else:
    for node in serializable_ast.class_type_nodes:
      local_filler.EnterClassType(node)
      if node.cls is None:
        raise AssertionError("This should not happen: %s" % str(node))
    for node in serializable_ast.function_type_nodes:
      local_filler.EnterFunctionType(node)
      if node.function is None:
        raise AssertionError("This should not happen: %s" % str(node))
    return serializable_ast
github google / pytype / pytype / pyi / parser.py View on Github external
text = None
        raise ParseError(utils.message(e), line=line, filename=self._filename,
                         column=self._error_location[1], text=text)
      else:
        raise e

    ast = ast.Visit(_PropertyToConstant())
    ast = ast.Visit(_InsertTypeParameters(ast.type_params))
    ast = ast.Visit(_VerifyMutators())
    # TODO(kramm): This is in the wrong place- it should happen after resolving
    # local names, in load_pytd.
    ast = ast.Visit(pep484.ConvertTypingToNative(name))

    if name:
      ast = ast.Replace(name=name)
      ast = ast.Visit(visitors.AddNamePrefix())
    else:
      # If there's no unique name, hash the sourcecode.
      ast = ast.Replace(name=hashlib.md5(src.encode("utf-8")).hexdigest())
    ast = ast.Visit(visitors.StripExternalNamePrefix())

    # Typeshed files that explicitly import and refer to "builtins" need to have
    # that rewritten to __builtin__
    return ast.Visit(visitors.RenameBuiltinsPrefix())
github google / pytype / pytype / convert_structural.py View on Github external
def match_call_record(self, matcher, solver, call_record, complete):
    """Match the record of a method call against the formal signature."""
    assert is_partial(call_record)
    assert is_complete(complete)
    formula = (
        matcher.match_Function_against_Function(call_record, complete, {}))
    if formula is booleq.FALSE:
      cartesian = call_record.Visit(visitors.ExpandSignatures())
      for signature in cartesian.signatures:
        formula = matcher.match_Signature_against_Function(
            signature, complete, {})
        if formula is booleq.FALSE:
          faulty_signature = pytd_utils.Print(signature)
          break
      else:
        faulty_signature = ""
      raise FlawedQuery("Bad call\n%s%s\nagainst:\n%s" % (
          type_match.unpack_name_of_partial(call_record.name),
          faulty_signature, pytd_utils.Print(complete)))
    solver.always_true(formula)
github google / pytype / pytype / pytd / optimize.py View on Github external
def VisitClass(self, cls):
    """Add superclass methods and constants to this Class."""
    if any(base for base in cls.parents if isinstance(base, pytd.NamedType)):
      raise AssertionError("AddInheritedMethods needs a resolved AST")
    # Filter out only the types we can reason about.
    # TODO(kramm): Do we want handle UnionTypes and GenericTypes at some point?
    bases = [base.cls
             for base in cls.parents
             if isinstance(base, pytd.ClassType)]
    # Don't pull in methods that are named the same as existing methods in
    # this class, local methods override parent class methods.
    names = {m.name for m in cls.methods} | {c.name for c in cls.constants}
    # TODO(kramm): This should do full-blown MRO.
    adjust_self = visitors.AdjustSelf(force=True)
    adjust_self.class_types.append(visitors.ClassAsType(cls))
    new_methods = list(cls.methods)
    for base in bases:
      for m in base.methods:
        if m.name not in names:
          new_methods.append(m.Visit(adjust_self))
    new_constants = list(cls.constants)
    for base in bases:
      for c in base.constants:
        if c.name not in names:
          new_constants.append(c)
    return cls.Replace(methods=tuple(new_methods),
                       constants=tuple(new_constants))
github google / pytype / pytype / load_pytd.py View on Github external
def _verify_pyi(self, pyval, ast_name=None):
    try:
      pyval.Visit(visitors.VerifyLookup(ignore_late_types=True))
    except ValueError as e:
      raise BadDependencyError(utils.message(e), ast_name or pyval.name)
    pyval.Visit(visitors.VerifyContainers())
github google / pytype / pytype / output.py View on Github external
types = []
    for val in options:
      if isinstance(val, abstract.InterpreterFunction):
        combinations = val.get_call_combinations(node)
        for sig, node_after, _, return_value in combinations:
          types.append(self._function_call_to_return_type(
              sig, node_after, val, return_value, len(combinations)))
      elif isinstance(val, abstract.PyTDFunction):
        types.extend(sig.pytd_sig.return_type for sig in val.signatures)
      else:
        types.append(pytd.AnythingType())
    safe_types = []  # types without type parameters
    for t in types:
      collector = visitors.CollectTypeParameters()
      t.Visit(collector)
      t = t.Visit(visitors.ReplaceTypeParameters(
          {p: p.upper_value for p in collector.params}))
      safe_types.append(t)
    return safe_types