| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d97daeb commit dcfcd14
30 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -126,16 +126,33 @@ The abstract grammar is currently defined as follows: | |||
| 126 | 126 | Apart from the node classes, the :mod:`ast` module defines these utility functions | |
| 127 | 127 | and classes for traversing abstract syntax trees: | |
| 128 | 128 | ||
| 129 | - .. function:: parse(source, filename='<unknown>', mode='exec') | ||
| 129 | + .. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False) | ||
| 130 | 130 | ||
| 131 | 131 | Parse the source into an AST node. Equivalent to ``compile(source, | |
| 132 | 132 | filename, mode, ast.PyCF_ONLY_AST)``. | |
| 133 | 133 | ||
| 134 | + If ``type_comments=True`` is given, the parser is modified to check | ||
| 135 | + and return type comments as specified by :pep:`484` and :pep:`526`. | ||
| 136 | + This is equivalent to adding :data:`ast.PyCF_TYPE_COMMENTS` to the | ||
| 137 | + flags passed to :func:`compile()`. This will report syntax errors | ||
| 138 | + for misplaced type comments. Without this flag, type comments will | ||
| 139 | + be ignored, and the ``type_comment`` field on selected AST nodes | ||
| 140 | + will always be ``None``. In addition, the locations of ``# type: | ||
| 141 | + ignore`` comments will be returned as the ``type_ignores`` | ||
| 142 | + attribute of :class:`Module` (otherwise it is always an empty list). | ||
| 143 | + | ||
| 144 | + In addition, if ``mode`` is ``'func_type'``, the input syntax is | ||
| 145 | + modified to correspond to :pep:`484` "signature type comments", | ||
| 146 | + e.g. ``(str, int) -> List[str]``. | ||
| 147 | + | ||
| 134 | 148 | .. warning:: | |
| 135 | 149 | It is possible to crash the Python interpreter with a | |
| 136 | 150 | sufficiently large/complex string due to stack depth limitations | |
| 137 | 151 | in Python's AST compiler. | |
| 138 | 152 | ||
| 153 | + .. versionchanged:: 3.8 | ||
| 154 | + Added ``type_comments=True`` and ``mode='func_type'``. | ||
| 155 | + | ||
| 139 | 156 | ||
| 140 | 157 | .. function:: literal_eval(node_or_string) | |
| 141 | 158 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,6 +69,13 @@ the :mod:`tokenize` module. | |||
| 69 | 69 | always be an ``ENCODING`` token. | |
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | + .. data:: TYPE_COMMENT | ||
| 73 | + | ||
| 74 | + Token value indicating that a type comment was recognized. Such | ||
| 75 | + tokens are only produced when :func:`ast.parse()` is invoked with | ||
| 76 | + ``type_comments=True``. | ||
| 77 | + | ||
| 78 | + | ||
| 72 | 79 | .. versionchanged:: 3.5 | |
| 73 | 80 | Added :data:`AWAIT` and :data:`ASYNC` tokens. | |
| 74 | 81 | ||
@@ -78,3 +85,6 @@ the :mod:`tokenize` module. | |||
| 78 | 85 | .. versionchanged:: 3.7 | |
| 79 | 86 | Removed :data:`AWAIT` and :data:`ASYNC` tokens. "async" and "await" are | |
| 80 | 87 | now tokenized as :data:`NAME` tokens. | |
| 88 | + | ||
| 89 | + .. versionchanged:: 3.8 | ||
| 90 | + Added :data:`TYPE_COMMENT`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,9 @@ | |||
| 7 | 7 | # single_input is a single interactive statement; | |
| 8 | 8 | # file_input is a module or sequence of commands read from an input file; | |
| 9 | 9 | # eval_input is the input for the eval() functions. | |
| 10 | + # func_type_input is a PEP 484 Python 2 function type comment | ||
| 10 | 11 | # NB: compound_stmt in single_input is followed by extra NEWLINE! | |
| 12 | + # NB: due to the way TYPE_COMMENT is tokenized it will always be followed by a NEWLINE | ||
| 11 | 13 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE | |
| 12 | 14 | file_input: (NEWLINE | stmt)* ENDMARKER | |
| 13 | 15 | eval_input: testlist NEWLINE* ENDMARKER | |
@@ -17,14 +19,14 @@ decorators: decorator+ | |||
| 17 | 19 | decorated: decorators (classdef | funcdef | async_funcdef) | |
| 18 | 20 | ||
| 19 | 21 | async_funcdef: 'async' funcdef | |
| 20 | - funcdef: 'def' NAME parameters ['->' test] ':' suite | ||
| 22 | + funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] func_body_suite | ||
| 21 | 23 | ||
| 22 | 24 | parameters: '(' [typedargslist] ')' | |
| 23 | - typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ | ||
| 24 | - '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | ||
| 25 | - | '**' tfpdef [',']]] | ||
| 26 | - | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | ||
| 27 | - | '**' tfpdef [',']) | ||
| 25 | + typedargslist: (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ | ||
| 26 | + '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | ||
| 27 | + | '**' tfpdef [','] [TYPE_COMMENT]]]) | ||
| 28 | + | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | ||
| 29 | + | '**' tfpdef [','] [TYPE_COMMENT]) | ||
| 28 | 30 | tfpdef: NAME [':' test] | |
| 29 | 31 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ | |
| 30 | 32 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
@@ -39,7 +41,7 @@ simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |||
| 39 | 41 | small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | | |
| 40 | 42 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt) | |
| 41 | 43 | expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | | |
| 42 | - ('=' (yield_expr|testlist_star_expr))*) | ||
| 44 | + [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) | ||
| 43 | 45 | annassign: ':' test ['=' (yield_expr|testlist)] | |
| 44 | 46 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] | |
| 45 | 47 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | | |
@@ -71,13 +73,13 @@ compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | |||
| 71 | 73 | async_stmt: 'async' (funcdef | with_stmt | for_stmt) | |
| 72 | 74 | if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite] | |
| 73 | 75 | while_stmt: 'while' test ':' suite ['else' ':' suite] | |
| 74 | - for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] | ||
| 76 | + for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] | ||
| 75 | 77 | try_stmt: ('try' ':' suite | |
| 76 | 78 | ((except_clause ':' suite)+ | |
| 77 | 79 | ['else' ':' suite] | |
| 78 | 80 | ['finally' ':' suite] | | |
| 79 | 81 | 'finally' ':' suite)) | |
| 80 | - with_stmt: 'with' with_item (',' with_item)* ':' suite | ||
| 82 | + with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite | ||
| 81 | 83 | with_item: test ['as' expr] | |
| 82 | 84 | # NB compile.c makes sure that the default except clause is last | |
| 83 | 85 | except_clause: 'except' [test ['as' NAME]] | |
@@ -150,3 +152,14 @@ encoding_decl: NAME | |||
| 150 | 152 | ||
| 151 | 153 | yield_expr: 'yield' [yield_arg] | |
| 152 | 154 | yield_arg: 'from' test | testlist_star_expr | |
| 155 | + | ||
| 156 | + # the TYPE_COMMENT in suites is only parsed for funcdefs, | ||
| 157 | + # but can't go elsewhere due to ambiguity | ||
| 158 | + func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT | ||
| 159 | + | ||
| 160 | + func_type_input: func_type NEWLINE* ENDMARKER | ||
| 161 | + func_type: '(' [typelist] ')' '->' test | ||
| 162 | + # typelist is a modified typedargslist (see above) | ||
| 163 | + typelist: (test (',' test)* [',' | ||
| 164 | + ['*' [test] (',' test)* [',' '**' test] | '**' test]] | ||
| 165 | + | '*' [test] (',' test)* [',' '**' test] | '**' test) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,8 @@ ELLIPSIS '...' | |||
| 55 | 55 | COLONEQUAL ':=' | |
| 56 | 56 | ||
| 57 | 57 | OP | |
| 58 | + TYPE_IGNORE | ||
| 59 | + TYPE_COMMENT | ||
| 58 | 60 | ERRORTOKEN | |
| 59 | 61 | ||
| 60 | 62 | # These aren't used by the C tokenizer but are needed for tokenize.py | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ PyAPI_FUNC(PyCodeObject *) PyNode_Compile(struct _node *, const char *); | |||
| 22 | 22 | #define PyCF_DONT_IMPLY_DEDENT 0x0200 | |
| 23 | 23 | #define PyCF_ONLY_AST 0x0400 | |
| 24 | 24 | #define PyCF_IGNORE_COOKIE 0x0800 | |
| 25 | + #define PyCF_TYPE_COMMENTS 0x1000 | ||
| 25 | 26 | ||
| 26 | 27 | #ifndef Py_LIMITED_API | |
| 27 | 28 | typedef struct { | |
@@ -85,10 +86,10 @@ PyAPI_FUNC(int) _PyAST_Optimize(struct _mod *, PyArena *arena, int optimize); | |||
| 85 | 86 | ||
| 86 | 87 | #endif /* !Py_LIMITED_API */ | |
| 87 | 88 | ||
| 88 | - /* These definitions must match corresponding definitions in graminit.h. | ||
| 89 | - There's code in compile.c that checks that they are the same. */ | ||
| 89 | + /* These definitions must match corresponding definitions in graminit.h. */ | ||
| 90 | 90 | #define Py_single_input 256 | |
| 91 | 91 | #define Py_file_input 257 | |
| 92 | 92 | #define Py_eval_input 258 | |
| 93 | + #define Py_func_type_input 345 | ||
| 93 | 94 | ||
| 94 | 95 | #endif /* !Py_COMPILE_H */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments