diff options
author | cpopa <devnull@localhost> | 2013-12-27 12:35:34 +0200 |
---|---|---|
committer | cpopa <devnull@localhost> | 2013-12-27 12:35:34 +0200 |
commit | 5a3650642608b767aadec50491360c14e0e1b412 (patch) | |
tree | dc5b4e40188275b3cb70d8a18b33b1ec6c3f74c3 | |
parent | 49b1c0ccc5e93016cc374d1c0b593d0ce25b2e8e (diff) | |
download | astroid-5a3650642608b767aadec50491360c14e0e1b412.tar.gz |
Fix `as_string` for one element tuples.
-rw-r--r-- | as_string.py | 2 | ||||
-rw-r--r-- | test/unittest_nodes.py | 9 |
2 files changed, 11 insertions, 0 deletions
diff --git a/as_string.py b/as_string.py index b7e6257..72c5b6c 100644 --- a/as_string.py +++ b/as_string.py @@ -399,6 +399,8 @@ class AsStringVisitor(object): def visit_tuple(self, node): """return an astroid.Tuple node as string""" + if len(node.elts) == 1: + return '(%s, )' % node.elts[0].accept(self) return '(%s)' % ', '.join([child.accept(self) for child in node.elts]) def visit_unaryop(self, node): diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py index 14813bb..514487c 100644 --- a/test/unittest_nodes.py +++ b/test/unittest_nodes.py @@ -35,6 +35,15 @@ abuilder = builder.AstroidBuilder() class AsString(testlib.TestCase): + def test_tuple_as_string(self): + def build(string): + return abuilder.string_build(string).body[0].value + + self.assertEqual(build('1,').as_string(), '(1, )') + self.assertEqual(build('1, 2, 3').as_string(), '(1, 2, 3)') + self.assertEqual(build('(1, )').as_string(), '(1, )') + self.assertEqual(build('1, 2, 3').as_string(), '(1, 2, 3)') + def test_varargs_kwargs_as_string(self): ast = abuilder.string_build( 'raise_string(*args, **kwargs)').body[0] self.assertEqual(ast.as_string(), 'raise_string(*args, **kwargs)') |