Пример #1
0
class _Folded:
    maxlen = Undefined(Any)
    policy = Undefined(Any)
    lastlen = Undefined(Any)
    stickyspace = Undefined(Any)
    firstline = Undefined(Any)
    done = Undefined(Any)
    current = Undefined(Any)

    def __init__(self, maxlen, policy):
        pass

    def newline(self):
        pass

    def finalize(self):
        pass

    def append(self, stoken):
        pass

    def append_if_fits(self, token, stoken=None):
        pass
Пример #2
0
class TryStmt(Node):
    body = Undefined(Block)                # Try body
    types = Undefined(List[Node])          # Except type expressions
    vars = Undefined(List['NameExpr'])     # Except variable names
    handlers = Undefined(List[Block])      # Except bodies
    else_body = Undefined(Block)
    finally_body = Undefined(Block)

    def __init__(self, body: Block, vars: List['NameExpr'], types: List[Node],
                 handlers: List[Block], else_body: Block,
                 finally_body: Block) -> None:
        self.body = body
        self.vars = vars
        self.types = types
        self.handlers = handlers
        self.else_body = else_body
        self.finally_body = finally_body

    def accept(self, visitor: NodeVisitor[T]) -> T:
        return visitor.visit_try_stmt(self)
Пример #3
0
class DictReader:
    restkey = Undefined(Any)
    restval = Undefined(Any)
    reader = Undefined(Any)
    dialect = Undefined(Any)
    line_num = Undefined(Any)
    fieldnames = Undefined(Any)  # Actually a property

    def __init__(self,
                 f,
                 fieldnames=None,
                 restkey=None,
                 restval=None,
                 dialect='',
                 *args,
                 **kwds):
        pass

    def __iter__(self):
        pass

    def __next__(self):
        pass
Пример #4
0
class ZipInfo:
    filename = ''
    date_time = Undefined(Tuple[int, int, int, int, int, int])
    compressed_size = 0
    file_size = 0
Пример #5
0
class StringFormatterChecker:
    """String interplation/formatter type checker.

    This class works closely together with checker.ExpressionChecker.
    """

    # Some services are provided by a TypeChecker instance.
    chk = Undefined('mypy.checker.TypeChecker')
    # This is shared with TypeChecker, but stored also here for convenience.
    msg = Undefined(MessageBuilder)
    # Some services are provided by a ExpressionChecker instance.
    exprchk = Undefined('mypy.checkexpr.ExpressionChecker')

    def __init__(self, exprchk: 'mypy.checkexpr.ExpressionChecker',
                 chk: 'mypy.checker.TypeChecker', msg: MessageBuilder) -> None:
        """Construct an expression type checker."""
        self.chk = chk
        self.exprchk = exprchk
        self.msg = msg

    def check_str_interpolation(self, str: StrExpr,
                                replacements: Node) -> Type:
        """Check the types of the 'replacements' in a string interpolation
        expression: str % replacements
        """
        specifiers = self.parse_conversion_specifiers(str.value)
        has_mapping_keys = self.analyse_conversion_specifiers(specifiers, str)
        if has_mapping_keys == None:
            pass  # Error was reported
        elif has_mapping_keys:
            self.check_mapping_str_interpolation(specifiers, replacements)
        else:
            self.check_simple_str_interpolation(specifiers, replacements)
        return self.named_type('builtins.str')

    def parse_conversion_specifiers(self,
                                    format: str) -> List[ConversionSpecifier]:
        key_regex = r'(\((\w*)\))?'  # (optional) parenthesised sequence of characters
        flags_regex = r'([#0\-+ ]*)'  # (optional) sequence of flags
        width_regex = r'(\*|[1-9][0-9]*)?'  # (optional) minimum field width (* or numbers)
        precision_regex = r'(?:\.(\*|[0-9]+))?'  # (optional) . followed by * of numbers
        length_mod_regex = r'[hlL]?'  # (optional) length modifier (unused)
        type_regex = r'(.)?'  # conversion type
        regex = ('%' + key_regex + flags_regex + width_regex +
                 precision_regex + length_mod_regex + type_regex)
        specifiers = []  # type: List[ConversionSpecifier]
        for parens_key, key, flags, width, precision, type in re.findall(
                regex, format):
            if parens_key == '':
                key = None
            specifiers.append(
                ConversionSpecifier(key, flags, width, precision, type))
        return specifiers

    def analyse_conversion_specifiers(self,
                                      specifiers: List[ConversionSpecifier],
                                      context: Context) -> bool:
        has_star = any(specifier.has_star() for specifier in specifiers)
        has_key = any(specifier.has_key() for specifier in specifiers)
        all_have_keys = all(specifier.has_key() for specifier in specifiers)

        if has_key and has_star:
            self.msg.string_interpolation_with_star_and_key(context)
            return None
        if has_key and not all_have_keys:
            self.msg.string_interpolation_mixing_key_and_non_keys(context)
            return None
        return has_key

    def check_simple_str_interpolation(self,
                                       specifiers: List[ConversionSpecifier],
                                       replacements: Node) -> None:
        checkers = self.build_replacement_checkers(specifiers, replacements)
        if checkers == None:
            return

        rhs_type = self.accept(replacements)
        rep_types = []  # type: List[Type]
        if isinstance(rhs_type, TupleType):
            rep_types = rhs_type.items
        elif isinstance(rhs_type, AnyType):
            return
        else:
            rep_types = [rhs_type]

        if len(checkers) > len(rep_types):
            self.msg.too_few_string_formatting_arguments(replacements)
        elif len(checkers) < len(rep_types):
            self.msg.too_many_string_formatting_arguments(replacements)
        else:
            if len(checkers) == 1:
                check_node, check_type = checkers[0]
                check_node(replacements)
            elif isinstance(replacements, TupleExpr):
                for checks, rep_node in zip(checkers, replacements.items):
                    check_node, check_type = checks
                    check_node(rep_node)
            else:
                for checks, rep_type in zip(checkers, rep_types):
                    check_node, check_type = checks
                    check_type(rep_type)

    def check_mapping_str_interpolation(self,
                                        specifiers: List[ConversionSpecifier],
                                        replacements: Node) -> None:
        dict_with_only_str_literal_keys = (isinstance(
            replacements, DictExpr) and all(
                isinstance(k, StrExpr)
                for k, v in cast(DictExpr, replacements).items))
        if dict_with_only_str_literal_keys:
            mapping = {}  # type: Dict[str, Type]
            for k, v in cast(DictExpr, replacements).items:
                key_str = cast(StrExpr, k).value
                mapping[key_str] = self.accept(v)

            for specifier in specifiers:
                if specifier.key not in mapping:
                    self.msg.key_not_in_mapping(specifier.key, replacements)
                    return
                rep_type = mapping[specifier.key]
                expected_type = self.conversion_type(specifier.type,
                                                     replacements)
                if expected_type == None:
                    return
                self.chk.check_subtype(
                    rep_type, expected_type, replacements,
                    messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
                    'expression has type',
                    'placeholder with key \'%s\' has type' % specifier.key)
        else:
            rep_type = self.accept(replacements)
            dict_type = self.chk.named_generic_type(
                'builtins.dict', [AnyType(), AnyType()])
            self.chk.check_subtype(rep_type, dict_type, replacements,
                                   messages.FORMAT_REQUIRES_MAPPING,
                                   'expression has type',
                                   'expected type for mapping is')

    def build_replacement_checkers(
        self, specifiers: List[ConversionSpecifier], context: Context
    ) -> List[Tuple[Callable[[Node], None], Callable[[Type], None]]]:
        checkers = [
        ]  # type: List[ Tuple[ Callable[[Node], None], Callable[[Type], None] ] ]
        for specifier in specifiers:
            checker = self.replacement_checkers(specifier, context)
            if checker == None:
                return None
            checkers.extend(checker)
        return checkers

    def replacement_checkers(
        self, specifier: ConversionSpecifier, context: Context
    ) -> List[Tuple[Callable[[Node], None], Callable[[Type], None]]]:
        """Returns a list of tuples of two functions that check whether a replacement is
        of the right type for the specifier. The first functions take a node and checks
        its type in the right type context. The second function just checks a type.
        """
        checkers = [
        ]  # type: List[ Tuple[ Callable[[Node], None], Callable[[Type], None] ] ]

        if specifier.width == '*':
            checkers.append(self.checkers_for_star(context))
        if specifier.precision == '*':
            checkers.append(self.checkers_for_star(context))
        if specifier.type == 'c':
            c = self.checkers_for_c_type(specifier.type, context)
            if c == None:
                return None
            checkers.append(c)
        elif specifier.type != '%':
            c = self.checkers_for_regular_type(specifier.type, context)
            if c == None:
                return None
            checkers.append(c)
        return checkers

    def checkers_for_star(
        self, context: Context
    ) -> Tuple[Callable[[Node], None], Callable[[Type], None]]:
        """Returns a tuple of check functions that check whether, respectively,
        a node or a type is compatible with a star in a conversion specifier
        """
        expected = self.named_type('builtins.int')

        def check_type(type: Type = None) -> None:
            expected = self.named_type('builtins.int')
            self.chk.check_subtype(type, expected, context, '* wants int')

        def check_node(node: Node) -> None:
            type = self.accept(node, expected)
            check_type(type)

        return check_node, check_type

    def checkers_for_regular_type(
        self, type: str, context: Context
    ) -> Tuple[Callable[[Node], None], Callable[[Type], None]]:
        """Returns a tuple of check functions that check whether, respectively,
        a node or a type is compatible with 'type'. Return None in case of an
        """
        expected_type = self.conversion_type(type, context)
        if expected_type == None:
            return None

        def check_type(type: Type = None) -> None:
            self.chk.check_subtype(
                type, expected_type, context,
                messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
                'expression has type', 'placeholder has type')

        def check_node(node: Node) -> None:
            type = self.accept(node, expected_type)
            check_type(type)

        return check_node, check_type

    def checkers_for_c_type(
        self, type: str, context: Context
    ) -> Tuple[Callable[[Node], None], Callable[[Type], None]]:
        """Returns a tuple of check functions that check whether, respectively,
        a node or a type is compatible with 'type' that is a character type
        """
        expected_type = self.conversion_type(type, context)
        if expected_type == None:
            return None

        def check_type(type: Type = None) -> None:
            self.chk.check_subtype(
                type, expected_type, context,
                messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
                'expression has type', 'placeholder has type')

        def check_node(node: Node) -> None:
            """int, or str with length 1"""
            type = self.accept(node, expected_type)
            if isinstance(node,
                          StrExpr) and len(cast(StrExpr, node).value) != 1:
                self.msg.requires_int_or_char(context)
            check_type(type)

        return check_node, check_type

    def conversion_type(self, p: str, context: Context) -> Type:
        """Return the type that is accepted for a string interpolation
        conversion specifier type.

        Note that both Python's float (e.g. %f) and integer (e.g. %d)
        specifier types accept both float and integers.
        """
        if p in ['s', 'r']:
            return AnyType()
        elif p in ['d', 'i', 'o', 'u', 'x', 'X', 'e', 'E', 'f', 'F', 'g', 'G']:
            return UnionType([
                self.named_type('builtins.int'),
                self.named_type('builtins.float')
            ])
        elif p in ['c']:
            return UnionType([
                self.named_type('builtins.int'),
                self.named_type('builtins.float'),
                self.named_type('builtins.str')
            ])
        else:
            self.msg.unsupported_placeholder(p, context)
            return None

    #
    # Helpers
    #

    def named_type(self, name: str) -> Instance:
        """Return an instance type with type given by the name and no type
        arguments. Alias for TypeChecker.named_type.
        """
        return self.chk.named_type(name)

    def accept(self, node: Node, context: Type = None) -> Type:
        """Type check a node. Alias for TypeChecker.accept."""
        return self.chk.accept(node, context)
