package pyre-ast

  1. Overview
  2. Docs

This module provides a type that represents a Python statement.

type ('alias, 'arguments, 'bin_op, 'except_handler, 'expr, 'identifier, 'keyword, 'location, 'match_case, 'with_item, 'stmt) t = private {
  1. function_def : location:'location -> name:'identifier -> args:'arguments -> body:'stmt list -> decorator_list:'expr list -> returns:'expr option -> type_comment:string option -> 'stmt;
    (*

    Represent a function definition (i.e. def) statement.

    • name is the name of the function.
    • args is the argument list of the function (see Arguments).
    • body is the body of the function.
    • decorator_list is the list of decorators applied to the function.
    • returns is the (optionally specified) return annotation.
    • type_comment is the Py2-style type comment for the function (see Parser.TaglessFinal.parse_function_type).
    *)
  2. async_function_def : location:'location -> name:'identifier -> args:'arguments -> body:'stmt list -> decorator_list:'expr list -> returns:'expr option -> type_comment:string option -> 'stmt;
    (*

    Represent an async function definition (i.e. async def) statement. It has exactly the same set of fields as def statement.

    *)
  3. class_def : location:'location -> name:'identifier -> bases:'expr list -> keywords:'keyword list -> body:'stmt list -> decorator_list:'expr list -> 'stmt;
    (*

    Represent a class definition (i.e. class) statement.

    • name is the name of the class.
    • bases is the positional arguments of the class' argument list (e.g. the Bar part in class Foo(Bar): ...).
    • keywords is the keyword arguments of the class' argument list (e.g. the metaclass=FooMeta part in class Foo(metaclass=FooMeta): ...). This is mainly used for metaclasses (see PEP 3115) but can also be used for other customizations of class creation (see PEP 487).
    • body is the body of the class.
    • decorator_list is the list of decorators applied to the function.
    *)
  4. return : location:'location -> value:'expr option -> 'stmt;
    (*

    Represent a return statement.

    • value is the (optionally specified) return value.
    *)
  5. delete : location:'location -> targets:'expr list -> 'stmt;
    (*

    Represent a delete statement.

    • targets is the values to delete (guaranteed by CPython parser to be non-empty). Deletion can only be one of a selected few kinds of expressions, and the corresponding expression context will always be set to ExpressionContext.del. See documentation of ExpressionContext for more details of which expressions are allowed.
    *)
  6. assign : location:'location -> targets:'expr list -> value:'expr -> type_comment:string option -> 'stmt;
    (*

    Represent an unannotated assignment statement.

    • targets is the variables/attributes/subscripts to assign to (guaranteed by CPython parser to be non-empty). Assignment target can only be one of a selected few kinds of expressions, and the corresponding expression context will always be set to ExpressionContext.t.store. See documentation of ExpressionContext for more details of which expressions are allowed.
    • value is the value to be assigned to targets, i.e. the "right-hand-side" of the assignment.
    • type_comment is the Py2-style type comment annotation for targets. Note that if targets contains more than one element, then type comment is the only possible way to specify the annotation for targets as the ann_assign statement only supports inline annotation for a single target.
    *)
  7. aug_assign : location:'location -> target:'expr -> op:'bin_op -> value:'expr -> 'stmt;
    (*

    Represent an augmented assignment statement (see PEP 203).

    • target is the variable/attribute/subscript to assign to. Assignment target can only be one of a selected few kinds of expressions, and the corresponding expression context will always be set to ExpressionContext.t.store. See documentation of ExpressionContext for more details of which expressions are allowed.
    • value is the value that will be used to compute what needs to be assigned to target. For an augmented assignment x op= y, then "right-hand-side" of the assignment is computed as x op y.
    *)
  8. ann_assign : location:'location -> target:'expr -> annotation:'expr -> value:'expr option -> simple:bool -> 'stmt;
    (*

    Represent an annotated assignment statement (see PEP 526).

    • target is the variable/attribute/subscript to assign to. Assignment target can only be one of a selected few kinds of expressions, and the corresponding expression context will always be set to ExpressionContext.t.store. See documentation of ExpressionContext for more details of which expressions are allowed.
    • value is the value to be assigned to target, i.e. the "right-hand-side" of the assignment. It can be unset (e.g. x: int), in which case the statement will only serve annotation purpose.
    • simple is set to true when target is a name expression that do not appear in between parenthesis, and false otherwise.
    *)
  9. for_ : location:'location -> target:'expr -> iter:'expr -> body:'stmt list -> orelse:'stmt list -> type_comment:string option -> 'stmt;
    (*

    Represent a for statement.

    • target is the "index variable" of the statement. It can only be one of a selected few kinds of expressions, and the corresponding expression context will always be set to ExpressionContext.t.store. See documentation of ExpressionContext for more details of which expressions are allowed.
    • iter is the expression that is being iterated on.
    • body is the body of the loop.
    • orelse is the list of statements that will be executed when the loop finishes.
    • type_comment is the Py2-style type comment annotation for target. Note that type comment is the only possible way to directly specify the annotation for targets. Achieving the same result with inline type annotation requires one to precede the loop with an annotation-only ann_assign statement.
    *)
  10. async_for : location:'location -> target:'expr -> iter:'expr -> body:'stmt list -> orelse:'stmt list -> type_comment:string option -> 'stmt;
    (*

    Represent an async for statement. It has exactly the same set of fields as for statement.

    *)
  11. while_ : location:'location -> test:'expr -> body:'stmt list -> orelse:'stmt list -> 'stmt;
    (*

    Represent a while statement.

    • test is the loop condition.
    • body is the body of the loop.
    • orelse is the list of statements that will be executed when the loop finishes.
    *)
  12. if_ : location:'location -> test:'expr -> body:'stmt list -> orelse:'stmt list -> 'stmt;
    (*

    Represent a if statement.

    • test is the branch condition.
    • body is the true-branch of the loop.
    • orelse is the false-branch of the loop.
    *)
  13. with_ : location:'location -> items:'with_item list -> body:'stmt list -> type_comment:string option -> 'stmt;
    (*

    Represent a with statement. See PEP 343.

    • items is the "context expression" which will be evaluated to obtain a context manager.
    • body is the body of the with block.
    • type_comment is the Py2-style type comment annotation for all targets in the with items. Note that type comment is the only possible way to directly specify the annotation for targets. Achieving the same result with inline type annotation requires one to precede the with block with annotation-only ann_assign statements.
    *)
  14. async_with : location:'location -> items:'with_item list -> body:'stmt list -> type_comment:string option -> 'stmt;
    (*

    Represent an async with statement. It has exactly the same set of fields as with statement.

    *)
  15. match_ : location:'location -> subject:'expr -> cases:'match_case list -> 'stmt;
    (*

    Represent a match statement. See PEP 622.

    • subject is the expression that will be matched on.
    • cases is the list of branches for this match. See MatchCase for more details.
    *)
  16. raise_ : location:'location -> exc:'expr option -> cause:'expr option -> 'stmt;
    (*

    Represent a raise statement.

    • exc is the exception to raise. It can be left unset, in which case Python runtime will re-raise the "last" exception that was just caught.
    • cause is set if one specify the "from" clause. For example, raise x from y will set cause to y, making the Python runtime attach y to the __cause__ field of x.
    *)
  17. try_ : location:'location -> body:'stmt list -> handlers:'except_handler list -> orelse:'stmt list -> finalbody:'stmt list -> 'stmt;
    (*

    Represent a try...except statement.

    • body is the body of the try block, i.e. the logic to run in the normal case.
    • handlers is a list of exception handlers, specifying the logic to run in the exceptional cases.
    • orelse is the list of statements that will be executed when the body finishes without having any exception raised and without early return/continue/break.
    • finalbody is the list of statements that is guaranteed to be executed when execution leaves the try block, regardless of whether an exception occured or not.
    *)
  18. try_star : location:'location -> body:'stmt list -> handlers:'except_handler list -> orelse:'stmt list -> finalbody:'stmt list -> 'stmt;
    (*

    Represent a try...except* statement. See PEP 654.

    Arguments here means the same thing as the try...except statement, except that the handlers part should be interpreted as catching&handling exception groups.

    *)
  19. assert_ : location:'location -> test:'expr -> msg:'expr option -> 'stmt;
    (*

    Represent an assert statement.

    • test is condition to assert on.
    • msg is an (optionally specified) argument that will be passed to the AssertionError being raised by the Python runtime, if the assertion fails.
    *)
  20. import : location:'location -> names:'alias list -> 'stmt;
    (*

    Represent an import statement.

    • name is the list of module names to import.
    *)
  21. import_from : location:'location -> module_:'identifier option -> names:'alias list -> level:int -> 'stmt;
    (*

    Represent a from ... import ... statement.

    • module_ is the name of the module from which we want to import. It can be left unset, when no module name is specified (e.g. from . import X).
    • names is the list of names to import to the current module.
    • level is an integer holding the level of the relative import (i.e. the number of dots in the import target).
    *)
  22. global : location:'location -> names:'identifier list -> 'stmt;
    (*

    Represent a global statement.

    • names is a list of names in the current block to declare as global.
    *)
  23. nonlocal : location:'location -> names:'identifier list -> 'stmt;
    (*

    Represent a nonlocal statement. See PEP 3104.

    • names is a list of names in the current block to declare as nonlocal.
    *)
  24. expr : location:'location -> value:'expr -> 'stmt;
    (*

    Represent an expression statement.

    • value holds the expression (usually with side-effects) that will be evaluated.
    *)
  25. pass : location:'location -> 'stmt;
    (*

    Represent a pass statement.

    *)
  26. break : location:'location -> 'stmt;
    (*

    Represent a break statement.

    *)
  27. continue : location:'location -> 'stmt;
    (*

    Represent a continue statement.

    *)
}
val make : function_def: (location:'a -> name:'b -> args:'c -> body:'d list -> decorator_list:'e list -> returns:'e option -> type_comment:string option -> 'd) -> async_function_def: (location:'a -> name:'b -> args:'c -> body:'d list -> decorator_list:'e list -> returns:'e option -> type_comment:string option -> 'd) -> class_def: (location:'a -> name:'b -> bases:'e list -> keywords:'f list -> body:'d list -> decorator_list:'e list -> 'd) -> return:(location:'a -> value:'e option -> 'd) -> delete:(location:'a -> targets:'e list -> 'd) -> assign: (location:'a -> targets:'e list -> value:'e -> type_comment:string option -> 'd) -> aug_assign:(location:'a -> target:'e -> op:'g -> value:'e -> 'd) -> ann_assign: (location:'a -> target:'e -> annotation:'e -> value:'e option -> simple:bool -> 'd) -> for_: (location:'a -> target:'e -> iter:'e -> body:'d list -> orelse:'d list -> type_comment:string option -> 'd) -> async_for: (location:'a -> target:'e -> iter:'e -> body:'d list -> orelse:'d list -> type_comment:string option -> 'd) -> while_:(location:'a -> test:'e -> body:'d list -> orelse:'d list -> 'd) -> if_:(location:'a -> test:'e -> body:'d list -> orelse:'d list -> 'd) -> with_: (location:'a -> items:'h list -> body:'d list -> type_comment:string option -> 'd) -> async_with: (location:'a -> items:'h list -> body:'d list -> type_comment:string option -> 'd) -> match_:(location:'a -> subject:'e -> cases:'i list -> 'd) -> raise_:(location:'a -> exc:'e option -> cause:'e option -> 'd) -> try_: (location:'a -> body:'d list -> handlers:'j list -> orelse:'d list -> finalbody:'d list -> 'd) -> try_star: (location:'a -> body:'d list -> handlers:'j list -> orelse:'d list -> finalbody:'d list -> 'd) -> assert_:(location:'a -> test:'e -> msg:'e option -> 'd) -> import:(location:'a -> names:'k list -> 'd) -> import_from: (location:'a -> module_:'b option -> names:'k list -> level:int -> 'd) -> global:(location:'a -> names:'b list -> 'd) -> nonlocal:(location:'a -> names:'b list -> 'd) -> expr:(location:'a -> value:'e -> 'd) -> pass:(location:'a -> 'd) -> break:(location:'a -> 'd) -> continue:(location:'a -> 'd) -> unit -> ('k, 'c, 'g, 'j, 'e, 'b, 'f, 'a, 'i, 'h, 'd) t

Constructor of t.