def evaluate_call(node: ast3.Call, *, scope: Scope, module_path: catalog.Path) -> Node: if not is_named_tuple_definition(node, scope=scope, module_path=module_path): return node class_name_node, fields_node = node.args def field_to_parameter(field_node: ast3.expr) -> ast3.arg: name_node, annotation_node = field_node.elts return ast3.arg(ast3.literal_eval(name_node), annotation_node) initializer_node = ast3.FunctionDef( '__init__', ast3.arguments([ast3.arg('self', None)] + list(map(field_to_parameter, fields_node.elts)), None, [], [], None, []), [ast3.Pass()], [], None) function_path = evaluate_node(node.func, scope=scope, module_path=module_path) class_def = ast3.ClassDef(ast3.literal_eval(class_name_node), [ast3.Name(str(function_path), ast3.Load())], [], [initializer_node], []) return ast3.fix_missing_locations(ast3.copy_location(class_def, node))
def visit_Assign(self, node): self.generic_visit(node) targets = node.targets # ! Consider only the case when "targets" has only one non-Tuple element if len(targets) > 1 or isinstance(targets[0], Tuple): return node varname = astunparse.unparse(targets[0]).strip() pred_type, pred_prob = self.__extract_type_and_prob( varname, node.lineno, AnnotationKind.VAR) if pred_prob is None: return self.__process_old_anns(node) elif pred_prob == self.__IGNORED_PRED_PROB: return node else: # ! Assume no type comments on AnnAssign nodes return copy_location( AnnAssign( target=node.targets[0], annotation=Name(id=pred_type, ctx=Load()), value=node.value, # TODO: validate simple=0, ), node, )
def __process_old_anns(self, node): # ! For One-at-a-Time, don't remove original type annotations if self.__granularity == "var": return node if isinstance(node, arg): return copy_location( arg(arg=node.arg, annotation=None, type_comment=None), node) elif isinstance(node, FunctionDef): return copy_location( FunctionDef( name=node.name, args=node.args, body=node.body, decorator_list=node.decorator_list, returns=None, type_comment=None, ), node, ) elif isinstance(node, AnnAssign): return copy_location( AnnAssign( target=node.target, annotation=None, value=node.value, simple=node.simple, ), node, ) elif isinstance(node, Assign): return copy_location( Assign( targets=node.targets, annotation=None, value=node.value, type_comment=None, ), node, ) else: return node
def visit_arg(self, node): pred_type, pred_prob = self.__extract_type_and_prob( node.arg, node.lineno, AnnotationKind.PARA) if pred_prob is None: return self.__process_old_anns(node) elif pred_prob == self.__IGNORED_PRED_PROB: return node else: return copy_location( arg( arg=node.arg, annotation=Name(id=pred_type, ctx=Load()), type_comment=None, ), node, )
def helper(v: Any) -> Any: "Takes a `typed_ast.AST` and converts it into a python ast.AST" if isinstance(v, ast3.AST): cls = type(v) new_v = cls( **{f: copy_ast3(node) for f, node in ast3.iter_fields(v)}) if hasattr(v, 'ctx'): new_v.ctx = v.ctx # type: ignore return ast3.copy_location(new_v, v) elif isinstance(v, list): return [copy_ast3(e) for e in v] elif isinstance(v, (str, int, float)) or v is None: return v raise AstTransformerError( "typed_ast3_to_ast: The type '{}' is unknown to me".format( type(v)))
def visit_FunctionDef(self, node): self.generic_visit(node) pred_type, pred_prob = self.__extract_type_and_prob( node.name, node.lineno, AnnotationKind.FUNC) if pred_prob is None: return self.__process_old_anns(node) elif pred_prob == self.__IGNORED_PRED_PROB: return node else: return copy_location( FunctionDef( name=node.name, args=node.args, body=node.body, decorator_list=node.decorator_list, returns=Name(id=pred_type, ctx=Load()), type_comment=None, ), node, )
def visit_AnnAssign(self, node): self.generic_visit(node) target = node.target varname = (target.id if isinstance(target, Name) else astunparse.unparse(target).strip()) pred_type, pred_prob = self.__extract_type_and_prob( varname, node.lineno, AnnotationKind.VAR) if pred_prob is None: return self.__process_old_anns(node) elif pred_prob == self.__IGNORED_PRED_PROB: return node else: return copy_location( AnnAssign( target=node.target, annotation=Name(id=pred_type, ctx=Load()), value=node.value, simple=node.simple, ), node, )
def visit(self: ast3.NodeTransformer, node: ast3.AST) -> ast.AST: node = self.generic_visit(node) result = plain_cls(*map(partial(getattr, node), plain_cls._fields)) return ast3.copy_location(result, node)
def expression_to_assignment(node: ast3.expr, *, name: str) -> ast3.Assign: name_node = ast3.copy_location(ast3.Name(name, ast3.Store()), node) result = ast3.Assign([name_node], node, None) return ast3.copy_location(result, node)