Format: application/octet-stream
⬇ Download File (8.9 KB)
   1  # Rename typing to _typing, as not to conflict with typing imported
   2  # from _ast below when loaded in an unorthodox way by the Dropbox
   3  # internal Bazel integration.
   4  
   5  # The same unorthodox Bazel integration causes issues with sys, which
   6  # is imported in both modules. unfortunately we can't just rename sys,
   7  # since mypy only supports version checks with a sys that is named
   8  # sys.
   9  import sys
  10  import typing as _typing
  11  from typing import Any, Iterator, Optional, TypeVar, Union, overload
  12  from typing_extensions import Literal
  13  
  14  from _ast import *  # type: ignore
  15  
  16  if sys.version_info >= (3, 8):
  17      class Num(Constant):
  18          value: complex
  19      class Str(Constant):
  20          value: str
  21          # Aliases for value, for backwards compatibility
  22          s: str
  23      class Bytes(Constant):
  24          value: bytes
  25          # Aliases for value, for backwards compatibility
  26          s: bytes
  27      class NameConstant(Constant): ...
  28      class Ellipsis(Constant): ...
  29  
  30  if sys.version_info >= (3, 9):
  31      class slice(AST): ...
  32      class ExtSlice(slice): ...
  33      class Index(slice): ...
  34      class Suite(mod): ...
  35      class AugLoad(expr_context): ...
  36      class AugStore(expr_context): ...
  37      class Param(expr_context): ...
  38  
  39  class NodeVisitor:
  40      def visit(self, node: AST) -> Any: ...
  41      def generic_visit(self, node: AST) -> Any: ...
  42      def visit_Module(self, node: Module) -> Any: ...
  43      def visit_Interactive(self, node: Interactive) -> Any: ...
  44      def visit_Expression(self, node: Expression) -> Any: ...
  45      def visit_FunctionDef(self, node: FunctionDef) -> Any: ...
  46      def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> Any: ...
  47      def visit_ClassDef(self, node: ClassDef) -> Any: ...
  48      def visit_Return(self, node: Return) -> Any: ...
  49      def visit_Delete(self, node: Delete) -> Any: ...
  50      def visit_Assign(self, node: Assign) -> Any: ...
  51      def visit_AugAssign(self, node: AugAssign) -> Any: ...
  52      def visit_AnnAssign(self, node: AnnAssign) -> Any: ...
  53      def visit_For(self, node: For) -> Any: ...
  54      def visit_AsyncFor(self, node: AsyncFor) -> Any: ...
  55      def visit_While(self, node: While) -> Any: ...
  56      def visit_If(self, node: If) -> Any: ...
  57      def visit_With(self, node: With) -> Any: ...
  58      def visit_AsyncWith(self, node: AsyncWith) -> Any: ...
  59      def visit_Raise(self, node: Raise) -> Any: ...
  60      def visit_Try(self, node: Try) -> Any: ...
  61      def visit_Assert(self, node: Assert) -> Any: ...
  62      def visit_Import(self, node: Import) -> Any: ...
  63      def visit_ImportFrom(self, node: ImportFrom) -> Any: ...
  64      def visit_Global(self, node: Global) -> Any: ...
  65      def visit_Nonlocal(self, node: Nonlocal) -> Any: ...
  66      def visit_Expr(self, node: Expr) -> Any: ...
  67      def visit_Pass(self, node: Pass) -> Any: ...
  68      def visit_Break(self, node: Break) -> Any: ...
  69      def visit_Continue(self, node: Continue) -> Any: ...
  70      def visit_Slice(self, node: Slice) -> Any: ...
  71      def visit_BoolOp(self, node: BoolOp) -> Any: ...
  72      def visit_BinOp(self, node: BinOp) -> Any: ...
  73      def visit_UnaryOp(self, node: UnaryOp) -> Any: ...
  74      def visit_Lambda(self, node: Lambda) -> Any: ...
  75      def visit_IfExp(self, node: IfExp) -> Any: ...
  76      def visit_Dict(self, node: Dict) -> Any: ...
  77      def visit_Set(self, node: Set) -> Any: ...
  78      def visit_ListComp(self, node: ListComp) -> Any: ...
  79      def visit_SetComp(self, node: SetComp) -> Any: ...
  80      def visit_DictComp(self, node: DictComp) -> Any: ...
  81      def visit_GeneratorExp(self, node: GeneratorExp) -> Any: ...
  82      def visit_Await(self, node: Await) -> Any: ...
  83      def visit_Yield(self, node: Yield) -> Any: ...
  84      def visit_YieldFrom(self, node: YieldFrom) -> Any: ...
  85      def visit_Compare(self, node: Compare) -> Any: ...
  86      def visit_Call(self, node: Call) -> Any: ...
  87      def visit_FormattedValue(self, node: FormattedValue) -> Any: ...
  88      def visit_JoinedStr(self, node: JoinedStr) -> Any: ...
  89      def visit_Constant(self, node: Constant) -> Any: ...
  90      if sys.version_info >= (3, 8):
  91          def visit_NamedExpr(self, node: NamedExpr) -> Any: ...
  92      def visit_Attribute(self, node: Attribute) -> Any: ...
  93      def visit_Subscript(self, node: Subscript) -> Any: ...
  94      def visit_Starred(self, node: Starred) -> Any: ...
  95      def visit_Name(self, node: Name) -> Any: ...
  96      def visit_List(self, node: List) -> Any: ...
  97      def visit_Tuple(self, node: Tuple) -> Any: ...
  98      def visit_Del(self, node: Del) -> Any: ...
  99      def visit_Load(self, node: Load) -> Any: ...
 100      def visit_Store(self, node: Store) -> Any: ...
 101      def visit_And(self, node: And) -> Any: ...
 102      def visit_Or(self, node: Or) -> Any: ...
 103      def visit_Add(self, node: Add) -> Any: ...
 104      def visit_BitAnd(self, node: BitAnd) -> Any: ...
 105      def visit_BitOr(self, node: BitOr) -> Any: ...
 106      def visit_BitXor(self, node: BitXor) -> Any: ...
 107      def visit_Div(self, node: Div) -> Any: ...
 108      def visit_FloorDiv(self, node: FloorDiv) -> Any: ...
 109      def visit_LShift(self, node: LShift) -> Any: ...
 110      def visit_Mod(self, node: Mod) -> Any: ...
 111      def visit_Mult(self, node: Mult) -> Any: ...
 112      def visit_MatMult(self, node: MatMult) -> Any: ...
 113      def visit_Pow(self, node: Pow) -> Any: ...
 114      def visit_RShift(self, node: RShift) -> Any: ...
 115      def visit_Sub(self, node: Sub) -> Any: ...
 116      def visit_Invert(self, node: Invert) -> Any: ...
 117      def visit_Not(self, node: Not) -> Any: ...
 118      def visit_UAdd(self, node: UAdd) -> Any: ...
 119      def visit_USub(self, node: USub) -> Any: ...
 120      def visit_Eq(self, node: Eq) -> Any: ...
 121      def visit_Gt(self, node: Gt) -> Any: ...
 122      def visit_GtE(self, node: GtE) -> Any: ...
 123      def visit_In(self, node: In) -> Any: ...
 124      def visit_Is(self, node: Is) -> Any: ...
 125      def visit_IsNot(self, node: IsNot) -> Any: ...
 126      def visit_Lt(self, node: Lt) -> Any: ...
 127      def visit_LtE(self, node: LtE) -> Any: ...
 128      def visit_NotEq(self, node: NotEq) -> Any: ...
 129      def visit_NotIn(self, node: NotIn) -> Any: ...
 130      def visit_comprehension(self, node: comprehension) -> Any: ...
 131      def visit_ExceptHandler(self, node: ExceptHandler) -> Any: ...
 132      def visit_arguments(self, node: arguments) -> Any: ...
 133      def visit_arg(self, node: arg) -> Any: ...
 134      def visit_keyword(self, node: keyword) -> Any: ...
 135      def visit_alias(self, node: alias) -> Any: ...
 136      def visit_withitem(self, node: withitem) -> Any: ...
 137      # visit methods for deprecated nodes
 138      def visit_ExtSlice(self, node: ExtSlice) -> Any: ...
 139      def visit_Index(self, node: Index) -> Any: ...
 140      def visit_Suite(self, node: Suite) -> Any: ...
 141      def visit_AugLoad(self, node: AugLoad) -> Any: ...
 142      def visit_AugStore(self, node: AugStore) -> Any: ...
 143      def visit_Param(self, node: Param) -> Any: ...
 144      def visit_Num(self, node: Num) -> Any: ...
 145      def visit_Str(self, node: Str) -> Any: ...
 146      def visit_Bytes(self, node: Bytes) -> Any: ...
 147      def visit_NameConstant(self, node: NameConstant) -> Any: ...
 148      def visit_Ellipsis(self, node: Ellipsis) -> Any: ...
 149  
 150  class NodeTransformer(NodeVisitor):
 151      def generic_visit(self, node: AST) -> AST: ...
 152      # TODO: Override the visit_* methods with better return types.
 153      #       The usual return type is Optional[AST], but Iterable[AST]
 154      #       is also allowed in some cases -- this needs to be mapped.
 155  
 156  _T = TypeVar("_T", bound=AST)
 157  
 158  if sys.version_info >= (3, 8):
 159      @overload
 160      def parse(
 161          source: Union[str, bytes],
 162          filename: Union[str, bytes] = ...,
 163          mode: Literal["exec"] = ...,
 164          *,
 165          type_comments: bool = ...,
 166          feature_version: Union[None, int, _typing.Tuple[int, int]] = ...,
 167      ) -> Module: ...
 168      @overload
 169      def parse(
 170          source: Union[str, bytes],
 171          filename: Union[str, bytes] = ...,
 172          mode: str = ...,
 173          *,
 174          type_comments: bool = ...,
 175          feature_version: Union[None, int, _typing.Tuple[int, int]] = ...,
 176      ) -> AST: ...
 177  
 178  else:
 179      @overload
 180      def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...) -> Module: ...
 181      @overload
 182      def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> AST: ...
 183  
 184  if sys.version_info >= (3, 9):
 185      def unparse(ast_obj: AST) -> str: ...
 186  
 187  def copy_location(new_node: _T, old_node: AST) -> _T: ...
 188  
 189  if sys.version_info >= (3, 9):
 190      def dump(
 191          node: AST, annotate_fields: bool = ..., include_attributes: bool = ..., *, indent: Union[int, str, None] = ...
 192      ) -> str: ...
 193  
 194  else:
 195      def dump(node: AST, annotate_fields: bool = ..., include_attributes: bool = ...) -> str: ...
 196  
 197  def fix_missing_locations(node: _T) -> _T: ...
 198  def get_docstring(node: AST, clean: bool = ...) -> Optional[str]: ...
 199  def increment_lineno(node: _T, n: int = ...) -> _T: ...
 200  def iter_child_nodes(node: AST) -> Iterator[AST]: ...
 201  def iter_fields(node: AST) -> Iterator[_typing.Tuple[str, Any]]: ...
 202  def literal_eval(node_or_string: Union[str, AST]) -> Any: ...
 203  
 204  if sys.version_info >= (3, 8):
 205      def get_source_segment(source: str, node: AST, *, padded: bool = ...) -> Optional[str]: ...
 206  
 207  def walk(node: AST) -> Iterator[AST]: ...
 208