How to use the pytype.pytd.pytd.GenericType 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 / pep484.py View on Github external
def _GetModuleAndName(self, t):
    if isinstance(t, pytd.GenericType):
      return self._GetModuleAndName(t.base_type)
    elif isinstance(t, (pytd.ClassType, pytd.NamedType)) and "." in t.name:
      return t.name.rsplit(".", 1)
    else:
      return None, t.name
github google / pytype / pytype / pyi / parser.py View on Github external
body: ?

    Returns:
      A _NameAndSig object.

    Raises:
      ParseError: if any validity checks fail.
    """
    if name == "__init__" and isinstance(return_type, pytd.AnythingType):
      ret = pytd.NamedType("NoneType")
    elif is_async:
      base = pytd.NamedType("typing.Coroutine")
      params = (pytd.NamedType("typing.Any"),
                pytd.NamedType("typing.Any"),
                return_type)
      ret = pytd.GenericType(base, params)
    else:
      ret = return_type
    params = _validate_params(param_list)

    exceptions = []
    mutators = []
    for stmt in body:
      if isinstance(stmt, pytd.Type):
        exceptions.append(stmt)  # raise stmt
        continue
      assert isinstance(stmt, tuple) and len(stmt) == 2, stmt
      mutators.append(_Mutator(stmt[0], stmt[1]))

    signature = pytd.Signature(params=tuple(params.required), return_type=ret,
                               starargs=params.starargs,
                               starstarargs=params.starstarargs,
github google / pytype / pytype / convert.py View on Github external
return self.unsolvable
        cls = actual.cls
      if isinstance(cls, pytd.ClassType):
        cls = cls.cls
      if (isinstance(cls, pytd.GenericType) and
          cls.base_type.name == "typing.ClassVar"):
        param, = cls.parameters
        return self.constant_to_value(
            abstract_utils.AsInstance(param), subst, self.vm.root_cfg_node)
      elif isinstance(cls, pytd.GenericType) or (isinstance(cls, pytd.Class) and
                                                 cls.template):
        # If we're converting a generic Class, need to create a new instance of
        # it. See test_classes.testGenericReinstantiated.
        if isinstance(cls, pytd.Class):
          params = tuple(t.type_param.upper_value for t in cls.template)
          cls = pytd.GenericType(base_type=pytd.ClassType(cls.name, cls),
                                 parameters=params)
        if isinstance(cls.base_type, pytd.LateType):
          actual = self._load_late_type(cls.base_type)
          if not isinstance(actual, pytd.ClassType):
            return self.unsolvable
          base_cls = actual.cls
        else:
          assert isinstance(cls.base_type, pytd.ClassType)
          base_cls = cls.base_type.cls
        assert isinstance(base_cls, pytd.Class), base_cls
        if base_cls.name == "__builtin__.type":
          c, = cls.parameters
          if isinstance(c, pytd.TypeParameter):
            if not subst or c.full_name not in subst:
              raise self.TypeParameterError(c.full_name)
            return self.merge_classes(subst[c.full_name].data)
github google / pytype / pytype / pytd / parse / parser.py View on Github external
def p_param_kw_type(self, p):
    """param : ASTERISK ASTERISK NAME COLON type"""
    _, _, _, name, _, t = p
    p[0] = pytd.Parameter(
        "**" + name,
        pytd.GenericType(pytd.NamedType("dict"), (pytd.NamedType("str"), t)),
        False, True, None)
github google / pytype / pytype / pytd / type_match.py View on Github external
def match_Function_against_Class(self, f1, cls2, subst, cache):
    cls2_methods = cache.get(id(cls2))
    if cls2_methods is None:
      cls2_methods = cache[id(cls2)] = {f.name: f for f in cls2.methods}
    if f1.name not in cls2_methods:
      # The class itself doesn't have this method, but base classes might.
      # TODO(kramm): This should do MRO order, not depth-first.
      for base in cls2.parents:
        if isinstance(base, pytd.AnythingType):
          # AnythingType can contain any method. However, that would mean that
          # a class that inherits from AnythingType contains any method
          # imaginable, and hence is a match for anything. To prevent the bad
          # results caused by that, return FALSE here.
          return booleq.FALSE
        elif isinstance(base, (pytd.ClassType, pytd.GenericType)):
          if isinstance(base, pytd.ClassType):
            cls = base.cls
            values = tuple(pytd.AnythingType() for _ in cls.template)
          elif isinstance(base, pytd.TupleType):
            cls = base.base_type.cls
            values = (pytd.UnionType(type_list=base.parameters),)
          else:
            cls = base.base_type.cls
            values = base.parameters
          if values:
            subst = subst.copy()
            for param, value in zip(cls.template, values):
              subst[param.type_param] = value
          implication = self.match_Function_against_Class(f1, cls, subst, cache)
          if implication is not booleq.FALSE:
            return implication
github google / pytype / pytype / pyi / parser.py View on Github external
def _make_type_type(value):
  return pytd.GenericType(pytd.NamedType("type"), (value,))
github google / pytype / pytype / pyi / parser.py View on Github external
def _starstar_param(name, param_type):
  """Return a pytd.Parameter for a **kwargs argument."""
  if param_type is None:
    param_type = pytd.NamedType("dict")
  else:
    param_type = pytd.GenericType(
        pytd.NamedType("dict"), (pytd.NamedType("str"), param_type))
  return pytd.Parameter(name, param_type, False, True, None)
github google / pytype / pytype / pyi / parser.py View on Github external
def _heterogeneous_tuple(self, base_type, parameters):
    if parameters:
      return pytd.TupleType(base_type=base_type, parameters=parameters)
    else:
      return pytd.GenericType(base_type=base_type,
                              parameters=(pytd.NothingType(),))
github google / pytype / pytype / pytd / mro.py View on Github external
mros[t] = None
      parent_mros = []
      for parent in _GetClass(t, lookup_ast).parents:
        if parent in mros:
          if mros[parent] is None:
            raise MROError([[t]])
          else:
            parent_mro = mros[parent]
        else:
          parent_mro = _ComputeMRO(parent, mros, lookup_ast)
        parent_mros.append(parent_mro)
      mros[t] = tuple(
          MROMerge([[t]] + parent_mros + [_Degenerify(
              _GetClass(t, lookup_ast).parents)]))
    return mros[t]
  elif isinstance(t, pytd.GenericType):
    return _ComputeMRO(t.base_type, mros, lookup_ast)
  else:
    return [t]