Пример #6
0
class ConstraintBuilderVisitor(TypeVisitor[List[Constraint]]):
    """Visitor class for inferring type constraints."""

    # The type that is compared against a template
    actual = Undefined(Type)

    def __init__(self, actual: Type, direction: int) -> None:
        # Direction must be SUBTYPE_OF or SUPERTYPE_OF.
        self.actual = actual
        self.direction = direction

    # Trivial leaf types

    def visit_unbound_type(self, template: UnboundType) -> List[Constraint]:
        return []

    def visit_any(self, template: AnyType) -> List[Constraint]:
        return []

    def visit_void(self, template: Void) -> List[Constraint]:
        return []

    def visit_none_type(self, template: NoneTyp) -> List[Constraint]:
        return []

    def visit_erased_type(self, template: ErasedType) -> List[Constraint]:
        return []

    # Non-trivial leaf type

    def visit_type_var(self, template: TypeVar) -> List[Constraint]:
        return [Constraint(template.id, self.direction, self.actual)]

    # Non-leaf types

    def visit_instance(self, template: Instance) -> List[Constraint]:
        actual = self.actual
        res = []  # type: List[Constraint]
        if isinstance(actual, Instance):
            instance = cast(Instance, actual)
            if (self.direction == SUBTYPE_OF
                    and template.type.has_base(instance.type.fullname())):
                mapped = map_instance_to_supertype(template, instance.type)
                for i in range(len(instance.args)):
                    # The constraints for generic type parameters are
                    # invariant. Include the default constraint and its
                    # negation to achieve the effect.
                    cb = infer_constraints(mapped.args[i], instance.args[i],
                                           self.direction)
                    res.extend(cb)
                    res.extend(negate_constraints(cb))
                return res
            elif (self.direction == SUPERTYPE_OF
                  and instance.type.has_base(template.type.fullname())):
                mapped = map_instance_to_supertype(instance, template.type)
                for j in range(len(template.args)):
                    # The constraints for generic type parameters are
                    # invariant.
                    cb = infer_constraints(template.args[j], mapped.args[j],
                                           self.direction)
                    res.extend(cb)
                    res.extend(negate_constraints(cb))
                return res
        if isinstance(actual, AnyType):
            # IDEA: Include both ways, i.e. add negation as well?
            return self.infer_against_any(template.args)
        if (isinstance(actual, TupleType)
                and (is_named_instance(template, 'typing.Iterable')
                     or is_named_instance(template, 'typing.Sequence')
                     or is_named_instance(template, 'typing.Reversible'))
                and self.direction == SUPERTYPE_OF):
            actual = cast(TupleType, actual)
            for item in actual.items:
                cb = infer_constraints(template.args[0], item, SUPERTYPE_OF)
                res.extend(cb)
            return res
        else:
            return []

    def visit_callable(self, template: Callable) -> List[Constraint]:
        if isinstance(self.actual, Callable):
            cactual = cast(Callable, self.actual)
            # FIX verify argument counts
            # FIX what if one of the functions is generic
            res = []  # type: List[Constraint]
            for i in range(len(template.arg_types)):
                # Negate constraints due function argument type contravariance.
                res.extend(
                    negate_constraints(
                        infer_constraints(template.arg_types[i],
                                          cactual.arg_types[i],
                                          self.direction)))
            res.extend(
                infer_constraints(template.ret_type, cactual.ret_type,
                                  self.direction))
            return res
        elif isinstance(self.actual, AnyType):
            # FIX what if generic
            res = self.infer_against_any(template.arg_types)
            res.extend(
                infer_constraints(template.ret_type, AnyType(),
                                  self.direction))
            return res
        elif isinstance(self.actual, Overloaded):
            return self.infer_against_overloaded(cast(Overloaded, self.actual),
                                                 template)
        else:
            return []

    def infer_against_overloaded(self, overloaded: Overloaded,
                                 template: Callable) -> List[Constraint]:
        # Create constraints by matching an overloaded type against a template.
        # This is tricky to do in general. We cheat by only matching against
        # the first overload item, and by only matching the return type. This
        # seems to work somewhat well, but we should really use a more
        # reliable technique.
        item = overloaded.items()[0]
        return infer_constraints(template.ret_type, item.ret_type,
                                 self.direction)

    def visit_tuple_type(self, template: TupleType) -> List[Constraint]:
        actual = self.actual
        if (isinstance(actual, TupleType) and len(
            (cast(TupleType, actual)).items) == len(template.items)):
            res = []  # type: List[Constraint]
            for i in range(len(template.items)):
                res.extend(
                    infer_constraints(template.items[i],
                                      cast(TupleType, actual).items[i],
                                      self.direction))
            return res
        elif isinstance(actual, AnyType):
            return self.infer_against_any(template.items)
        else:
            return []

    def visit_union_type(self, template: UnionType) -> List[Constraint]:
        res = []  # type: List[Constraint]
        for item in template.items:
            res.extend(infer_constraints(item, self.actual, self.direction))
        return res

    def infer_against_any(self, types: List[Type]) -> List[Constraint]:
        res = []  # type: List[Constraint]
        for t in types:
            res.extend(infer_constraints(t, AnyType(), self.direction))
        return res
Пример #7
0
        pass

    def add_get_handler(self, key, handler):
        pass

    def get_content(self, msg, *args, **kw):
        pass

    def add_set_handler(self, typekey, handler):
        pass

    def set_content(self, msg, obj, *args, **kw):
        pass


raw_data_manager = Undefined(Any)


def get_text_content(msg, errors=''):
    pass


def get_non_text_content(msg):
    pass


def get_message_content(msg):
    pass


def get_and_fixup_unknown_message_content(msg):
Пример #8
0
class function:
    # TODO not defined in builtins!
    __name__ = ''
    __module__ = ''
    __code__ = Undefined(Any)
Пример #9
0
# Stubs for email.base64mime (Python 3.4)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.

from typing import Undefined, Any


def header_length(bytearray):
    pass


def header_encode(header_bytes, charset=''):
    pass


def body_encode(s, maxlinelen=76, eol=Undefined):
    pass


def decode(string):
    pass


body_decode = Undefined(Any)
decodestring = Undefined(Any)
Пример #10
0
class _AssertRaisesBaseContext:
    expected = Undefined(Any)
    failureException = Undefined(type)
    obj_name = ''
    expected_regex = Undefined(Pattern[str])
