コード例 #1
0
ファイル: fastparse2.py プロジェクト: smalias/mypy
    def visit_Assign(self, n: ast27.Assign) -> AssignmentStmt:
        typ = None
        if n.type_comment:
            typ = parse_type_comment(n.type_comment, n.lineno, self.errors)

        return AssignmentStmt(self.translate_expr_list(n.targets),
                              self.visit(n.value),
                              type=typ)
コード例 #2
0
ファイル: fastparse2.py プロジェクト: greatmazinger/mypy
    def visit_Assign(self, n: ast27.Assign) -> AssignmentStmt:
        typ = None
        if n.type_comment:
            typ = parse_type_comment(n.type_comment, n.lineno, self.errors)

        return AssignmentStmt(self.translate_expr_list(n.targets),
                              self.visit(n.value),
                              type=typ)
コード例 #3
0
ファイル: fastparse2.py プロジェクト: smalias/mypy
 def visit_With(self, n: ast27.With) -> WithStmt:
     if n.type_comment is not None:
         target_type = parse_type_comment(n.type_comment, n.lineno, self.errors)
     else:
         target_type = None
     return WithStmt([self.visit(n.context_expr)],
                     [self.visit(n.optional_vars)],
                     self.as_required_block(n.body, n.lineno),
                     target_type)
コード例 #4
0
ファイル: fastparse2.py プロジェクト: wlmgithub/mypy
 def visit_For(self, n: ast27.For) -> ForStmt:
     if n.type_comment is not None:
         target_type = parse_type_comment(n.type_comment, n.lineno,
                                          self.errors)
     else:
         target_type = None
     return ForStmt(self.visit(n.target), self.visit(n.iter),
                    self.as_block(n.body, n.lineno),
                    self.as_block(n.orelse, n.lineno), target_type)
コード例 #5
0
ファイル: fastparse2.py プロジェクト: greatmazinger/mypy
 def visit_With(self, n: ast27.With) -> WithStmt:
     if n.type_comment is not None:
         target_type = parse_type_comment(n.type_comment, n.lineno, self.errors)
     else:
         target_type = None
     return WithStmt([self.visit(n.context_expr)],
                     [self.visit(n.optional_vars)],
                     self.as_required_block(n.body, n.lineno),
                     target_type)
コード例 #6
0
ファイル: fastparse2.py プロジェクト: pbasista/mypy
    def visit_Assign(self, n: ast27.Assign) -> AssignmentStmt:
        typ = None
        if n.type_comment:
            typ = parse_type_comment(n.type_comment, n.lineno, self.errors,
                                     assume_str_is_unicode=self.unicode_literals)

        stmt = AssignmentStmt(self.translate_expr_list(n.targets),
                              self.visit(n.value),
                              type=typ)
        return self.set_line(stmt, n)
コード例 #7
0
ファイル: fastparse2.py プロジェクト: greatmazinger/mypy
 def visit_For(self, n: ast27.For) -> ForStmt:
     if n.type_comment is not None:
         target_type = parse_type_comment(n.type_comment, n.lineno, self.errors)
     else:
         target_type = None
     return ForStmt(self.visit(n.target),
                    self.visit(n.iter),
                    self.as_required_block(n.body, n.lineno),
                    self.as_block(n.orelse, n.lineno),
                    target_type)
コード例 #8
0
ファイル: fastparse2.py プロジェクト: pbasista/mypy
 def visit_With(self, n: ast27.With) -> WithStmt:
     if n.type_comment is not None:
         target_type = parse_type_comment(n.type_comment, n.lineno, self.errors,
                                          assume_str_is_unicode=self.unicode_literals)
     else:
         target_type = None
     stmt = WithStmt([self.visit(n.context_expr)],
                     [self.visit(n.optional_vars)],
                     self.as_required_block(n.body, n.lineno),
                     target_type)
     return self.set_line(stmt, n)
コード例 #9
0
ファイル: fastparse2.py プロジェクト: pbasista/mypy
 def visit_For(self, n: ast27.For) -> ForStmt:
     if n.type_comment is not None:
         target_type = parse_type_comment(n.type_comment, n.lineno, self.errors,
                                          assume_str_is_unicode=self.unicode_literals)
     else:
         target_type = None
     stmt = ForStmt(self.visit(n.target),
                    self.visit(n.iter),
                    self.as_required_block(n.body, n.lineno),
                    self.as_block(n.orelse, n.lineno),
                    target_type)
     return self.set_line(stmt, n)
コード例 #10
0
ファイル: fastparse2.py プロジェクト: priyank-p/mypy
 def translate_type_comment(self, n: ast27.stmt, type_comment: Optional[str]) -> Optional[Type]:
     if type_comment is None:
         return None
     else:
         lineno = n.lineno
         extra_ignore, typ = parse_type_comment(type_comment,
                                                lineno,
                                                n.col_offset,
                                                self.errors,
                                                assume_str_is_unicode=self.unicode_literals)
         if extra_ignore is not None:
             self.type_ignores[lineno] = extra_ignore
         return typ
