How to use the pytype.pytd.pytd.Parameter 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.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 / output.py View on Github external
def get_parameter(p, kwonly):
      return pytd.Parameter(p, sig.annotations[p].get_instance_type(node),
                            kwonly, p in sig.defaults, None)
    params = [get_parameter(p, False) for p in sig.param_names]
github google / pytype / pytype / output.py View on Github external
"""Convert a SimpleFunction to a PyTD definition."""
    sig = v.signature
    def get_parameter(p, kwonly):
      return pytd.Parameter(p, sig.annotations[p].get_instance_type(node),
                            kwonly, p in sig.defaults, None)
    params = [get_parameter(p, False) for p in sig.param_names]
    kwonly = [get_parameter(p, True) for p in sig.kwonly_params]
    if sig.varargs_name:
      star = pytd.Parameter(
          sig.varargs_name,
          sig.annotations[sig.varargs_name].get_instance_type(node),
          False, False, None)
    else:
      star = None
    if sig.kwargs_name:
      starstar = pytd.Parameter(
          sig.kwargs_name,
          sig.annotations[sig.kwargs_name].get_instance_type(node),
          False, False, None)
    else:
      starstar = None
    if sig.has_return_annotation:
      ret_type = sig.annotations["return"].get_instance_type(node)
    else:
      ret_type = pytd.NamedType("__builtin__.NoneType")
    pytd_sig = pytd.Signature(
        params=tuple(params+kwonly),
        starargs=star,
        starstarargs=starstar,
        return_type=ret_type,
        exceptions=(),
        template=())
github google / pytype / pytype / analyze.py View on Github external
funcs = collections.defaultdict(pytd_utils.OrderedSet)
    for node, func, sigs, args, kws, retvar in call_traces:
      # The lengths may be different in the presence of optional and kw args.
      arg_names = max((sig.get_positional_names() for sig in sigs), key=len)
      for i in range(len(arg_names)):
        if not isinstance(func.data, abstract.BoundFunction) or i > 0:
          arg_names[i] = function.argname(i)
      arg_types = (a.data.to_type(node) for a in args)
      ret = pytd_utils.JoinTypes(t.to_type(node) for t in retvar.data)
      # TODO(kramm): Record these:
      starargs = None
      starstarargs = None
      funcs[func.data.name].add(pytd.Signature(
          tuple(pytd.Parameter(n, t, False, False, None)
                for n, t in zip(arg_names, arg_types)) +
          tuple(pytd.Parameter(name, a.data.to_type(node), False, False, None)
                for name, a in kws),
          starargs, starstarargs,
          ret, exceptions=(), template=()))
    functions = []
    for name, signatures in funcs.items():
      functions.append(pytd.Function(name_transform(name), tuple(signatures),
                                     pytd.METHOD))
    return functions
github google / pytype / pytype / function.py View on Github external
Self with an updated signature.
    """
    defaults = list(defaults)
    params = []
    for param in reversed(self.pytd_sig.params):
      if defaults:
        defaults.pop()  # Discard the default. Unless we want to update type?
        params.append(pytd.Parameter(
            name=param.name,
            type=param.type,
            kwonly=param.kwonly,
            optional=True,
            mutated_type=param.mutated_type
        ))
      else:
        params.append(pytd.Parameter(
            name=param.name,
            type=param.type,
            kwonly=param.kwonly,
            optional=False,  # Reset any previously-set defaults
            mutated_type=param.mutated_type
        ))
    new_sig = pytd.Signature(
        params=tuple(reversed(params)),
        starargs=self.pytd_sig.starargs,
        starstarargs=self.pytd_sig.starstarargs,
        return_type=self.pytd_sig.return_type,
        exceptions=self.pytd_sig.exceptions,
        template=self.pytd_sig.template
    )
    # Now update self
    self.pytd_sig = new_sig
github google / pytype / pytype / analyze.py View on Github external
def _call_traces_to_function(call_traces, name_transform=lambda x: x):
    funcs = collections.defaultdict(pytd_utils.OrderedSet)
    for node, func, sigs, args, kws, retvar in call_traces:
      # The lengths may be different in the presence of optional and kw args.
      arg_names = max((sig.get_positional_names() for sig in sigs), key=len)
      for i in range(len(arg_names)):
        if not isinstance(func.data, abstract.BoundFunction) or i > 0:
          arg_names[i] = function.argname(i)
      arg_types = (a.data.to_type(node) for a in args)
      ret = pytd_utils.JoinTypes(t.to_type(node) for t in retvar.data)
      # TODO(kramm): Record these:
      starargs = None
      starstarargs = None
      funcs[func.data.name].add(pytd.Signature(
          tuple(pytd.Parameter(n, t, False, False, None)
                for n, t in zip(arg_names, arg_types)) +
          tuple(pytd.Parameter(name, a.data.to_type(node), False, False, None)
                for name, a in kws),
          starargs, starstarargs,
          ret, exceptions=(), template=()))
    functions = []
    for name, signatures in funcs.items():
      functions.append(pytd.Function(name_transform(name), tuple(signatures),
                                     pytd.METHOD))
    return functions
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 / pytd / visitors.py View on Github external
def InventStarArgParams(existing_names):
  """Try to find names for *args, **kwargs that aren't taken already."""
  names = {x if isinstance(x, str) else x.name
           for x in existing_names}
  args, kwargs = "args", "kwargs"
  while args in names:
    args = "_" + args
  while kwargs in names:
    kwargs = "_" + kwargs
  return (pytd.Parameter(args, pytd.NamedType("tuple"), False, True, None),
          pytd.Parameter(kwargs, pytd.NamedType("dict"), False, True, None))