Пример #11
0
class DyncheckTransformVisitor(TraverserVisitor):
    """Translate a parse tree to use runtime representation of generics.

    Translate generic type variables to ordinary variables and all make
    all non-trivial coercions explicit. Also generate generic wrapper classes
    for coercions between generic types and wrapper methods for overrides
    and for more efficient access from dynamically typed code.
    
    This visitor modifies the parse tree in-place.
    """

    type_map = Undefined(Dict[Node, Type])
    modules = Undefined(Dict[str, MypyFile])
    is_pretty = False
    type_tf = Undefined(TypeTransformer)

    # Stack of function return types
    return_types = Undefined(List[Type])
    # Stack of dynamically typed function flags
    dynamic_funcs = Undefined(List[bool])

    # Associate a Node with its start end line numbers.
    line_map = Undefined(Dict[Node, Tuple[int, int]])

    is_java = False

    # The current type context (or None if not within a type).
    _type_context = None  # type: TypeInfo

    def type_context(self) -> TypeInfo:
        return self._type_context

    def __init__(self,
                 type_map: Dict[Node, Type],
                 modules: Dict[str, MypyFile],
                 is_pretty: bool,
                 is_java: bool = False) -> None:
        self.type_tf = TypeTransformer(self)
        self.return_types = []
        self.dynamic_funcs = [False]
        self.line_map = {}
        self.type_map = type_map
        self.modules = modules
        self.is_pretty = is_pretty
        self.is_java = is_java

    #
    # Transform definitions
    #

    def visit_mypy_file(self, o: MypyFile) -> None:
        """Transform an file."""
        res = []  # type: List[Node]
        for d in o.defs:
            if isinstance(d, ClassDef):
                self._type_context = d.info
                res.extend(self.type_tf.transform_class_def(d))
                self._type_context = None
            else:
                d.accept(self)
                res.append(d)
        o.defs = res

    def visit_var_def(self, o: VarDef) -> None:
        """Transform a variable definition in-place.

        This is not suitable for member variable definitions; they are
        transformed in TypeTransformer.
        """
        super().visit_var_def(o)

        if o.init is not None:
            if o.items[0].type:
                t = o.items[0].type
            else:
                t = AnyType()
            o.init = self.coerce(o.init, t, self.get_type(o.init),
                                 self.type_context())

    def visit_func_def(self, fdef: FuncDef) -> None:
        """Transform a global function definition in-place.

        This is not suitable for methods; they are transformed in
        FuncTransformer.
        """
        self.prepend_generic_function_tvar_args(fdef)
        self.transform_function_body(fdef)

    def transform_function_body(self, fdef: FuncDef) -> None:
        """Transform the body of a function."""
        self.dynamic_funcs.append(fdef.is_implicit)
        # FIX overloads
        self.return_types.append(cast(Callable, function_type(fdef)).ret_type)
        super().visit_func_def(fdef)
        self.return_types.pop()
        self.dynamic_funcs.pop()

    def prepend_generic_function_tvar_args(self, fdef: FuncDef) -> None:
        """Add implicit function type variable arguments if fdef is generic."""
        sig = cast(Callable, function_type(fdef))
        tvars = sig.variables
        if not fdef.type:
            fdef.type = sig

        tv = []  # type: List[Var]
        ntvars = len(tvars)
        if fdef.is_method():
            # For methods, add type variable arguments after the self arg.
            for n in range(ntvars):
                tv.append(Var(tvar_arg_name(-1 - n)))
                fdef.type = add_arg_type_after_self(cast(Callable, fdef.type),
                                                    AnyType())
            fdef.args = [fdef.args[0]] + tv + fdef.args[1:]
        else:
            # For ordinary functions, prepend type variable arguments.
            for n in range(ntvars):
                tv.append(Var(tvar_arg_name(-1 - n)))
                fdef.type = prepend_arg_type(cast(Callable, fdef.type),
                                             AnyType())
            fdef.args = tv + fdef.args
        fdef.init = List[AssignmentStmt]([None]) * ntvars + fdef.init

    #
    # Transform statements
    #

    def transform_block(self, block: List[Node]) -> None:
        for stmt in block:
            stmt.accept(self)

    def visit_return_stmt(self, s: ReturnStmt) -> None:
        super().visit_return_stmt(s)
        s.expr = self.coerce(s.expr, self.return_types[-1],
                             self.get_type(s.expr), self.type_context())

    def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
        super().visit_assignment_stmt(s)
        if isinstance(s.lvalues[0], IndexExpr):
            index = cast(IndexExpr, s.lvalues[0])
            method_type = index.method_type
            if self.dynamic_funcs[-1] or isinstance(method_type, AnyType):
                lvalue_type = AnyType()  # type: Type
            else:
                method_callable = cast(Callable, method_type)
                # TODO arg_types[1] may not be reliable
                lvalue_type = method_callable.arg_types[1]
        else:
            lvalue_type = self.get_type(s.lvalues[0])

        s.rvalue = self.coerce2(s.rvalue, lvalue_type, self.get_type(s.rvalue),
                                self.type_context())

    #
    # Transform expressions
    #

    def visit_member_expr(self, e: MemberExpr) -> None:
        super().visit_member_expr(e)

        typ = self.get_type(e.expr)

        if self.dynamic_funcs[-1]:
            e.expr = self.coerce_to_dynamic(e.expr, typ, self.type_context())
            typ = AnyType()

        if isinstance(typ, Instance):
            # Reference to a statically-typed method variant with the suffix
            # derived from the base object type.
            suffix = self.get_member_reference_suffix(e.name, typ.type)
        else:
            # Reference to a dynamically-typed method variant.
            suffix = self.dynamic_suffix()
        e.name += suffix

    def visit_name_expr(self, e: NameExpr) -> None:
        super().visit_name_expr(e)
        if e.kind == MDEF and isinstance(e.node, FuncDef):
            # Translate reference to a method.
            suffix = self.get_member_reference_suffix(e.name, e.info)
            e.name += suffix
            # Update representation to have the correct name.
            prefix = e.repr.components[0].pre

    def get_member_reference_suffix(self, name: str, info: TypeInfo) -> str:
        if info.has_method(name):
            fdef = cast(FuncDef, info.get_method(name))
            return self.type_suffix(fdef)
        else:
            return ''

    def visit_call_expr(self, e: CallExpr) -> None:
        if e.analyzed:
            # This is not an ordinary call.
            e.analyzed.accept(self)
            return

        super().visit_call_expr(e)

        # Do no coercions if this is a call to debugging facilities.
        if self.is_debugging_call_expr(e):
            return

        # Get the type of the callable (type variables in the context of the
        # enclosing class).
        ctype = self.get_type(e.callee)

        # Add coercions for the arguments.
        for i in range(len(e.args)):
            arg_type = AnyType()  # type: Type
            if isinstance(ctype, Callable):
                arg_type = ctype.arg_types[i]
            e.args[i] = self.coerce2(e.args[i], arg_type,
                                     self.get_type(e.args[i]),
                                     self.type_context())

        # Prepend type argument values to the call as needed.
        if isinstance(ctype,
                      Callable) and cast(Callable, ctype).bound_vars != []:
            bound_vars = (cast(Callable, ctype)).bound_vars

            # If this is a constructor call (target is the constructor
            # of a generic type or superclass __init__), include also
            # instance type variables.  Otherwise filter them away --
            # include only generic function type variables.
            if (not (cast(Callable, ctype)).is_type_obj()
                    and not (isinstance(e.callee, SuperExpr) and
                             (cast(SuperExpr, e.callee)).name == '__init__')):
                # Filter instance type variables; only include function tvars.
                bound_vars = [(id, t) for id, t in bound_vars if id < 0]

            args = []  # type: List[Node]
            for i in range(len(bound_vars)):
                # Compile type variables to runtime type variable expressions.
                tv = translate_runtime_type_vars_in_context(
                    bound_vars[i][1], self.type_context(), self.is_java)
                args.append(TypeExpr(tv))
            e.args = args + e.args

    def is_debugging_call_expr(self, e):
        return isinstance(e.callee, NameExpr) and e.callee.name in ['__print']

    def visit_cast_expr(self, e: CastExpr) -> None:
        super().visit_cast_expr(e)
        if isinstance(self.get_type(e), AnyType):
            e.expr = self.coerce(e.expr, AnyType(), self.get_type(e.expr),
                                 self.type_context())

    def visit_op_expr(self, e: OpExpr) -> None:
        super().visit_op_expr(e)
        if e.op in ['and', 'or']:
            target = self.get_type(e)
            e.left = self.coerce(e.left, target, self.get_type(e.left),
                                 self.type_context())
            e.right = self.coerce(e.right, target, self.get_type(e.right),
                                  self.type_context())
        else:
            method_type = e.method_type
            if self.dynamic_funcs[-1] or isinstance(method_type, AnyType):
                e.left = self.coerce_to_dynamic(e.left, self.get_type(e.left),
                                                self.type_context())
                e.right = self.coerce(e.right, AnyType(),
                                      self.get_type(e.right),
                                      self.type_context())
            elif method_type:
                method_callable = cast(Callable, method_type)
                operand = e.right
                # For 'in', the order of operands is reversed.
                if e.op == 'in':
                    operand = e.left
                # TODO arg_types[0] may not be reliable
                operand = self.coerce(operand, method_callable.arg_types[0],
                                      self.get_type(operand),
                                      self.type_context())
                if e.op == 'in':
                    e.left = operand
                else:
                    e.right = operand

    def visit_index_expr(self, e: IndexExpr) -> None:
        if e.analyzed:
            # Actually a type application, not indexing.
            e.analyzed.accept(self)
            return
        super().visit_index_expr(e)
        method_type = e.method_type
        if self.dynamic_funcs[-1] or isinstance(method_type, AnyType):
            e.base = self.coerce_to_dynamic(e.base, self.get_type(e.base),
                                            self.type_context())
            e.index = self.coerce_to_dynamic(e.index, self.get_type(e.index),
                                             self.type_context())
        else:
            method_callable = cast(Callable, method_type)
            e.index = self.coerce(e.index, method_callable.arg_types[0],
                                  self.get_type(e.index), self.type_context())

    #
    # Helpers
    #

    def get_type(self, node: Node) -> Type:
        """Return the type of a node as reported by the type checker."""
        return self.type_map[node]

    def set_type(self, node: Node, typ: Type) -> None:
        self.type_map[node] = typ

    def type_suffix(self, fdef: FuncDef, info: TypeInfo = None) -> str:
        """Return the suffix for a mangled name.

        This includes an optional type suffix for a function or method.
        """
        if not info:
            info = fdef.info
        # If info is None, we have a global function => no suffix. Also if the
        # method is not an override, we need no suffix.
        if not info or (not info.bases
                        or not info.bases[0].type.has_method(fdef.name())):
            return ''
        elif is_simple_override(fdef, info):
            return self.type_suffix(fdef, info.bases[0].type)
        elif self.is_pretty:
            return '`' + info.name()
        else:
            return '__' + info.name()

    def dynamic_suffix(self) -> str:
        """Return the suffix of the dynamic wrapper of a method or class."""
        return dynamic_suffix(self.is_pretty)

    def wrapper_class_suffix(self) -> str:
        """Return the suffix of a generic wrapper class."""
        return '**'

    def coerce(self,
               expr: Node,
               target_type: Type,
               source_type: Type,
               context: TypeInfo,
               is_wrapper_class: bool = False) -> Node:
        return coerce(expr, target_type, source_type, context,
                      is_wrapper_class, self.is_java)

    def coerce2(self,
                expr: Node,
                target_type: Type,
                source_type: Type,
                context: TypeInfo,
                is_wrapper_class: bool = False) -> Node:
        """Create coercion from source_type to target_type.

        Also include middle coercion do 'Any' if transforming a dynamically
        typed function.
        """
        if self.dynamic_funcs[-1]:
            return self.coerce(
                self.coerce(expr, AnyType(), source_type, context,
                            is_wrapper_class), target_type, AnyType(), context,
                is_wrapper_class)
        else:
            return self.coerce(expr, target_type, source_type, context,
                               is_wrapper_class)

    def coerce_to_dynamic(self, expr: Node, source_type: Type,
                          context: TypeInfo) -> Node:
        if isinstance(source_type, AnyType):
            return expr
        source_type = translate_runtime_type_vars_in_context(
            source_type, context, self.is_java)
        return CoerceExpr(expr, AnyType(), source_type, False)

    def add_line_mapping(self, orig_node: Node, new_node: Node) -> None:
        """Add a line mapping for a wrapper.

        The node new_node has logically the same line numbers as
        orig_node. The nodes should be FuncDef/ClassDef nodes.
        """
        if orig_node.repr:
            start_line = orig_node.line
            end_line = start_line  # TODO use real end line
            self.line_map[new_node] = (start_line, end_line)

    def named_type(self, name: str) -> Instance:
        # TODO combine with checker
        # Assume that the name refers to a type.
        sym = self.lookup(name, GDEF)
        return Instance(cast(TypeInfo, sym.node), [])

    def lookup(self, fullname: str, kind: int) -> SymbolTableNode:
        # TODO combine with checker
        # TODO remove kind argument
        parts = fullname.split('.')
        n = self.modules[parts[0]]
        for i in range(1, len(parts) - 1):
            n = cast(MypyFile, ((n.names.get(parts[i], None).node)))
        return n.names[parts[-1]]

    def object_member_name(self) -> str:
        if self.is_java:
            return '__o_{}'.format(self.type_context().name())
        else:
            return '__o'
Пример #12
0
from typing import Undefined, Any

html_parts = Undefined(Any)
Пример #13
0
# Stubs for requests.auth (Python 3)

from typing import Undefined, Any
from . import compat
from . import cookies
from . import utils
from . import status_codes

extract_cookies_to_jar = cookies.extract_cookies_to_jar
parse_dict_header = utils.parse_dict_header
to_native_string = utils.to_native_string
codes = status_codes.codes

CONTENT_TYPE_FORM_URLENCODED = Undefined(Any)
CONTENT_TYPE_MULTI_PART = Undefined(Any)


class AuthBase:
    def __call__(self, r):
        pass


class HTTPBasicAuth(AuthBase):
    username = Undefined(Any)
    password = Undefined(Any)

    def __init__(self, username, password):
        pass

    def __call__(self, r):
        pass
