fst.parsex
Extended AST parse. Allows parsing bits and pieces of valid python AST trees which are not normally parsable
individually with ast.parse().
The parse functions in this module are oriented towards parsing all valid elements which they name which may include parsing things which are not those elements, common sense is applied liberally though. And just because an element is parsed successfully doesn't mean it may not need parentheses if used in the way intended. Or that it needs a trailing newline due to comment ending without an explicit trailing newline.
Mode =
typing.Union[typing.Literal['all', 'strict', 'exec', 'eval', 'single', 'stmts', 'stmt', 'ExceptHandler', '_ExceptHandlers', 'match_case', '_match_cases', 'expr', 'expr_all', 'expr_arglike', 'expr_slice', 'Tuple_elt', 'Tuple', '_Assign_targets', '_decorator_list', 'boolop', 'operator', 'unaryop', 'cmpop', 'comprehension', '_comprehensions', '_comprehension_ifs', 'arguments', 'arguments_lambda', 'arg', 'keyword', 'alias', '_aliases', 'Import_name', '_Import_names', 'ImportFrom_name', '_ImportFrom_names', 'withitem', '_withitems', 'pattern', 'type_param', '_type_params', '_expr_arglikes'], type[ast.AST]]
Extended parse modes:
'all': Check all possible parse modes (from most likely to least). There is syntax overlap so certain types will never be returned, for exampleTypeVaris always shadowed byAnnAssign. Since this attempts many parses before failing it is slower to do so than other modes, though the most likely success is just as fast. Will never return anExpressionorInteractive.'strict': Attempt parse minumum valid parsable code. If only one statement then return the statement itself instead of theModule. If that statement is anExprthen return the expression instead of the statement. If nothing present then return emptyModule. Doesn't attempt any of the other parse modes which would not normally be parsable by python, just anything that can be parsed natively byast.parse().'exec': Parse to aModule. Same as passingModuletype or'stmts'. Same asast.parse()mode 'exec'.'eval': Parse to anExpression. Same as passingExpressiontype. Same asast.parse()mode 'eval'.'single': Parse to anInteractive. Same as passingInteractivetype. Same asast.parse()mode 'single'.'stmts': Parse zero or morestmts returned in aModule. Same as passing'exec'orModule.'stmt': Parse a singlestmtreturned as itself. Same as passingstmttype.'ExceptHandler': Parse as a singleExceptHandlerreturned as itself. Same as passingExceptHandlertype.'_ExceptHandlers': Parse zero or moreExceptHandlers returned in an_ExceptHandlersSPECIAL SLICE.'match_case': Parse a singlematch_casereturned as itself. Same as passingmatch_casetype.'_match_cases': Parse zero or morematch_cases returned in a_match_casesSPECIAL SLICE.'expr': "expression", parse a singleexprreturned as itself. This is differentiated from the following modes by the handling of slices and starred expressions. In this modea:band*not vare syntax errors. Same as passingexprtype.'expr_all': Parse to any kind of expression includingSlice,*not aorTupleof any of those combined (but unparenthesized in that case).'expr_arglike': Accept special syntax forStarred(call argument, class base definition, slice implicit tuple), same as'expr'except that in this modea:bis a syntax error and*not vparses to a starred expression*(not v).'expr_slice': "slice expression", same as'expr'except that in this modea:bparses to aSliceand*not vparses to a single element tuple containing a starred expression(*(not v),).'Tuple_elt': "tuple element expression" as in aTuplethat can be anywhere including aSubscript.slice. Same as'expr'except that in this modea:bparses to aSliceand*not vparses to a starred expression*(not v).Tuplesare parsed but cannot containSlices or arglike expressions.'Tuple': Parse to aTuplewhich may contain anything that a tuple can contain like multipleSlices and arglike expressions. If it containsSlices or arglikes then it must not be parenthesized (as per python syntax).'_Assign_targets': Parse zero or moreAssigntargets returned in a_Assign_targetsSPECIAL SLICE, with=as separators and an optional trailing=.'_decorator_list': Parse zero or more decorators returned in a_decorator_listSPECIAL SLICE. Each decorator must have a leading@.'boolop': Parse to aboolopoperator.'operator': Parse to anoperatoroperator.'unaryop': Parse to aunaryopoperator.'cmpop': Parse to acmpopcompare operator.'comprehension': Parse a singlecomprehensionreturned as itself. Same as passingcomprehensiontype.'_comprehensions': Parse zero or morecomprehensions returned in a_comprehensionsSPECIAL SLICE.'_comprehension_ifs': Parse zero or morecomprehensionifsreturned in a_comprehension_ifsSPECIAL SLICE.'arguments': Parse asargumentsfor aFunctionDeforAsyncFunctionDefreturned as itself. In this mode type annotations are allowed for the arguments. Same as passingargumentstype.'arguments_lambda': Parse asargumentsfor aLambdareturned as itself. In this mode type annotations are not allowed for the arguments.'arg': Parse as a singleargreturned as itself. Same as passingargtype.'keyword': Parse as a singlekeywordreturned as itself. Same as passingkeywordtype.'alias': Parse as a singlealiasreturned as itself. Either starred or dotted versions are accepted. Same as passingaliastype.'_aliases': Parse zero or morealiases returned in a_aliasesSPECIAL SLICE. Either starred or dotted versions are accepted. Does not need trailing comma for a single element.'Import_name': Parse as a singlealiasreturned as itself, with starred version being a syntax error. This is thealiasused inImport.names.'_Import_names': Parse zero or morealiasreturned in a_aliasesSPECIAL SLICE, with starred version being a syntax error. Does not need trailing comma for a single element. This is thealiasused inImport.names.'ImportFrom_name': Parse as a singlealiasreturned as itself, with dotted version being a syntax error. This is thealiasused inImportFrom.names.'_ImportFrom_names': Parse zero or morealiasreturned in a_aliasesSPECIAL SLICE, with dotted version being a syntax error. Does not need trailing comma for a single element. This is thealiasused inImportFrom.names.'withitem': Parse as a singlewithitemreturned as itself. Same as passingwithitemtype.'_withitems': Parse zero or morewithitems returned in a_withitemsSPECIAL SLICE.'pattern': Parse as a a singlepatternreturned as itself. Same as passingpatterntype.'type_param': Parse as a singletype_paramreturned as itself, eitherTypeVar,ParamSpecorTypeVarTuple. Same as passingtype_paramtype.'_type_params': Parse zero or moretype_params returned in a_type_paramsSPECIAL SLICE. Does not need trailing comma for a single element.type[AST]: If anASTtype is passed then will attempt to parse to this type. This can be used to narrow the scope of desired return, for exampleConstantwill parse as an expression but fail if the expression is not aConstant. These overlap with the string specifiers to an extent but not all of them. For exampleASTtypeexpris the same as passing'expr'. Not all string specified modes are can be matched, for example'arguments_lambda'. Likewise'exec'and'stmts'specify the same parse mode.Tupleparse also allows parsingSlices in theTupleas well as otherwise invalid star notation*not a.str(type[AST]): Same astype[AST].
class
ParseError(builtins.SyntaxError):
Not technically a syntax error but mostly not the code we were expecting.