def remove_from_import(node, package, name): """ Removes the ``from X import Y`` from the module. """ root = fixer_util.find_root(node) import_node = fixer_util.find_binding(name, root, package) if not import_node: return from_imports = get_from_imports(import_node) from_imports_children = [] while from_imports: from_import = from_imports.pop(0) if from_import.value == name: from_import.remove() continue from_imports_children.append(from_import) if not from_imports_children: import_node.remove() return new_import = fixer_util.FromImport(package, from_imports_children) import_node.replace(new_import)
def _find_bind_rec(self, name, node): # Search a tree for a binding -- used to find the starting # point for these tests. c = fixer_util.find_binding(name, node) if c: return c for child in node.children: c = self._find_bind_rec(name, child) if c: return c
def _rename_test(node, filename): """ Renames one test to `test_<name>` """ old_name = node.value root = find_root(node) def_statement = find_binding(old_name, root) if old_name in ("None", ): # why are these there? return if not old_name.startswith("test_"): new_name = f"test_{old_name}" # def_statement can be None if the test doesn't exist. # Could happen if it was referenced in multiple places; # the first time we came across it we renamed it. if def_statement is not None: # Rename the function def_statement.children[1].value = new_name def_statement.children[1].changed() # Rename all references, including `node` for n in root.leaves(): if n.type == TOKEN.NAME and n.value == old_name: # Don't include dotted names if (n.parent.type == syms.trailer and n.prev_sibling and n.prev_sibling.value == '.'): continue # This is probably a reference to the test function # However, we need to check in case it's a separate local var. # Figure out if we're in a function, and see if there's a binding for # the same name func_node = get_ancestor_of_type(n, syms.funcdef) if func_node and find_binding(old_name, func_node): # This is a local var with the same name, don't rename it continue n.value = new_name n.changed()
def remove_import(node, name): """ Removes the import. """ # def is_import_stmt(node): # return ( # node.type == fixer_util.syms.simple_stmt # pylint: disable=no-member # and node.children # and fixer_util.is_import(node.children[0]) # ) root = fixer_util.find_root(node) if "." in name: raise NotImplementedError("Dotted imports not yet supported") import_node = fixer_util.find_binding(name, root) if import_node: import_node.remove()
def replace_unicode_methods(node, capture, arguments): # remove any existing __str__ method b = find_binding("__str__", capture['suite']) if b and b.type == syms.funcdef: b.remove() # rename __unicode__ to __str__ funcname = capture['funcname'].clone() funcname.value = '__str__' capture['funcname'].replace(funcname) # Add a six import touch_import(None, "six", node) # Decorate the class with `@six.python_2_unicode_compatible` classdef = node.clone() classdef.prefix = '' decorated = Node( syms.decorated, [ Node( syms.decorator, [ Leaf(TOKEN.AT, '@', prefix=node.prefix), Node( syms.dotted_name, [ Name('six'), Dot(), Name('python_2_unicode_compatible') ], ), Newline(), ], prefix=node.prefix, ), classdef, ], prefix=node.prefix, ) node.replace(decorated)
def find_binding(self, name, string, package=None): return fixer_util.find_binding(name, parse(string), package)