コード例 #11
0
ファイル: fastparse2.py プロジェクト: melisajuma/Awards
 def visit_With(self, n: ast27.With) -> WithStmt:
     if n.type_comment is not None:
         extra_ignore, typ = parse_type_comment(n.type_comment,
                                                n.lineno,
                                                n.col_offset,
                                                self.errors,
                                                assume_str_is_unicode=self.unicode_literals)
         if extra_ignore:
             self.type_ignores.add(n.lineno)
     else:
         typ = None
     stmt = WithStmt([self.visit(n.context_expr)],
                     [self.visit(n.optional_vars)],
                     self.as_required_block(n.body, n.lineno),
                     typ)
     return self.set_line(stmt, n)
コード例 #12
0
ファイル: fastparse2.py プロジェクト: melisajuma/Awards
 def visit_For(self, n: ast27.For) -> ForStmt:
     if n.type_comment is not None:
         extra_ignore, typ = parse_type_comment(n.type_comment,
                                                n.lineno,
                                                n.col_offset,
                                                self.errors,
                                                assume_str_is_unicode=self.unicode_literals)
         if extra_ignore:
             self.type_ignores.add(n.lineno)
     else:
         typ = None
     stmt = ForStmt(self.visit(n.target),
                    self.visit(n.iter),
                    self.as_required_block(n.body, n.lineno),
                    self.as_block(n.orelse, n.lineno),
                    typ)
     return self.set_line(stmt, n)
コード例 #13
0
ファイル: exprtotype.py プロジェクト: suryansh2020/mypy
def expr_to_unanalyzed_type(expr: Expression) -> Type:
    """Translate an expression to the corresponding type.

    The result is not semantically analyzed. It can be UnboundType or TypeList.
    Raise TypeTranslationError if the expression cannot represent a type.
    """
    if isinstance(expr, NameExpr):
        name = expr.name
        return UnboundType(name, line=expr.line, column=expr.column)
    elif isinstance(expr, MemberExpr):
        fullname = get_member_expr_fullname(expr)
        if fullname:
            return UnboundType(fullname, line=expr.line, column=expr.column)
        else:
            raise TypeTranslationError()
    elif isinstance(expr, IndexExpr):
        base = expr_to_unanalyzed_type(expr.base)
        if isinstance(base, UnboundType):
            if base.args:
                raise TypeTranslationError()
            if isinstance(expr.index, TupleExpr):
                args = expr.index.items
            else:
                args = [expr.index]
            base.args = [expr_to_unanalyzed_type(arg) for arg in args]
            if not base.args:
                base.empty_tuple_index = True
            return base
        else:
            raise TypeTranslationError()
    elif isinstance(expr, ListExpr):
        return TypeList([expr_to_unanalyzed_type(t) for t in expr.items],
                        line=expr.line,
                        column=expr.column)
    elif isinstance(expr, (StrExpr, BytesExpr, UnicodeExpr)):
        # Parse string literal type.
        try:
            result = parse_type_comment(expr.value, expr.line, None)
        except SyntaxError:
            raise TypeTranslationError()
        return result
    elif isinstance(expr, EllipsisExpr):
        return EllipsisType(expr.line)
    else:
        raise TypeTranslationError()
コード例 #14
0
ファイル: exprtotype.py プロジェクト: greatmazinger/mypy
def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = None) -> Type:
    """Translate an expression to the corresponding type.

    The result is not semantically analyzed. It can be UnboundType or TypeList.
    Raise TypeTranslationError if the expression cannot represent a type.
    """
    # The `parent` paremeter is used in recursive calls to provide context for
    # understanding whether an CallableArgument is ok.
    name = None  # type: Optional[str]
    if isinstance(expr, NameExpr):
        name = expr.name
        return UnboundType(name, line=expr.line, column=expr.column)
    elif isinstance(expr, MemberExpr):
        fullname = get_member_expr_fullname(expr)
        if fullname:
            return UnboundType(fullname, line=expr.line, column=expr.column)
        else:
            raise TypeTranslationError()
    elif isinstance(expr, IndexExpr):
        base = expr_to_unanalyzed_type(expr.base, expr)
        if isinstance(base, UnboundType):
            if base.args:
                raise TypeTranslationError()
            if isinstance(expr.index, TupleExpr):
                args = expr.index.items
            else:
                args = [expr.index]
            base.args = [expr_to_unanalyzed_type(arg, expr) for arg in args]
            if not base.args:
                base.empty_tuple_index = True
            return base
        else:
            raise TypeTranslationError()
    elif isinstance(expr, CallExpr) and isinstance(_parent, ListExpr):
        c = expr.callee
        names = []
        # Go through the dotted member expr chain to get the full arg
        # constructor name to look up
        while True:
            if isinstance(c, NameExpr):
                names.append(c.name)
                break
            elif isinstance(c, MemberExpr):
                names.append(c.name)
                c = c.expr
            else:
                raise TypeTranslationError()
        arg_const = '.'.join(reversed(names))

        # Go through the constructor args to get its name and type.
        name = None
        default_type = AnyType(TypeOfAny.unannotated)
        typ = default_type  # type: Type
        for i, arg in enumerate(expr.args):
            if expr.arg_names[i] is not None:
                if expr.arg_names[i] == "name":
                    if name is not None:
                        # Two names
                        raise TypeTranslationError()
                    name = _extract_argument_name(arg)
                    continue
                elif expr.arg_names[i] == "type":
                    if typ is not default_type:
                        # Two types
                        raise TypeTranslationError()
                    typ = expr_to_unanalyzed_type(arg, expr)
                    continue
                else:
                    raise TypeTranslationError()
            elif i == 0:
                typ = expr_to_unanalyzed_type(arg, expr)
            elif i == 1:
                name = _extract_argument_name(arg)
            else:
                raise TypeTranslationError()
        return CallableArgument(typ, name, arg_const, expr.line, expr.column)
    elif isinstance(expr, ListExpr):
        return TypeList([expr_to_unanalyzed_type(t, expr) for t in expr.items],
                        line=expr.line, column=expr.column)
    elif isinstance(expr, (StrExpr, BytesExpr, UnicodeExpr)):
        # Parse string literal type.
        try:
            result = parse_type_comment(expr.value, expr.line, None)
            assert result is not None
        except SyntaxError:
            raise TypeTranslationError()
        return result
    elif isinstance(expr, EllipsisExpr):
        return EllipsisType(expr.line)
    else:
        raise TypeTranslationError()