Пример #14
0
class User(object):
    _nick = Undefined(Optional[str])
    hopcount = Undefined(int)
    username = Undefined(Optional[str])
    hostname = Undefined(Optional[str])
    servername = Undefined(Optional[str])
    realname = Undefined(Optional[str])
    connection = Undefined('Connection')
    server = Undefined('IRCServer')
    channels = Undefined(LowerCaseDict)
    pingtimer = None
    timeouttimer = None
    modes = Undefined(set)

    ##
    # Nick getter and setter
    ##
    _nick_regex = re.compile(r'(\w+)')

    @property
    def nick(self):
        return self._nick

    @nick.setter
    def nick(self, value):
        '''Validate and set nick value.'''
        match = self._nick_regex.match(value)
        if not match:
            raise ErroneousNicknameError(value)
        value = match.groups()[0]

        nicklen = self.server.settings['nicklen']
        value = value[0:min(len(value), nicklen)]

        if value.lower() == self._nick.lower():
            return

        if value in self.server.users:
            raise NicknameInUseError(value)

        self._nick = value

    @property
    def address(self):
        return '%s!%s@%s' % (self.nick, self.username, self.hostname)

    ##
    # Init
    ##
    def __init__(self, nick: str, connection: 'Connection', server: 'Server',
                 hopcount: int, username: str, hostname: str, servername: str,
                 realname: str):
        self.connection = connection
        self.server = server
        self._nick = ''
        self.nick = nick
        self.hopcount = hopcount
        self.username = username
        self.hostname = hostname
        self.servername = servername
        self.realname = realname
        self.channels = LowerCaseDict()
        self.modes = set()

        logger.info('Registered new user: %s!%s@%s', self.nick, self.username,
                    self.hostname)

    def __repr__(self):
        return '<User: %s>' % self.address

    def __str__(self):
        return self.nick

    ##
    # Events
    ##
    def on_register(self):
        self.send_welcome()

    def on_close(self):
        io_loop = IOLoop.current()
        if self.pingtimer:
            io_loop.remove_timeout(self.pingtimer)
            self.pingtimer = None
        if self.timeouttimer:
            io_loop.remove_timeout(self.timeouttimer)
            self.timeoutttimer = None

        self.quit(message='Connection reset by peer.')

        # Remove nick from server catalog
        del self.server.users[self.nick]

    ##
    # Server send msg commands
    ##
    def send_message(self, *args, **kwargs):
        '''Send message to user.'''
        self.connection.send_message(*args, **kwargs)

    def schedule_ping(self):
        io_loop = IOLoop.current()

        # Disable previous timers
        if self.pingtimer:
            io_loop.remove_timeout(self.pingtimer)

        interval = self.server.settings['pinginterval']
        interval = randint(0.5 * interval, 1.5 * interval)
        self.pingtimer = io_loop.call_later(interval, self.send_ping)

    def send_ping(self):
        '''Send PING to user'''
        if self.connection.stream.closed():
            return
        io_loop = IOLoop.current()

        # Disable previous timeouts
        if self.timeouttimer:
            io_loop.remove_timeout(self.timeouttimer)

        # Send PING
        self.send_message('CMD_PING')

        # Setup timeout timer
        timeout = self.server.settings['pingtimeout']
        self.timeouttimer = io_loop.call_later(timeout, self.timeout)

    def timeout(self):
        '''Disconnect user for timeout.'''
        self.quit(message='Ping timeout')

    def send_welcome(self):
        '''Send welcome messages to user.'''
        self.send_message('RPL_WELCOME')
        self.send_message('RPL_YOURHOST', version=self.server.version)
        self.send_message('RPL_CREATED', date=self.server.date)
        self.send_message('RPL_MYINFO',
                          date=self.server.date,
                          version=self.server.version,
                          usermodes=self.server.usermodes,
                          channelmodes=self.server.channelmodes)

        self.cmd_lusers()
        self.cmd_motd()
        self.schedule_ping()

    def send_privmsg(self, sender: str, recipient: str, text: str):
        '''Send PRIVMSG command to user.'''
        self.send_message('CMD_PRIVMSG',
                          sender=sender,
                          recipient=recipient,
                          text=text)

    def send_notice(self, sender: str, recipient: str, text: str):
        '''Send NOTICE command to user.'''
        self.send_message('CMD_NOTICE',
                          sender=sender,
                          recipient=recipient,
                          text=text)

    def quit(self, message: str = ''):
        '''QUIT user (called by cmd_quit, ping timeout, conn reset etc).'''
        # Leave channels
        channels = self.channels
        channels = [channels[name] for name in channels]

        for channel in channels:
            channel.quit(self, message=message)

        # Close connection
        self.send_message('CMD_ERROR', text='Quit: %s' % message)
        self.connection.stream.close()

    ##
    # Server command handlers
    ##
    def cmd_ping(self, payload: str, destination: Optional[str] = None):
        '''Process PING command.'''
        if destination and destination != self.server.name:
            raise NoSuchServerError(server=destination)
        self.send_message('CMD_PONG', payload=payload)

    def cmd_pong(self, payload: str):
        if self.timeouttimer:
            IOLoop.current().remove_timeout(self.timeouttimer)
            self.timeouttimer = None
        self.schedule_ping()

    def cmd_privmsg(self, target: str, text: str):
        '''Process PRIVMSG command.'''
        if target not in self.server.router:
            raise NoSuchNickError(nick=target)
        entity = self.server.router[target]
        entity.send_privmsg(sender=self.address, recipient=target, text=text)

    def cmd_notice(self, target: str, text: str):
        '''Process NOTICE command.'''
        if target not in self.server.router:
            return
        entity = self.server.router[target]
        entity.send_notice(sender=self.address, recipient=target, text=text)

    def cmd_profiling(self):
        '''Process PROFILING command.'''
        import yappi
        stats = yappi.get_func_stats()[0:30]
        self.send_notice(sender=self.server.name,
                         recipient=self.nick,
                         text='name' + ' ' * 4 + ' ' * 47 + 'ncall' + ' ' * 3 +
                         'tsub' + ' ' * 4 + 'tot' + ' ' * 5 + 'tavg')
        for row in stats:
            self.send_notice(sender=self.server.name,
                             recipient=self.nick,
                             text='%s  %s %s %s %s' %
                             (row[12][-53:], format(row[4] / row[3], '.5f'),
                              format(row[7], '.5f'), format(
                                  row[6], '.5f'), format(row[11], '.5f')))

    ##
    # RFC2812 - 3.1 Connection registration
    ##
    def cmd_nick(self, nick: str):
        '''Process NICK command.'''
        oldaddr = self.address
        oldnick = self.nick
        self.nick = nick
        if self.nick.lower() != oldnick.lower():
            self.server.users[self.nick] = self
            del self.server.users[oldnick]

        self.send_message('CMD_NICK', oldaddr=oldaddr, nick=self.nick)

    @log_exceptions
    def cmd_mode(self, target: str, *modes: Tuple[str]):
        '''Process MODE command.'''
        if target.lower() != self.nick.lower():
            if target[0] != '#':
                return
            if target not in self.channels:
                raise NotOnChannelError(channel=target)
            #logger.info('%s %s %s', self, modes, self.channels[target].mode)
            self.channels[target].mode(user=self, modes=modes)
            return

        if not modes:
            self.send_message('RPL_UMODEIS', modes=''.join(self.modes))
            return
        modes = modes[0]

        add = True
        usermodes = self.server.usermodes
        usermodes_restricted_add = self.server.usermodes_restricted_add
        usermodes_restricted_rem = self.server.usermodes_restricted_rem
        added = set()
        removed = set()
        for m in modes:
            if m == '+':
                add = True
                continue
            if m == '-':
                add = False
                continue
            if m in usermodes:
                if add and m not in usermodes_restricted_add:
                    if m not in self.modes:
                        self.modes.add(m)
                        added.add(m)
                        if m in removed:
                            removed.remove(m)
                if not add and m not in usermodes_restricted_add:
                    if m in self.modes:
                        self.modes.remove(m)
                        removed.add(m)
                        if m in added:
                            added.remove(m)
        added = ''.join(added)
        removed = ''.join(removed)
        action = ('+%s' % added) if added else ''
        action += ('-%s' % removed) if removed else ''
        self.send_message('CMD_MODE',
                          sender=self.nick,
                          recipient=self.nick,
                          modes=action)

    def cmd_quit(self, message: str = ''):
        '''Process QUIT command.'''
        message = 'Quit: %s' % message
        self.quit(message)

    ##
    # RFC2812 - 3.2 Channel operations
    ##
    @log_exceptions
    def cmd_join(self, channels: str, keys: Optional[str] = None):
        '''Process JOIN Command.'''
        if channels == '0':
            channels = [name for name in self.channels]
            channels = ','.join(channels)
            self.cmd_part(channels)
            return

        channels = channels.split(',')
        keys = keys.split(',') if keys else []
        chancatalog = self.server.channels

        # czipk = [('#chan1', 'key1'), ('#chan2', None), ...]
        czipk = zip(
            channels,
            keys + [None for _ in range(max(0,
                                            len(channels) - len(keys)))])
        for name, key in czipk:
            if not name:
                continue
            if key:
                chancatalog.join(user=self, name=name, key=key)
            else:
                chancatalog.join(user=self, name=name)

    @log_exceptions
    def cmd_part(self, channels: str, message: Optional[str] = None):
        '''Process PART command.'''
        channels = channels.split(',')

        # Part channels
        for name in channels:
            if name not in self.channels:
                raise NotOnChannelError(channel=name)
            channel = self.channels[name]
            channel.part(user=self, message=message)

    @log_exceptions
    def cmd_topic(self, channel: str, topic: Optional[str] = None):
        '''Process TOPIC command.'''
        if channel not in self.channels:
            raise NotOnChannelError(channel=channel)
        if topic == None:
            self.channels[channel].send_topic(user=self)
            return
        self.channels[channel].set_topic(user=self, topic=topic)

    @log_exceptions
    def cmd_names(self,
                  channels: Optional[str] = None,
                  target: Optional[str] = None):
        '''Process NAMES command.

        This method violates RFC2812 in the sense that it only sends
        the names list for channels in which the user are. This is done
        out of lazyness and performance concerns.
        '''
        userchannels = self.channels
        if not channels:
            chanlist = list(userchannels.keys())
        else:
            chanlist = channels.split(',')

        for name in chanlist:
            if name in userchannels:
                userchannels[name].send_names(user=self, suppress_end=True)
        self.send_message('RPL_ENDOFNAMES',
                          channel=channels if channels else '*')

    @log_exceptions
    def cmd_list(self,
                 channels: Optional[str] = None,
                 target: Optional[str] = None):
        '''Process LIST command.

        This method violates RFC2812 in the sense that it always sends 0
        as the number of visible members of the channel.
        '''
        chancatalog = self.server.channels
        for name in chancatalog:
            topic = chancatalog[name].topic
            self.send_message('RPL_LIST',
                              channel=name,
                              visible=0,
                              topic=topic if topic else '')
        self.send_message('RPL_LISTEND')

    ##
    # RFC2812 - 3.4 Server queries and commands
    ##
    def cmd_motd(self, target: Optional[str] = None):
        '''Process MOTD command.'''
        if not self.server.settings['motd']:
            raise NoMotdError()
        self.send_message('RPL_MOTDSTART')
        for text in self.server.settings['motd']:
            self.send_message('RPL_MOTD', text=text[0:min(len(text) + 1, 81)])
        self.send_message('RPL_ENDOFMOTD')

    def cmd_lusers(self,
                   mask: Optional[str] = None,
                   target: Optional[str] = None):
        '''Process LUSERS command.'''
        self.send_message('RPL_LUSERCLIENT',
                          usercount=len(self.server.users),
                          servicescount=0,
                          serverscount=1)
        self.send_message('RPL_LUSERME',
                          usercount=len(self.server.users),
                          serverscount=0)

    def cmd_version(self, target: Optional[str] = None):
        '''Process VERSION command.'''
        if target != None and target != self.server.name:
            raise NoSuchServerError(server=target)

        self.send_message('RPL_VERSION',
                          version=self.server.version,
                          servername=self.server.name,
                          comments='')

    ##
    # RFC2812 - 3.6 User based queries
    ##
    def cmd_whois(self, par1: str, par2: Optional[str] = None):
        '''Process WHOIS command.'''
        if par2:
            target = par1
            mask = par2
        else:
            target = None
            mask = par1
        nick = mask.split('!')[0]

        if nick not in self.server.users:
            raise NoSuchNickError(nick=nick)
        user = self.server.users[nick]

        self.send_message('RPL_WHOISUSER',
                          nick=user.nick,
                          username=user.username,
                          hostname=user.hostname,
                          realname=user.realname)
        self.send_message('RPL_WHOISSERVER',
                          nick=user.nick,
                          servername=user.servername,
                          serverinfo='')
        if user.channels:
            chandict = user.channels
            channels = (chandict[name].name for name in chandict)
            self.send_message('RPL_WHOISCHANNELS',
                              nick=user.nick,
                              iterator=channels)

        self.send_message('RPL_ENDOFWHOIS', nick=user.nick)

    ##
    # RFC2812 - 4. Optional features
    ##
    def cmd_ison(self, nick: str, *nicklist: List[str]):
        '''Process ISON command.'''
        nicklist = list(nicklist)
        nicklist.insert(0, nick)
        users = self.server.users
        online = [nick for nick in nicklist if nick in users]
        online = ' '.join(online)
        self.send_message('RPL_ISON', nicklist=online)
Пример #15
0
class excel_tab(excel):
    delimiter = Undefined(Any)
Пример #16
0
class FuncTransformer:
    """Transform methods for runtime type checking.
    
    This is used by DyncheckTransformVisitor and TypeTransformer is logically
    forms a single unit with these classes.
    """
    
    # Used for common transformation operations.
    tf = Undefined('mypy.transform.DyncheckTransformVisitor')
    
    def __init__(self, tf: 'mypy.transform.DyncheckTransformVisitor') -> None:
        self.tf = tf
    
    def transform_method(self, fdef: FuncDef) -> List[FuncDef]:
        """Transform a method.

        The result is one or more methods.
        """
        # Transform the body of the method.
        self.tf.transform_function_body(fdef)
        
        res = Undefined # type: List[FuncDef]
        
        if fdef.is_constructor():
            # The method is a constructor. Constructors are transformed to one
            # method.
            res = [self.transform_method_implementation(fdef, fdef.name())]
        else:
            # Normal methods are transformed to 1-3 variants. The
            # first is the main implementation of the method, and the
            # second is the dynamically-typed wrapper. The third
            # variant is for method overrides, and represents the
            # overridden supertype method.
            
            res = [self.transform_method_implementation(
                fdef, fdef.name() + self.tf.type_suffix(fdef))]
            
            if fdef.info.bases and fdef.info.mro[1].has_method(fdef.name()):
                # Override.
                # TODO do not assume single inheritance
                
                # Is is an override with a different signature? For
                # trivial overrides we can inherit wrappers.
                if not is_simple_override(fdef, fdef.info):
                    # Create a wrapper for overridden superclass method.
                    res.append(self.override_method_wrapper(fdef))
                    # Create a dynamically-typed method wrapper.
                    res.append(self.dynamic_method_wrapper(fdef))
            else:
                # Not an override.
                
                # Create a dynamically-typed method wrapper.
                res.append(self.dynamic_method_wrapper(fdef))
        
        return res
    
    def transform_method_implementation(self, fdef: FuncDef,
                                        name: str) -> FuncDef:
        """Transform the implementation of a method (i.e. unwrapped)."""
        args = fdef.args
        arg_kinds = fdef.arg_kinds
        
        typ = function_type(fdef) # type: Type
        init = fdef.init_expressions()
        
        if fdef.name() == '__init__' and is_generic(fdef):
            args, arg_kinds, init, typ = self.add_constructor_tvar_args(
                fdef, typ, args, arg_kinds, init)
        
        fdef2 = FuncDef(name, args, arg_kinds, init, fdef.body, typ)
        fdef2.info = fdef.info
        
        self.tf.prepend_generic_function_tvar_args(fdef2)
        
        return fdef2
    
    def add_constructor_tvar_args(
            self, fdef: FuncDef, typ: Type,
            args: List[Var], arg_kinds: List[int], 
            init: List[Node]) -> Tuple[List[Var], List[int], List[Node], Type]:
        """Add type variable arguments for __init__ of a generic type.

        Return tuple (new args, new kinds, new inits).
        """
        tv = [] # type: List[Var]
        ntvars = len(fdef.info.type_vars)
        for n in range(ntvars):
            tv.append(Var(tvar_arg_name(n + 1)))
            typ = add_arg_type_after_self(cast(Callable, typ), AnyType())
        args = [args[0]] + tv + args[1:]
        arg_kinds = [arg_kinds[0]] + [nodes.ARG_POS] * ntvars + arg_kinds[1:]
        init = List[Node]([None]) * ntvars + init
        return (args, arg_kinds, init, typ)
    
    def override_method_wrapper(self, fdef: FuncDef) -> FuncDef:
        """Construct a method wrapper for an overridden method."""
        orig_fdef = fdef.info.mro[1].get_method(fdef.name())
        return self.method_wrapper(cast(FuncDef, orig_fdef), fdef, False,
                                   False)
    
    def dynamic_method_wrapper(self, fdef: FuncDef) -> FuncDef:
        """Construct a dynamically typed method wrapper."""
        return self.method_wrapper(fdef, fdef, True, False)
    
    def generic_method_wrappers(self, fdef: FuncDef) -> List[Node]:
        """Construct wrapper class methods for a method of a generic class."""
        return [self.generic_static_method_wrapper(fdef),
                self.generic_dynamic_method_wrapper(fdef)]
    
    def generic_static_method_wrapper(self, fdef: FuncDef) -> FuncDef:
        """Construct statically typed wrapper class method."""
        return self.method_wrapper(fdef, fdef, False, True)
    
    def generic_dynamic_method_wrapper(self, fdef: FuncDef) -> FuncDef:
        """Construct dynamically-typed wrapper class method."""
        return self.method_wrapper(fdef, fdef, True, True)
    
    def method_wrapper(self, act_as_func_def: FuncDef,
                       target_func_def: FuncDef, is_dynamic: bool,
                       is_wrapper_class: bool) -> FuncDef:
        """Construct a method wrapper.

        It acts as a specific method (with the same signature), coerces
        arguments, calls the target method and finally coerces the return
        value.
        """
        is_override = act_as_func_def.info != target_func_def.info
        
        # Determine suffixes.
        target_suffix = self.tf.type_suffix(target_func_def)
        wrapper_suffix = self.get_wrapper_suffix(act_as_func_def, is_dynamic)
        
        # Determine function signatures.
        target_sig = self.get_target_sig(act_as_func_def, target_func_def,
                                         is_dynamic, is_wrapper_class)
        wrapper_sig = self.get_wrapper_sig(act_as_func_def, is_dynamic)
        call_sig = self.get_call_sig(act_as_func_def, target_func_def.info,
                                     is_dynamic, is_wrapper_class, is_override)
        
        if is_wrapper_class:
            bound_sig = cast(Callable,
                             translate_type_vars_to_bound_vars(target_sig))
        else:
            bound_sig = None
        
        call_stmt = self.call_wrapper(act_as_func_def, is_dynamic,
                                      is_wrapper_class, target_sig, call_sig,
                                      target_suffix, bound_sig)
        
        wrapper_args = self.get_wrapper_args(act_as_func_def, is_dynamic)    
        wrapper_func_def = FuncDef(act_as_func_def.name() + wrapper_suffix,
                                   wrapper_args,
                                   act_as_func_def.arg_kinds,
                                   [None] * len(wrapper_args),
                                   Block([call_stmt]),
                                   wrapper_sig)
        
        self.tf.add_line_mapping(target_func_def, wrapper_func_def)
        
        if is_wrapper_class and not is_dynamic:
            self.tf.prepend_generic_function_tvar_args(wrapper_func_def)
        
        return wrapper_func_def
    
    def get_target_sig(self, act_as_func_def: FuncDef,
                       target_func_def: FuncDef,
                       is_dynamic: bool, is_wrapper_class: bool) -> Callable:
        """Return the target method signature for a method wrapper."""
        sig = cast(Callable, function_type(target_func_def))
        if is_wrapper_class:
            if sig.is_generic() and is_dynamic:
                sig = cast(Callable,
                           translate_function_type_vars_to_dynamic(sig))
            return cast(Callable,
                        translate_type_vars_to_wrapped_object_vars(sig))
        elif is_dynamic:
            if sig.is_generic():
                return cast(Callable,
                            translate_function_type_vars_to_dynamic(sig))
            else:
                return sig
        else:
            return sig
    
    def get_wrapper_sig(self, act_as_func_def: FuncDef,
                        is_dynamic: bool) -> Callable:
        """Return the signature of the wrapper method.

        The wrapper method signature has an additional type variable
        argument (with type 'Any'), and all type variables have been
        erased.
        """
        sig = cast(Callable, function_type(act_as_func_def))
        if is_dynamic:
            return dynamic_sig(sig)
        elif is_generic(act_as_func_def):
            return cast(Callable, erase_generic_types(sig)) # FIX REFACTOR?
        else:
            return sig
    
    def get_call_sig(self, act_as_func_def: FuncDef,
                     current_class: TypeInfo, is_dynamic: bool,
                     is_wrapper_class: bool, is_override: bool) -> Callable:
        """Return the source signature in a wrapped call.
        
        It has type variables replaced with 'Any', but as an
        exception, type variables are intact in the return type in
        generic wrapper classes. The exception allows omitting an
        extra return value coercion, as the target return type and the
        source return type will be the same.
        """
        sig = cast(Callable, function_type(act_as_func_def))
        if is_dynamic:
            return dynamic_sig(sig)
        elif is_generic(act_as_func_def):
            call_sig = sig
            # If this is an override wrapper, keep type variables
            # intact. Otherwise replace them with dynamic to get
            # desired coercions that check argument types.
            if not is_override or is_wrapper_class:
                call_sig = (cast(Callable, replace_type_vars(call_sig, False)))
            else:
                call_sig = cast(Callable, map_type_from_supertype(
                    call_sig, current_class, act_as_func_def.info))
            if is_wrapper_class:
                # Replace return type with the original return within
                # wrapper classes to get rid of an unnecessary
                # coercion. There will still be a coercion due to the
                # extra coercion generated for generic wrapper
                # classes. However, function generic type variables
                # still need to be replaced, as the wrapper does not
                # affect them.
                ret = sig.ret_type
                if is_dynamic:
                    ret = translate_function_type_vars_to_dynamic(ret)
                call_sig = replace_ret_type(
                    call_sig, translate_type_vars_to_wrapper_vars(ret))
            return call_sig
        else:
            return sig
    
    def get_wrapper_args(self, act_as_func_def: FuncDef,
                         is_dynamic: bool) -> List[Var]:
        """Return the formal arguments of a wrapper method.

        These may include the type variable argument.
        """
        args = [] # type: List[Var]
        for a in act_as_func_def.args:
            args.append(Var(a.name()))
        return args
    
    def call_wrapper(self, fdef: FuncDef, is_dynamic: bool,
                     is_wrapper_class: bool, target_ann: Callable,
                     cur_ann: Callable, target_suffix: str,
                     bound_sig: Callable) -> Node:
        """Return the body of wrapper method.

        The body contains only a call to the wrapped method and a
        return statement (if the call returns a value). Arguments are coerced
        to the target signature.
        """        
        args = self.call_args(fdef.args, target_ann, cur_ann, is_dynamic,
                              is_wrapper_class, bound_sig,
                              ismethod=fdef.is_method())
        selfarg = args[0]
        args = args[1:]
        
        member = fdef.name() + target_suffix
        if not is_wrapper_class:
            callee = MemberExpr(selfarg, member)
        else:
            callee = MemberExpr(
                MemberExpr(self_expr(), self.tf.object_member_name()), member)
        
        call = CallExpr(callee,
                        args,
                        [nodes.ARG_POS] * len(args),
                        [None] * len(args)) # type: Node
        if bound_sig:
            call = self.tf.coerce(call, bound_sig.ret_type,
                                  target_ann.ret_type, self.tf.type_context(),
                                  is_wrapper_class)
            call = self.tf.coerce(call, cur_ann.ret_type, bound_sig.ret_type,
                                  self.tf.type_context(), is_wrapper_class)
        else:
            call = self.tf.coerce(call, cur_ann.ret_type, target_ann.ret_type,
                                  self.tf.type_context(), is_wrapper_class)
        if not isinstance(target_ann.ret_type, Void):
            return ReturnStmt(call)
        else:
            return ExpressionStmt(call)
    
    def call_args(self, vars: List[Var], target_ann: Callable,
                  cur_ann: Callable, is_dynamic: bool, is_wrapper_class: bool,
                  bound_sig: Callable = None,
                  ismethod: bool = False) -> List[Node]:
        """Construct the arguments of a wrapper call expression.

        Insert coercions as needed.
        """
        args = [] # type: List[Node]
        # Add ordinary arguments, including self (for methods).
        for i in range(len(vars)):
            a = vars[i]
            name = NameExpr(a.name())
            if bound_sig is None:
                args.append(self.tf.coerce(name, target_ann.arg_types[i],
                                           cur_ann.arg_types[i],
                                           self.tf.type_context(),
                                           is_wrapper_class))
            else:
                c = self.tf.coerce(name, bound_sig.arg_types[i],
                                   cur_ann.arg_types[i],
                                   self.tf.type_context(), is_wrapper_class)
                args.append(self.tf.coerce(c, target_ann.arg_types[i],
                                           bound_sig.arg_types[i],
                                           self.tf.type_context(),
                                           is_wrapper_class))
        # Add type variable arguments for a generic function.
        for i in range(len(target_ann.variables)):
            # Non-dynamic wrapper method in a wrapper class passes
            # generic function type arguments to the target function;
            # otherwise use dynamic types.
            index = i
            if ismethod:
                index += 1
            if is_wrapper_class and not is_dynamic:
                args.insert(index,
                    TypeExpr(RuntimeTypeVar(NameExpr(tvar_arg_name(-i - 1)))))
            else:
                args.insert(index, TypeExpr(AnyType()))
        return args
    
    def get_wrapper_suffix(self, func_def: FuncDef, is_dynamic: bool) -> str:
        if is_dynamic:
            return self.tf.dynamic_suffix()
        else:
            return self.tf.type_suffix(func_def)
Пример #17
0
    pass


# Return str/bytes
def check_output(args: Sequence[str],
                 *,
                 stdin: Any = None,
                 stderr: Any = None,
                 shell: bool = False,
                 universal_newlines: bool = False,
                 env: Mapping[str, str] = None) -> Any:
    pass


# TODO types
PIPE = Undefined(Any)
STDOUT = Undefined(Any)


class CalledProcessError(Exception):
    returncode = 0
    cmd = ''
    output = b''  # May be None


class Popen:
    stdin = Undefined(IO[Any])
    stdout = Undefined(IO[Any])
    stderr = Undefined(IO[Any])
    pid = 0
    returncode = 0
Пример #18
0
from typing import Undefined


class _Feature:
    pass


