def transform(self, node, results): syms = self.syms imports = results.get("imports") f = results.get("f") names = results.get("names") if imports: if imports.type == syms.import_as_name or not imports.children: children = [imports] else: children = imports.children for child in children[::2]: if isinstance(child, Node): for kid in child.children: if kid.value == "filterfalse": kid.changed() kid.value = "ifilterfalse" break elif child.value == "filterfalse": child.changed() child.value = "ifilterfalse" break elif names: for name in names: if is_probably_builtin(name): name.value = "i" + name.value touch_import("itertools", name.value, node) elif f: f.changed() f.value = "ifilterfalse"
def transform(self, node, results): collections = results["collections"] name = results["name"][0] collections.replace(Name(name.value, prefix=collections.prefix)) if node.type == syms.power: touch_import(None, name.value, node)
def transform(self, node, results): if results: # I feel like we should be able to express this logic in the # PATTERN above but I don't know how to do it so... obj = results["obj"] if obj: if obj.type == self.syms.star_expr: return # Make no change. if obj.type == self.syms.argument and obj.children[0].value == "**": return # Make no change. names = ("sys", "intern") new = ImportAndCall(node, results, names) touch_import(None, "sys", node) return new
def test_name_import(self): node = parse("bar()") fixer_util.touch_import(None, "cgi", node) self.assertEqual(str(node), "import cgi\nbar()\n\n")
def test_from_import(self): node = parse("bar()") fixer_util.touch_import("html", "escape", node) self.assertEqual(str(node), "from html import escape\nbar()\n\n")
def test_beginning(self): node = parse("bar()") fixer_util.touch_import(None, "foo", node) self.assertEqual(str(node), "import foo\nbar()\n\n")
def test_after_imports(self): node = parse('"""foo"""\nimport bar\nbar()') fixer_util.touch_import(None, "foo", node) self.assertEqual(str(node), '"""foo"""\nimport bar\nimport foo\nbar()\n\n')
def _handle_type2abc(self, node, results, module, abc): touch_import(None, module, node) obj = results["obj"] args = [obj.clone(), String(", " + ".".join([module, abc]))] return Call(Name("isinstance"), args, prefix=node.prefix)
def transform(self, node, results): mod = results["mod"][0] mod.replace(Name("collections", prefix=mod.prefix)) if node.type == syms.power: touch_import(None, "collections", node)
def transform(self, node, results): touch_import("functools", "reduce", node)