コード例 #15
0
ファイル: exprtotype.py プロジェクト: KenJi544/async_training
def expr_to_unanalyzed_type(expr: Expression,
                            _parent: Optional[Expression] = None) -> Type:
    """Translate an expression to the corresponding type.

    The result is not semantically analyzed. It can be UnboundType or TypeList.
    Raise TypeTranslationError if the expression cannot represent a type.
    """
    # The `parent` parameter is used in recursive calls to provide context for
    # understanding whether an CallableArgument is ok.
    name = None  # type: Optional[str]
    if isinstance(expr, NameExpr):
        name = expr.name
        return UnboundType(name, line=expr.line, column=expr.column)
    elif isinstance(expr, MemberExpr):
        fullname = get_member_expr_fullname(expr)
        if fullname:
            return UnboundType(fullname, line=expr.line, column=expr.column)
        else:
            raise TypeTranslationError()
    elif isinstance(expr, IndexExpr):
        base = expr_to_unanalyzed_type(expr.base, expr)
        if isinstance(base, UnboundType):
            if base.args:
                raise TypeTranslationError()
            if isinstance(expr.index, TupleExpr):
                args = expr.index.items
            else:
                args = [expr.index]
            base.args = [expr_to_unanalyzed_type(arg, expr) for arg in args]
            if not base.args:
                base.empty_tuple_index = True
            return base
        else:
            raise TypeTranslationError()
    elif isinstance(expr, CallExpr) and isinstance(_parent, ListExpr):
        c = expr.callee
        names = []
        # Go through the dotted member expr chain to get the full arg
        # constructor name to look up
        while True:
            if isinstance(c, NameExpr):
                names.append(c.name)
                break
            elif isinstance(c, MemberExpr):
                names.append(c.name)
                c = c.expr
            else:
                raise TypeTranslationError()
        arg_const = '.'.join(reversed(names))

        # Go through the constructor args to get its name and type.
        name = None
        default_type = AnyType(TypeOfAny.unannotated)
        typ = default_type  # type: Type
        for i, arg in enumerate(expr.args):
            if expr.arg_names[i] is not None:
                if expr.arg_names[i] == "name":
                    if name is not None:
                        # Two names
                        raise TypeTranslationError()
                    name = _extract_argument_name(arg)
                    continue
                elif expr.arg_names[i] == "type":
                    if typ is not default_type:
                        # Two types
                        raise TypeTranslationError()
                    typ = expr_to_unanalyzed_type(arg, expr)
                    continue
                else:
                    raise TypeTranslationError()
            elif i == 0:
                typ = expr_to_unanalyzed_type(arg, expr)
            elif i == 1:
                name = _extract_argument_name(arg)
            else:
                raise TypeTranslationError()
        return CallableArgument(typ, name, arg_const, expr.line, expr.column)
    elif isinstance(expr, ListExpr):
        return TypeList([expr_to_unanalyzed_type(t, expr) for t in expr.items],
                        line=expr.line,
                        column=expr.column)
    elif isinstance(expr, (StrExpr, BytesExpr, UnicodeExpr)):
        # Parse string literal type.
        try:
            result = parse_type_comment(expr.value, expr.line, None)
            assert result is not None
        except SyntaxError:
            raise TypeTranslationError()
        return result
    elif isinstance(expr, EllipsisExpr):
        return EllipsisType(expr.line)
    else:
        raise TypeTranslationError()