absolute_import = Undefined(_Feature)
division = Undefined(_Feature)
generators = Undefined(_Feature)
nested_scopes = Undefined(_Feature)
print_function = Undefined(_Feature)
unicode_literals = Undefined(_Feature)
with_statement = Undefined(_Feature)
Пример #19
0
class MessageBuilder:
    """Helper class for reporting type checker error messages with parameters.

    The methods of this class need to be provided with the context within a
    file; the errors member manages the wider context.

    IDEA: Support a 'verbose mode' that includes full information about types
          in error messages and that may otherwise produce more detailed error
          messages.
    """

    # Report errors using this instance. It knows about the current file and
    # import context.
    errors = Undefined(Errors)

    # Number of times errors have been disabled.
    disable_count = 0

    # Hack to deduplicate error messages from union types
    disable_type_names = 0

    def __init__(self, errors: Errors) -> None:
        self.errors = errors
        self.disable_count = 0
        self.disable_type_names = 0

    #
    # Helpers
    #

    def copy(self) -> 'MessageBuilder':
        return MessageBuilder(self.errors.copy())

    def add_errors(self, messages: 'MessageBuilder') -> None:
        """Add errors in messages to this builder."""
        self.errors.error_info.extend(messages.errors.error_info)

    def disable_errors(self) -> None:
        self.disable_count += 1

    def enable_errors(self) -> None:
        self.disable_count -= 1

    def is_errors(self) -> bool:
        return self.errors.is_errors()

    def fail(self, msg: str, context: Context) -> None:
        """Report an error message (unless disabled)."""
        if self.disable_count <= 0:
            self.errors.report(context.get_line(), msg.strip())

    def format(self, typ: Type) -> str:
        """Convert a type to a relatively short string that is
        suitable for error messages. Mostly behave like format_simple
        below, but never return an empty string.
        """
        s = self.format_simple(typ)
        if s != '':
            # If format_simple returns a non-trivial result, use that.
            return s
        elif isinstance(typ, FunctionLike):
            func = cast(FunctionLike, typ)
            if func.is_type_obj():
                # The type of a type object type can be derived from the
                # return type (this always works).
                itype = cast(Instance, func.items()[0].ret_type)
                return self.format(itype)
            elif isinstance(func, Callable):
                arg_types = map(self.format, func.arg_types)
                return_type = self.format(func.ret_type)
                return 'Function[[{}] -> {}]'.format(", ".join(arg_types), return_type)
            else:
                # Use a simple representation for function types; proper
                # function types may result in long and difficult-to-read
                # error messages.
                return 'functionlike'
        else:
            # Default case; we simply have to return something meaningful here.
            return 'object'

    def format_simple(self, typ: Type) -> str:
        """Convert simple types to string that is suitable for error messages.

        Return "" for complex types. Try to keep the length of the result
        relatively short to avoid overly long error messages.

        Examples:
          builtins.int -> 'int'
          Any type -> 'Any'
          void -> None
          function type -> "" (empty string)
        """
        if isinstance(typ, Instance):
            itype = cast(Instance, typ)
            # Get the short name of the type.
            base_str = itype.type.name()
            if itype.args == []:
                # No type arguments. Place the type name in quotes to avoid
                # potential for confusion: otherwise, the type name could be
                # interpreted as a normal word.
                return '"{}"'.format(base_str)
            elif itype.type.fullname() in reverse_type_aliases:
                alias = reverse_type_aliases[itype.type.fullname()]
                alias = alias.split('.')[-1]
                items = [strip_quotes(self.format(arg)) for arg in itype.args]
                return '{}[{}]'.format(alias, ', '.join(items))
            else:
                # There are type arguments. Convert the arguments to strings
                # (using format() instead of format_simple() to avoid empty
                # strings). If the result is too long, replace arguments
                # with [...].
                a = []  # type: List[str]
                for arg in itype.args:
                    a.append(strip_quotes(self.format(arg)))
                s = ', '.join(a)
                if len((base_str + s)) < 25:
                    return '{}[{}]'.format(base_str, s)
                else:
                    return '{}[...]'.format(base_str)
        elif isinstance(typ, TypeVar):
            # This is similar to non-generic instance types.
            return '"{}"'.format((cast(TypeVar, typ)).name)
        elif isinstance(typ, TupleType):
            items = []
            for t in (cast(TupleType, typ)).items:
                items.append(strip_quotes(self.format(t)))
            s = '"Tuple[{}]"'.format(', '.join(items))
            if len(s) < 40:
                return s
            else:
                return 'tuple(length {})'.format(len(items))
        elif isinstance(typ, UnionType):
            items = []
            for t in (cast(UnionType, typ)).items:
                items.append(strip_quotes(self.format(t)))
            s = '"Union[{}]"'.format(', '.join(items))
            if len(s) < 40:
                return s
            else:
                return 'union(length {})'.format(len(items))
        elif isinstance(typ, Void):
            return 'None'
        elif isinstance(typ, NoneTyp):
            return 'None'
        elif isinstance(typ, AnyType):
            return '"Any"'
        elif typ is None:
            raise RuntimeError('Type is None')
        else:
            # No simple representation for this type that would convey very
            # useful information. No need to mention the type explicitly in a
            # message.
            return ''

    #
    # Specific operations
    #

    # The following operations are for genering specific error messages. They
    # get some information as arguments, and they build an error message based
    # on them.

    def has_no_attr(self, typ: Type, member: str, context: Context) -> Type:
        """Report a missing or non-accessible member.

        The type argument is the base type. If member corresponds to
        an operator, use the corresponding operator name in the
        messages. Return type Any.
        """
        if (isinstance(typ, Instance) and
                (cast(Instance, typ)).type.has_readable_member(member)):
            self.fail('Member "{}" is not assignable'.format(member), context)
        elif isinstance(typ, Void):
            self.check_void(typ, context)
        elif member == '__contains__':
            self.fail('Unsupported right operand type for in ({})'.format(
                self.format(typ)), context)
        elif member in op_methods.values():
            # Access to a binary operator member (e.g. _add). This case does
            # not handle indexing operations.
            for op, method in op_methods.items():
                if method == member:
                    self.unsupported_left_operand(op, typ, context)
                    break
        elif member == '__neg__':
            self.fail('Unsupported operand type for unary - ({})'.format(
                self.format(typ)), context)
        elif member == '__pos__':
            self.fail('Unsupported operand type for unary + ({})'.format(
                self.format(typ)), context)
        elif member == '__invert__':
            self.fail('Unsupported operand type for ~ ({})'.format(
                self.format(typ)), context)
        elif member == '__getitem__':
            # Indexed get.
            self.fail('Value of type {} is not indexable'.format(
                self.format(typ)), context)
        elif member == '__setitem__':
            # Indexed set.
            self.fail('Unsupported target for indexed assignment', context)
        else:
            # The non-special case: a missing ordinary attribute.
            if not self.disable_type_names:
                self.fail('{} has no attribute "{}"'.format(self.format(typ),
                                                            member), context)
            else:
                self.fail('Some element of union has no attribute "{}"'.format(
                    member), context)
        return AnyType()

    def unsupported_operand_types(self, op: str, left_type: Any,
                                  right_type: Any, context: Context) -> None:
        """Report unsupported operand types for a binary operation.

        Types can be Type objects or strings.
        """
        if isinstance(left_type, Void) or isinstance(right_type, Void):
            self.check_void(left_type, context)
            self.check_void(right_type, context)
            return
        left_str = ''
        if isinstance(left_type, str):
            left_str = left_type
        else:
            left_str = self.format(left_type)

        right_str = ''
        if isinstance(right_type, str):
            right_str = right_type
        else:
            right_str = self.format(right_type)

        if self.disable_type_names:
            msg = 'Unsupported operand types for {} (likely involving Union)'.format(op)
        else:
            msg = 'Unsupported operand types for {} ({} and {})'.format(
                op, left_str, right_str)
        self.fail(msg, context)

    def unsupported_left_operand(self, op: str, typ: Type,
                                 context: Context) -> None:
        if not self.check_void(typ, context):
            if self.disable_type_names:
                msg = 'Unsupported left operand type for {} (some union)'.format(op)
            else:
                msg = 'Unsupported left operand type for {} ({})'.format(
                    op, self.format(typ))
            self.fail(msg, context)

    def type_expected_as_right_operand_of_is(self, context: Context) -> None:
        self.fail('Type expected as right operand of "is"', context)

    def not_callable(self, typ: Type, context: Context) -> Type:
        self.fail('{} not callable'.format(self.format(typ)), context)
        return AnyType()

    def incompatible_argument(self, n: int, callee: Callable, arg_type: Type,
                              context: Context) -> None:
        """Report an error about an incompatible argument type.

        The argument type is arg_type, argument number is n and the
        callee type is 'callee'. If the callee represents a method
        that corresponds to an operator, use the corresponding
        operator name in the messages.
        """
        target = ''
        if callee.name:
            name = callee.name
            base = extract_type(name)

            for op, method in op_methods.items():
                for variant in method, '__r' + method[2:]:
                    if name.startswith('"{}" of'.format(variant)):
                        if op == 'in' or variant != method:
                            # Reversed order of base/argument.
                            self.unsupported_operand_types(op, arg_type, base,
                                                           context)
                        else:
                            self.unsupported_operand_types(op, base, arg_type,
                                                           context)
                        return

            if name.startswith('"__getitem__" of'):
                self.invalid_index_type(arg_type, base, context)
                return

            if name.startswith('"__setitem__" of'):
                if n == 1:
                    self.invalid_index_type(arg_type, base, context)
                else:
                    self.fail(INCOMPATIBLE_TYPES_IN_ASSIGNMENT, context)
                return

            target = 'to {} '.format(name)

        msg = ''
        if callee.name == '<list>':
            name = callee.name[1:-1]
            msg = '{} item {} has incompatible type {}'.format(
                name[0].upper() + name[1:], n, self.format_simple(arg_type))
        elif callee.name == '<list-comprehension>':
            msg = 'List comprehension has incompatible type List[{}]'.format(
                strip_quotes(self.format(arg_type)))
        elif callee.name == '<generator>':
            msg = 'Generator has incompatible item type {}'.format(
                self.format_simple(arg_type))
        else:
            try:
                expected_type = callee.arg_types[n - 1]
            except IndexError:  # Varargs callees
                expected_type = callee.arg_types[-1]
            msg = 'Argument {} {}has incompatible type {}; expected {}'.format(
                n, target, self.format(arg_type), self.format(expected_type))
        self.fail(msg, context)

    def invalid_index_type(self, index_type: Type, base_str: str,
                           context: Context) -> None:
        self.fail('Invalid index type {} for {}'.format(
            self.format(index_type), base_str), context)

    def invalid_argument_count(self, callee: Callable, num_args: int,
                               context: Context) -> None:
        if num_args < len(callee.arg_types):
            self.too_few_arguments(callee, context)
        else:
            self.too_many_arguments(callee, context)

    def too_few_arguments(self, callee: Callable, context: Context) -> None:
        msg = 'Too few arguments'
        if callee.name:
            msg += ' for {}'.format(callee.name)
        self.fail(msg, context)

    def too_many_arguments(self, callee: Callable, context: Context) -> None:
        msg = 'Too many arguments'
        if callee.name:
            msg += ' for {}'.format(callee.name)
        self.fail(msg, context)

    def too_many_positional_arguments(self, callee: Callable,
                                      context: Context) -> None:
        msg = 'Too many positional arguments'
        if callee.name:
            msg += ' for {}'.format(callee.name)
        self.fail(msg, context)

    def unexpected_keyword_argument(self, callee: Callable, name: str,
                                    context: Context) -> None:
        msg = 'Unexpected keyword argument "{}"'.format(name)
        if callee.name:
            msg += ' for {}'.format(callee.name)
        self.fail(msg, context)

    def duplicate_argument_value(self, callee: Callable, index: int,
                                 context: Context) -> None:
        self.fail('{} gets multiple values for keyword argument "{}"'.
                  format(capitalize(callable_name(callee)),
                         callee.arg_names[index]), context)

    def does_not_return_value(self, void_type: Type, context: Context) -> None:
        """Report an error about a void type in a non-void context.

        The first argument must be a void type. If the void type has a
        source in it, report it in the error message. This allows
        giving messages such as 'Foo does not return a value'.
        """
        if (cast(Void, void_type)).source is None:
            self.fail('Function does not return a value', context)
        else:
            self.fail('{} does not return a value'.format(
                capitalize((cast(Void, void_type)).source)), context)

    def no_variant_matches_arguments(self, overload: Overloaded,
                                     context: Context) -> None:
        if overload.name():
            self.fail('No overload variant of {} matches argument types'
                      .format(overload.name()), context)
        else:
            self.fail('No overload variant matches argument types', context)

    def function_variants_overlap(self, n1: int, n2: int,
                                  context: Context) -> None:
        self.fail('Function signature variants {} and {} overlap'.format(
            n1 + 1, n2 + 1), context)

    def invalid_cast(self, target_type: Type, source_type: Type,
                     context: Context) -> None:
        if not self.check_void(source_type, context):
            self.fail('Cannot cast from {} to {}'.format(
                self.format(source_type), self.format(target_type)), context)

    def incompatible_operator_assignment(self, op: str,
                                         context: Context) -> None:
        self.fail('Result type of {} incompatible in assignment'.format(op),
                  context)

    def incompatible_value_count_in_assignment(self, lvalue_count: int,
                                               rvalue_count: int,
                                               context: Context) -> None:
        if rvalue_count < lvalue_count:
            self.fail('Need {} values to assign'.format(lvalue_count), context)
        elif rvalue_count > lvalue_count:
            self.fail('Too many values to assign', context)

    def type_incompatible_with_supertype(self, name: str, supertype: TypeInfo,
                                         context: Context) -> None:
        self.fail('Type of "{}" incompatible with supertype "{}"'.format(
            name, supertype.name), context)

    def signature_incompatible_with_supertype(
            self, name: str, name_in_super: str, supertype: str,
            context: Context) -> None:
        target = self.override_target(name, name_in_super, supertype)
        self.fail('Signature of "{}" incompatible with {}'.format(
            name, target), context)

    def argument_incompatible_with_supertype(
            self, arg_num: int, name: str, name_in_supertype: str,
            supertype: str, context: Context) -> None:
        target = self.override_target(name, name_in_supertype, supertype)
        self.fail('Argument {} of "{}" incompatible with {}'
                  .format(arg_num, name, target), context)

    def return_type_incompatible_with_supertype(
            self, name: str, name_in_supertype: str, supertype: str,
            context: Context) -> None:
        target = self.override_target(name, name_in_supertype, supertype)
        self.fail('Return type of "{}" incompatible with {}'
                  .format(name, target), context)

    def override_target(self, name: str, name_in_super: str,
                        supertype: str) -> str:
        target = 'supertype "{}"'.format(supertype)
        if name_in_super != name:
            target = '"{}" of {}'.format(name_in_super, target)
        return target

    def boolean_return_value_expected(self, method: str,
                                      context: Context) -> None:
        self.fail('Boolean return value expected for method "{}"'.format(
            method), context)

    def incompatible_type_application(self, expected_arg_count: int,
                                      actual_arg_count: int,
                                      context: Context) -> None:
        if expected_arg_count == 0:
            self.fail('Type application targets a non-generic function',
                      context)
        elif actual_arg_count > expected_arg_count:
            self.fail('Type application has too many types ({} expected)'
                      .format(expected_arg_count), context)
        else:
            self.fail('Type application has too few types ({} expected)'
                      .format(expected_arg_count), context)

    def incompatible_array_item_type(self, typ: Type, index: int,
                                     context: Context) -> None:
        self.fail('Array item {} has incompatible type {}'.format(
            index, self.format(typ)), context)

    def could_not_infer_type_arguments(self, callee_type: Callable, n: int,
                                       context: Context) -> None:
        if callee_type.name and n > 0:
            self.fail('Cannot infer type argument {} of {}'.format(
                n, callee_type.name), context)
        else:
            self.fail('Cannot infer function type argument', context)

    def invalid_var_arg(self, typ: Type, context: Context) -> None:
        self.fail('List or tuple expected as variable arguments', context)

    def invalid_keyword_var_arg(self, typ: Type, context: Context) -> None:
        if isinstance(typ, Instance) and (
                (cast(Instance, typ)).type.fullname() == 'builtins.dict'):
            self.fail('Keywords must be strings', context)
        else:
            self.fail('Argument after ** must be a dictionary',
                      context)

    def incomplete_type_var_match(self, member: str, context: Context) -> None:
        self.fail('"{}" has incomplete match to supertype type variable'
                  .format(member), context)

    def not_implemented(self, msg: str, context: Context) -> Type:
        self.fail('Feature not implemented yet ({})'.format(msg), context)
        return AnyType()

    def undefined_in_superclass(self, member: str, context: Context) -> None:
        self.fail('"{}" undefined in superclass'.format(member), context)

    def check_void(self, typ: Type, context: Context) -> bool:
        """If type is void, report an error such as '.. does not
        return a value' and return True. Otherwise, return False.
        """
        if isinstance(typ, Void):
            self.does_not_return_value(typ, context)
            return True
        else:
            return False

    def cannot_determine_type(self, name: str, context: Context) -> None:
        self.fail("Cannot determine type of '%s'" % name, context)

    def invalid_method_type(self, sig: Callable, context: Context) -> None:
        self.fail('Invalid method type', context)

    def incompatible_conditional_function_def(self, defn: FuncDef) -> None:
        self.fail('All conditional function variants must have identical '
                  'signatures', defn)

    def cannot_instantiate_abstract_class(self, class_name: str,
                                          abstract_attributes: List[str],
                                          context: Context) -> None:
        attrs = format_string_list("'%s'" % a for a in abstract_attributes[:5])
        self.fail("Cannot instantiate abstract class '%s' with abstract "
                  "method%s %s" % (class_name, plural_s(abstract_attributes),
                                   attrs),
                  context)

    def base_class_definitions_incompatible(self, name: str, base1: TypeInfo,
                                            base2: TypeInfo,
                                            context: Context) -> None:
        self.fail('Definition of "{}" in base class "{}" is incompatible '
                  'with definition in base class "{}"'.format(
                      name, base1.name(), base2.name()), context)

    def cant_assign_to_method(self, context: Context) -> None:
        self.fail(CANNOT_ASSIGN_TO_METHOD, context)

    def read_only_property(self, name: str, type: TypeInfo,
                           context: Context) -> None:
        self.fail('Property "{}" defined in "{}" is read-only'.format(
            name, type.name()), context)

    def incompatible_typevar_value(self, callee: Callable, index: int,
                                   type: Type, context: Context) -> None:
        self.fail('Type argument {} of {} has incompatible value {}'.format(
            index, callable_name(callee), self.format(type)), context)

    def disjointness_violation(self, cls: TypeInfo, disjoint: TypeInfo,
                               context: Context) -> None:
        self.fail('disjointclass constraint of class {} disallows {} as a '
                  'base class'.format(cls.name(), disjoint.name()), context)

    def overloaded_signatures_overlap(self, index1: int, index2: int,
                                      context: Context) -> None:
        self.fail('Overloaded function signatures {} and {} overlap with '
                  'incompatible return types'.format(index1, index2), context)

    def invalid_reverse_operator_signature(self, reverse: str, other: str,
                                           context: Context) -> None:
        self.fail('"Any" return type expected since argument to {} does not '
                  'support {}'.format(reverse, other), context)

    def reverse_operator_method_with_any_arg_must_return_any(
            self, method: str, context: Context) -> None:
        self.fail('"Any" return type expected since argument to {} has type '
                  '"Any"'.format(method), context)

    def operator_method_signatures_overlap(
            self, reverse_class: str, reverse_method: str, forward_class: str,
            forward_method: str, context: Context) -> None:
        self.fail('Signatures of "{}" of "{}" and "{}" of "{}" are unsafely '
                  'overlapping'.format(reverse_method, reverse_class,
                                       forward_method, forward_class), context)

    def signatures_incompatible(self, method: str, other_method: str,
                                context: Context) -> None:
        self.fail('Signatures of "{}" and "{}" are incompatible'.format(
            method, other_method), context)
