class FixImport(fixer_base.BaseFix):
PATTERN = """
- import_from< type='from' imp=any 'import' any >
+ import_from< type='from' imp=any 'import' ['('] any [')'] >
|
import_name< type='import' imp=any >
"""
yield """import_name< 'import' ((%s)
| dotted_as_names< any* (%s) any* >) >
""" % (mod_list, mod_list)
- yield """import_from< 'from' (%s) 'import'
+ yield """import_from< 'from' (%s) 'import' ['(']
( any | import_as_name< any 'as' any > |
- import_as_names< any* >) >
+ import_as_names< any* >) [')'] >
""" % mod_name_list
yield """import_name< 'import'
dotted_as_name< (%s) 'as' any > >
elif node.type == syms.simple_stmt and node.children:
expr_node = node.children[0]
if expr_node.type == syms.expr_stmt and expr_node.children:
- leaf_node = expr_node.children[0]
- if leaf_node.value == '__metaclass__':
+ left_side = expr_node.children[0]
+ if isinstance(left_side, Leaf) and \
+ left_side.value == '__metaclass__':
return True
return False
if node.children[3].type == syms.arglist:
arglist = node.children[3]
# Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite])
- elif isinstance(node.children[3], Leaf):
+ else:
parent = node.children[3].clone()
arglist = Node(syms.arglist, [parent])
node.set_child(3, arglist)
- else:
- raise ValueError("Unexpected class inheritance arglist")
elif len(node.children) == 6:
# Node(classdef, ['class', 'name', '(', ')', ':', suite])
# 0 1 2 3 4 5
self.log_error("Can't create %s: %s", filename, err)
return
try:
- try:
- f.write(new_text)
- except os.error as err:
- self.log_error("Can't write %s: %s", filename, err)
+ f.write(new_text)
+ except os.error as err:
+ self.log_error("Can't write %s: %s", filename, err)
finally:
f.close()
self.log_debug("Wrote changes to %s", filename)
a = "from %s import foo, bar" % new
self.check(b, a)
+ b = "from %s import (yes, no)" % old
+ a = "from %s import (yes, no)" % new
+ self.check(b, a)
+
def test_import_module_as(self):
for old, new in self.modules.items():
b = "import %s as foo_bar" % old
a = "from .foo import bar"
self.check_both(b, a)
+ b = "from foo import (bar, baz)"
+ a = "from .foo import (bar, baz)"
+ self.check_both(b, a)
+
def test_dotted_from(self):
b = "from green.eggs import ham"
a = "from .green.eggs import ham"
"""
self.unchanged(s)
+ s = """
+ class X:
+ a[23] = 74
+ """
+ self.unchanged(s)
+
def test_comments(self):
b = """
class X:
a = """class m(a, arg=23, metaclass=Meta): pass"""
self.check(b, a)
+ b = """
+ class X(expression(2 + 4)):
+ __metaclass__ = Meta
+ """
+ a = """
+ class X(expression(2 + 4), metaclass=Meta):
+ pass
+ """
+ self.check(b, a)
+
+ b = """
+ class X(expression(2 + 4), x**4):
+ __metaclass__ = Meta
+ """
+ a = """
+ class X(expression(2 + 4), x**4, metaclass=Meta):
+ pass
+ """
+ self.check(b, a)
+
class Test_getcwdu(FixerTestCase):