Python ast 模块,Ellipsis() 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用ast.Ellipsis()

项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_Subscript(self, ctx, e):
        slice_ = e.slice 
        if isinstance(slice_, ast.Ellipsis):
            raise _errors.TyError("stringing slice cannot be an Ellipsis.", e)
        elif isinstance(slice_, ast.ExtSlice):
            raise _errors.TyError("stringing slice can only have one dimension.", e)
        elif isinstance(slice_, ast.Index):
            ctx.ana(slice_.value, _numeric.num)
        else: # if isinstance(slice_, ast.Slice):
            lower, upper, step = slice_.lower, slice_.upper, slice_.step
            if lower is not None:
                ctx.ana(lower, _numeric.num)
            if upper is not None:
                ctx.ana(upper, _numeric.num)
            if not _is_None(step):
                ctx.ana(step, _numeric.num)
        return self
项目:vulture    作者:jendrikseipp    | 项目源码 | 文件源码
def test_size_ellipsis():
    example = """
class Foo:
    bar[1:2,
        ...]
"""
    if sys.version_info < (3, 0):
        check_size(example, 2)
    else:
        # ast.Ellipsis is a subclass of ast.expr in Python 3.
        check_size(example, 3)
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def make_unconcat_slice(axis, lower, upper):
    dims = []
    for i in range(axis):
        dims.append(ast.Slice(lower=None, upper=None, step=None))
    dims.append(ast.Slice(lower=lower, upper=upper, step=None))
    dims.append(ast.Ellipsis())

    ext_slice = ast.ExtSlice(dims=dims)

    return ext_slice
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def is_stub(node):
    """Check if the function is a stub definition:

    For the function to be a stub, it should be fully annotated and should have no body.
    The body should be a single `Pass` statement with optional docstring.
    """
    if not is_annotated(node):
        return False

    if len(node.body) == 1 and isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Ellipsis):
        return True
    return ((len(node.body) == 1 and isinstance(node.body[0], ast.Pass))
            or (len(node.body) == 2 and isinstance(node.body[0], ast.Expr) and isinstance(node.body[1], ast.Pass)))
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def init_inc_idx(cls, inc_idx):
        if inc_idx != () and inc_idx != Ellipsis:
            raise typy.TypeFormationError("Incomplete index of float_ type must be () or Ellipsis.")
        return inc_idx
项目:bandit-ss    作者:zeroSteiner    | 项目源码 | 文件源码
def test__get_literal_value(self):
        new_context = context.Context()

        value = ast.Num(42)
        expected = value.n
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.Str('spam')
        expected = value.s
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.List([ast.Str('spam'), ast.Num(42)], ast.Load())
        expected = [ast.Str('spam').s, ast.Num(42).n]
        self.assertListEqual(expected, new_context._get_literal_value(value))

        value = ast.Tuple([ast.Str('spam'), ast.Num(42)], ast.Load())
        expected = (ast.Str('spam').s, ast.Num(42).n)
        self.assertTupleEqual(expected, new_context._get_literal_value(value))

        value = ast.Set([ast.Str('spam'), ast.Num(42)])
        expected = set([ast.Str('spam').s, ast.Num(42).n])
        self.assertSetEqual(expected, new_context._get_literal_value(value))

        value = ast.Dict(['spam', 'eggs'], [42, 'foo'])
        expected = dict(spam=42, eggs='foo')
        self.assertDictEqual(expected, new_context._get_literal_value(value))

        value = ast.Ellipsis()
        self.assertIsNone(new_context._get_literal_value(value))

        value = ast.Name('spam', ast.Load())
        expected = value.id
        self.assertEqual(expected, new_context._get_literal_value(value))

        if six.PY3:
            value = ast.NameConstant(True)
            expected = str(value.value)
            self.assertEqual(expected, new_context._get_literal_value(value))

        if six.PY3:
            value = ast.Bytes(b'spam')
            expected = value.s
            self.assertEqual(expected, new_context._get_literal_value(value))

        self.assertIsNone(new_context._get_literal_value(None))
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def _infer_func_def(node, context, solver):
    """Infer the type for a function definition"""
    if is_stub(node) or has_type_var(node, solver):
        return_annotation = node.returns
        args_annotations = []
        for arg in node.args.args:
            args_annotations.append(arg.annotation)
        defaults_count = len(node.args.defaults)
        if hasattr(node, "method_type"):  # check if the node contains the manually added method flag
            if node.method_type not in context.builtin_methods:
                context.builtin_methods[node.method_type] = {}
            context.builtin_methods[node.method_type][node.name] = AnnotatedFunction(args_annotations,
                                                                                     return_annotation,
                                                                                     defaults_count)
        else:
            context.set_type(node.name, AnnotatedFunction(args_annotations, return_annotation, defaults_count))
        return solver.z3_types.none

    func_context, args_types = _init_func_context(node, node.args.args, context, solver)
    result_type = context.get_type(node.name)
    result_type.args_count = len(node.args.args)
    context.set_type(node.name, result_type)
    context.add_func_ast(node.name, node)

    if hasattr(node.args, "defaults"):
        # Use the default args to infer the function parameters
        _infer_args_defaults(args_types, node.args.defaults, context, solver)
        defaults_len = len(node.args.defaults)
    else:
        defaults_len = 0

    if node.returns:
        return_type = solver.resolve_annotation(node.returns)
        if inference_config["ignore_fully_annotated_function"] and is_annotated(node)\
                or isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Ellipsis):
            # Ignore the body if it has return annotation and one of the following conditions:
            # The configuration flag for doing so is set
            # The body begins with ellipsis
            body_type = return_type
        else:
            body_type = _infer_body(node.body, func_context, node.lineno, solver)
        solver.add(body_type == return_type,
                   fail_message="Return type annotation in line {}".format(node.lineno))
    else:
        body_type = _infer_body(node.body, func_context, node.lineno, solver)
        return_type = solver.new_z3_const("return")
        solver.add(solver.z3_types.subtype(body_type, return_type),
                   fail_message="Return type in line {}".format(node.lineno))
        # Putting higher weight for this soft constraint to give it higher priority over soft-constraint
        # added by inheritance covariance/contravariance return type
        solver.optimize.add_soft(body_type == return_type, weight=2)
    func_type = solver.z3_types.funcs[len(args_types)]((defaults_len,) + args_types + (return_type,))
    solver.add(result_type == func_type,
               fail_message="Function definition in line {}".format(node.lineno))
    return solver.z3_types.none