def assert_ast_like(self, f, target): from lambdex.utils.ast import ( ast_from_source, pformat, pprint, recursively_set_attr, ) ast_f = f.__ast__ recursively_set_attr(ast_f, "type_comment", None) ast_target = ast_from_source(target, "def") ast_target.name = ast_f.name try: assert_ast_like(ast_f, ast_target) except AssertionError as cause: msg = "\n".join( [ "", "===> Compiled:", pformat(ast_f), "===> Target:", pformat(ast_target), ] ) raise AssertionError(msg) from cause
def assert_ast_like(self, f, target): from lambdex.utils.ast import ast_from_source, pformat, pprint, recursively_set_attr ast_f = f.__ast__ recursively_set_attr(ast_f, 'type_comment', None) ast_target = ast_from_source(target, 'async def') ast_target.name = ast_f.name try: assert_ast_like(ast_f, ast_target) except AssertionError as cause: msg = '\n'.join(['', '===> Compiled:', pformat(ast_f), '===> Target:', pformat(ast_target)]) raise AssertionError(msg) from cause
def assert_ast_like(self, f, target): ast_f = f.__ast__ recursively_set_attr(ast_f, "type_comment", None) if is_coroutine_ast(ast_f): keyword = "async def" else: keyword = "def" ast_target = ast_from_source(target, keyword) ast_target.name = ast_f.name try: assert_ast_like(ast_f, ast_target) except AssertionError as cause: msg = "\n".join([ "", "===> Compiled:", pformat(ast_f), "===> Target:", pformat(ast_target), ]) raise AssertionError(msg) from cause
def lambda_to_ast(lambda_object: types.FunctionType, *, keyword: str, identifier: str = ''): """ Returns the AST of `lambda_object`. """ tree = ast_from_source(lambda_object, keyword) if isinstance(tree, ast.Expr): assert not isinstance(tree.value, ast.Lambda) pattern = _make_pattern(keyword, identifier) matched = list(_shallow_match_ast(tree, pattern)) if not len(matched): raise SyntaxError('cannot parse lambda for unknown reason') if len(matched) > 1: _raise_ambiguity(matched[0], lambda_object.__code__.co_filename, keyword, identifier) assert isinstance(matched[0], ast.Call) return matched[0]