Пример #20
0
class DataDrivenTestCase(TestCase):
    input = Undefined(List[str])
    output = Undefined(List[str])

    file = ''
    line = 0

    perform = Undefined(Function[['DataDrivenTestCase'], None])

    # (file path, file content) tuples
    files = Undefined(List[Tuple[str, str]])

    clean_up = Undefined(List[Tuple[bool, str]])

    def __init__(self, name, input, output, file, line, lastline, perform,
                 files):
        super().__init__(name)
        self.input = input
        self.output = output
        self.lastline = lastline
        self.file = file
        self.line = line
        self.perform = perform
        self.files = files

    def set_up(self) -> None:
        super().set_up()
        self.clean_up = []
        for path, content in self.files:
            dir = os.path.dirname(path)
            for d in self.add_dirs(dir):
                self.clean_up.append((True, d))
            f = open(path, 'w')
            f.write(content)
            f.close()
            self.clean_up.append((False, path))

    def add_dirs(self, dir: str) -> List[str]:
        """Add all subdirectories required to create dir.

        Return an array of the created directories in the order of creation.
        """
        if dir == '' or os.path.isdir(dir):
            return []
        else:
            dirs = self.add_dirs(os.path.dirname(dir)) + [dir]
            os.mkdir(dir)
            return dirs

    def run(self):
        if self.name.endswith('-skip'):
            raise SkipTestCaseException()
        else:
            self.perform(self)

    def tear_down(self) -> None:
        # First remove files.
        for is_dir, path in reversed(self.clean_up):
            if not is_dir:
                remove(path)
        # Then remove directories.
        for is_dir, path in reversed(self.clean_up):
            if is_dir:
                pycache = os.path.join(path, '__pycache__')
                if os.path.isdir(pycache):
                    shutil.rmtree(pycache)
                try:
                    rmdir(path)
                except OSError as error:
                    print(' ** Error removing directory %s -- contents:' %
                          path)
                    for item in os.listdir(path):
                        print('  ', item)
                    # Most likely, there are some files in the
                    # directory. Use rmtree to nuke the directory, but
                    # fail the test case anyway, since this seems like
                    # a bug in a test case -- we shouldn't leave
                    # garbage lying around. By nuking the directory,
                    # the next test run hopefully passes.
                    path = error.filename
                    # Be defensive -- only call rmtree if we're sure we aren't removing anything
                    # valuable.
                    if path.startswith('tmp/') and os.path.isdir(path):
                        shutil.rmtree(path)
                    raise
        super().tear_down()
Пример #21
0
 def index(self, object: _T, start: int = 0, stop: int = Undefined(int)) -> int: pass
 def count(self, object: _T) -> int: pass
Пример #22
0
# Stubs for requests.packages.urllib3.util.retry (Python 3.4)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.

from typing import Undefined, Any
from .. import exceptions
from .. import packages

ConnectTimeoutError = exceptions.ConnectTimeoutError
MaxRetryError = exceptions.MaxRetryError
ProtocolError = exceptions.ProtocolError
ReadTimeoutError = exceptions.ReadTimeoutError
ResponseError = exceptions.ResponseError

log = Undefined(Any)


