Example #1
0
  def _assign(self, node, target, value):
    name = target.id

    # Record and erase TypeVar and ParamSpec definitions.
    if isinstance(value, _TypeVar):
      self.defs.add_type_var(name, value)
      return Splice([])
    elif isinstance(value, _ParamSpec):
      self.defs.add_param_spec(name, value)
      return Splice([])

    if node.type_comment:
      # TODO(mdemello): can pyi files have aliases with typecomments?
      ret = pytd.Constant(name, node.type_comment)
    else:
      ret = self.new_alias_or_constant(name, value)

    if self.in_function:
      # Should never happen, but this keeps pytype happy.
      if isinstance(ret, types.SlotDecl):
        raise ParseError("Cannot change the type of __slots__")
      return function.Mutator(name, ret.type)

    if self.level == 0:
      self.defs.add_alias_or_constant(ret)
    return ret
Example #2
0
    def visit_Assign(self, node):
        targets = node.targets
        if len(targets) > 1 or isinstance(targets[0], ast3.Tuple):
            msg = "Assignments must be of the form 'name = value'"
            raise ParseError(msg)
        self.convert_node_annotations(node)
        target = targets[0]
        name = target.id

        # Record and erase typevar definitions.
        if isinstance(node.value, _TypeVar):
            self.defs.add_type_var(name, node.value)
            return Splice([])

        if node.type_comment:
            # TODO(mdemello): can pyi files have aliases with typecomments?
            ret = pytd.Constant(name, node.type_comment)
        else:
            ret = self.new_alias_or_constant(name, node.value)

        if self.in_function:
            # Should never happen, but this keeps pytype happy.
            if isinstance(ret, types.SlotDecl):
                raise ParseError("Cannot change the type of __slots__")
            return function.Mutator(name, ret.type)

        if self.level == 0:
            self.defs.add_alias_or_constant(ret)
        return ret