class Retry:
    DEFAULT_METHOD_WHITELIST = Undefined(Any)
    BACKOFF_MAX = Undefined(Any)
    total = Undefined(Any)
    connect = Undefined(Any)
    read = Undefined(Any)
    redirect = Undefined(Any)
    status_forcelist = Undefined(Any)
    method_whitelist = Undefined(Any)
    backoff_factor = Undefined(Any)
    raise_on_redirect = Undefined(Any)

    def __init__(self,
                 total=10,
Пример #23
0
class TestBasicOps(unittest.TestCase):
    # Superclass with tests common to all generators.
    # Subclasses must arrange for self.gen to retrieve the Random instance
    # to be tested.

    gen = Undefined(Any) # Either Random or SystemRandom 

    def randomlist(self, n: int) -> List[float]:
        """Helper function to make a list of random numbers"""
        return [self.gen.random() for i in range(n)]

    def test_autoseed(self) -> None:
        self.gen.seed()
        state1 = self.gen.getstate()
        time.sleep(0.1)
        self.gen.seed()      # diffent seeds at different times
        state2 = self.gen.getstate()
        self.assertNotEqual(state1, state2)

    def test_saverestore(self) -> None:
        N = 1000
        self.gen.seed()
        state = self.gen.getstate()
        randseq = self.randomlist(N)
        self.gen.setstate(state)    # should regenerate the same sequence
        self.assertEqual(randseq, self.randomlist(N))

    def test_seedargs(self) -> None:
        for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20),
                    3.14, complex(1., 2.), 'a', tuple('abc')]:
            self.gen.seed(arg)
        for arg in [list(range(3)), {'one': 1}]:
            self.assertRaises(TypeError, self.gen.seed, arg)
        self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
        self.assertRaises(TypeError, type(self.gen), [])

    def test_choice(self) -> None:
        choice = self.gen.choice
        with self.assertRaises(IndexError):
            choice([])
        self.assertEqual(choice([50]), 50)
        self.assertIn(choice([25, 75]), [25, 75])

    def test_sample(self) -> None:
        # For the entire allowable range of 0 <= k <= N, validate that
        # the sample is of the correct length and contains only unique items
        N = 100
        population = range(N)
        for k in range(N+1):
            s = self.gen.sample(population, k)
            self.assertEqual(len(s), k)
            uniq = set(s)
            self.assertEqual(len(uniq), k)
            self.assertTrue(uniq <= set(population))
        self.assertEqual(self.gen.sample([], 0), [])  # test edge case N==k==0

    def test_sample_distribution(self) -> None:
        # For the entire allowable range of 0 <= k <= N, validate that
        # sample generates all possible permutations
        n = 5
        pop = range(n)
        trials = 10000  # large num prevents false negatives without slowing normal case
        def factorial(n: int) -> int:
            if n == 0:
                return 1
            return n * factorial(n - 1)
        for k in range(n):
            expected = factorial(n) // factorial(n-k)
            perms = Dict[tuple, object]()
            for i in range(trials):
                perms[tuple(self.gen.sample(pop, k))] = None
                if len(perms) == expected:
                    break
            else:
                self.fail()

    def test_sample_inputs(self) -> None:
        # SF bug #801342 -- population can be any iterable defining __len__()
        self.gen.sample(set(range(20)), 2)
        self.gen.sample(range(20), 2)
        self.gen.sample(range(20), 2)
        self.gen.sample(str('abcdefghijklmnopqrst'), 2)
        self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)

    def test_sample_on_dicts(self) -> None:
        self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)

    def test_gauss(self) -> None:
        # Ensure that the seed() method initializes all the hidden state.  In
        # particular, through 2.2.1 it failed to reset a piece of state used
        # by (and only by) the .gauss() method.

        for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
            self.gen.seed(seed)
            x1 = self.gen.random()
            y1 = self.gen.gauss(0, 1)

            self.gen.seed(seed)
            x2 = self.gen.random()
            y2 = self.gen.gauss(0, 1)

            self.assertEqual(x1, x2)
            self.assertEqual(y1, y2)

    def test_pickling(self) -> None:
        state = pickle.dumps(self.gen)
        origseq = [self.gen.random() for i in range(10)]
        newgen = pickle.loads(state)
        restoredseq = [newgen.random() for i in range(10)]
        self.assertEqual(origseq, restoredseq)

    def test_bug_1727780(self) -> None:
        # verify that version-2-pickles can be loaded
        # fine, whether they are created on 32-bit or 64-bit
        # platforms, and that version-3-pickles load fine.
        files = [("randv2_32.pck", 780),
                 ("randv2_64.pck", 866),
                 ("randv3.pck", 343)]
        for file, value in files:
            f = open(support.findfile(file),"rb")
            r = pickle.load(f)
            f.close()
            self.assertEqual(int(r.random()*1000), value)

    def test_bug_9025(self) -> None:
        # Had problem with an uneven distribution in int(n*random())
        # Verify the fix by checking that distributions fall within expectations.
        n = 100000
        randrange = self.gen.randrange
        k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
        self.assertTrue(0.30 < k/n < .37, (k/n))
Пример #24
0
class State:
    """Abstract base class for build states.

    There is always at most one state per source file.
    """

    # The StateInfo attributes are duplicated here for convenience.
    path = Undefined(str)
    id = Undefined(str)
    import_context = Undefined(List[Tuple[str, int]])
    manager = Undefined(BuildManager)
    # Modules that this file directly depends on (in no particular order).
    dependencies = Undefined(List[str])
    
    def __init__(self, info: StateInfo) -> None:
        self.path = info.path
        self.id = info.id
        self.import_context = info.import_context
        self.manager = info.manager
        self.dependencies = []
    
    def info(self) -> StateInfo:
        return StateInfo(self.path, self.id, self.import_context, self.manager)
    
    def process(self) -> None:
        raise RuntimeError('Not implemented')
    
    def is_ready(self) -> bool:
        """Return True if all dependencies are at least in the same state
        as this object (but not in the initial state).
        """
        for module in self.dependencies:
            state = self.manager.module_state(module)
            if earlier_state(state,
                             self.state()) or state == UNPROCESSED_STATE:
                return False
        return True

    def num_incomplete_deps(self) -> int:
        """Return the number of dependencies that are ready but incomplete."""
        return 0 # Does not matter in this state
    
    def state(self) -> int:
        raise RuntimeError('Not implemented')
    
    def switch_state(self, state_object: 'State') -> None:
        """Called by state objects to replace the state of the file.

        Also notify the manager.
        """
        for i in range(len(self.manager.states)):
            if self.manager.states[i].path == state_object.path:
                self.manager.states[i] = state_object
                return 
        raise RuntimeError('State for {} not found'.format(state_object.path))
    
    def errors(self) -> Errors:
        return self.manager.errors
    
    def semantic_analyzer(self) -> SemanticAnalyzer:
        return self.manager.semantic_analyzer
    
    def semantic_analyzer_pass3(self) -> ThirdPass:
        return self.manager.semantic_analyzer_pass3
    
    def type_checker(self) -> TypeChecker:
        return self.manager.type_checker
    
    def fail(self, path: str, line: int, msg: str) -> None:
        """Report an error in the build (e.g. if could not find a module)."""
        self.errors().set_file(path)
        self.errors().report(line, msg)
Пример #25
0
# Stubs for requests.packages.urllib3 (Python 3.4)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.

from typing import Undefined, Any
from . import connectionpool
from . import filepost
from . import poolmanager
from . import response
from .util import request as _request
from .util import url
from .util import timeout
from .util import retry
import logging

__license__ = Undefined(Any)

HTTPConnectionPool = connectionpool.HTTPConnectionPool
HTTPSConnectionPool = connectionpool.HTTPSConnectionPool
connection_from_url = connectionpool.connection_from_url
encode_multipart_formdata = filepost.encode_multipart_formdata
PoolManager = poolmanager.PoolManager
ProxyManager = poolmanager.ProxyManager
proxy_from_url = poolmanager.proxy_from_url
HTTPResponse = response.HTTPResponse
make_headers = _request.make_headers
get_host = url.get_host
Timeout = timeout.Timeout
Retry = retry.Retry

Пример #26
0
    pass


class SSLWantReadError(SSLError):
    pass


class SSLWantWriteError(SSLError):
    pass


class SSLZeroReturnError(SSLError):
    pass


OPENSSL_VERSION = Undefined(str)
OPENSSL_VERSION_INFO = Undefined(Any)
OPENSSL_VERSION_NUMBER = Undefined(int)

VERIFY_CRL_CHECK_CHAIN = Undefined(int)
VERIFY_CRL_CHECK_LEAF = Undefined(int)
VERIFY_DEFAULT = Undefined(int)
VERIFY_X509_STRICT = Undefined(int)

ALERT_DESCRIPTION_ACCESS_DENIED = Undefined(int)
ALERT_DESCRIPTION_BAD_CERTIFICATE = Undefined(int)
ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE = Undefined(int)
ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE = Undefined(int)
ALERT_DESCRIPTION_BAD_RECORD_MAC = Undefined(int)
ALERT_DESCRIPTION_CERTIFICATE_EXPIRED = Undefined(int)
ALERT_DESCRIPTION_CERTIFICATE_REVOKED = Undefined(int)
Пример #27
0
# Stubs for sys
# Ron Murawski <*****@*****.**>

# based on http://docs.python.org/3.2/library/sys.html

from typing import (Undefined, List, Sequence, Any, Dict, Tuple, TextIO,
                    overload, Optional)
from types import TracebackType

# ----- sys variables -----
abiflags = ''
argv = Undefined(List[str])
byteorder = ''
builtin_module_names = Undefined(Sequence[str])  # actually a tuple of strings
copyright = ''
#dllhandle = 0  # Windows only
dont_write_bytecode = False
__displayhook__ = Undefined(Any)  # contains the original value of displayhook
__excepthook__ = Undefined(Any)  # contains the original value of excepthook
exec_prefix = ''
executable = ''
float_repr_style = ''
hexversion = 0  # this is a 32-bit int
last_type = Undefined(Any)
last_value = Undefined(Any)
last_traceback = Undefined(Any)
maxsize = 0
maxunicode = 0
meta_path = Undefined(List[Any])
modules = Undefined(Dict[str, Any])
path = Undefined(List[str])
Пример #28
0
class Purpose(_ASN1Object, _Enum):
    SERVER_AUTH = Undefined(Any)
    CLIENT_AUTH = Undefined(Any)
Пример #29
0
# Stubs for errno

# Based on http://docs.python.org/3.2/library/errno.html

from typing import Undefined, Dict

errorcode = Undefined(Dict[int, str])

# TODO some of the names below are platform specific

EPERM = 0
ENOENT = 0
ESRCH = 0
EINTR = 0
EIO = 0
ENXIO = 0
E2BIG = 0
ENOEXEC = 0
EBADF = 0
ECHILD = 0
EAGAIN = 0
ENOMEM = 0
EACCES = 0
EFAULT = 0
ENOTBLK = 0
EBUSY = 0
EEXIST = 0
EXDEV = 0
ENODEV = 0
ENOTDIR = 0
EISDIR = 0
Пример #30
0
class SSLSocket(socket):
    keyfile = Undefined(Any)
    certfile = Undefined(Any)
    cert_reqs = Undefined(Any)
    ssl_version = Undefined(Any)
    ca_certs = Undefined(Any)
    ciphers = Undefined(Any)
    server_side = Undefined(Any)
    server_hostname = Undefined(Any)
    do_handshake_on_connect = Undefined(Any)
    suppress_ragged_eofs = Undefined(Any)
    context = Undefined(Any)  # TODO: This should be a property.

    def __init__(self,
                 sock=None,
                 keyfile=None,
                 certfile=None,
                 server_side=False,
                 cert_reqs=Undefined,
                 ssl_version=Undefined,
                 ca_certs=None,
                 do_handshake_on_connect=True,
                 family=Undefined,
                 type=Undefined,
                 proto=0,
                 fileno=None,
                 suppress_ragged_eofs=True,
                 npn_protocols=None,
                 ciphers=None,
                 server_hostname=None,
                 _context=None):
        pass

    def dup(self):
        pass

    def read(self, len=0, buffer=None):
        pass

    def write(self, data):
        pass

    def getpeercert(self, binary_form=False):
        pass

    def selected_npn_protocol(self):
        pass

    def cipher(self):
        pass

    def compression(self):
        pass

    def send(self, data, flags=0):
        pass

    def sendto(self, data, flags_or_addr, addr=None):
        pass

    def sendmsg(self, *args, **kwargs):
        pass

    def sendall(self, data, flags=0):
        pass

    def recv(self, buflen=1024, flags=0):
        pass

    def recv_into(self, buffer, nbytes=None, flags=0):
        pass

    def recvfrom(self, buflen=1024, flags=0):
        pass

    def recvfrom_into(self, buffer, nbytes=None, flags=0):
        pass

    def recvmsg(self, *args, **kwargs):
        pass

    def recvmsg_into(self, *args, **kwargs):
        pass

    def pending(self):
        pass

    def shutdown(self, how):
        pass

    def unwrap(self):
        pass

    def do_handshake(self, block=False):
        pass

    def connect(self, addr):
        pass

    def connect_ex(self, addr):
        pass

    def accept(self):
        pass

    def get_channel_binding(self, cb_type=''):
        pass