Don't add a space between a string and its subscript
diff --git a/CHANGELOG b/CHANGELOG
index 4bb00e1..bec7de1 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -9,6 +9,7 @@
supporting Python 3 type annotations.
### Fixed
- Format subscript lists so that splits are essentially free after a comma.
+- Don't add a space between a string and its subscript.
## [0.28.0] 2019-07-11
### Added
diff --git a/yapf/yapflib/split_penalty.py b/yapf/yapflib/split_penalty.py
index 3d56f03..e15ea2e 100644
--- a/yapf/yapflib/split_penalty.py
+++ b/yapf/yapflib/split_penalty.py
@@ -144,6 +144,12 @@
def Visit_arglist(self, node): # pylint: disable=invalid-name
# arglist ::= argument (',' argument)* [',']
+ if pytree_utils.NodeName(node.children[0]) == 'STAR':
+ # Python 3 treats a star expression as a specific expression type.
+ # Process it in that method.
+ self.Visit_star_expr(node)
+ return
+
self.DefaultNodeVisit(node)
for index in py3compat.range(1, len(node.children)):
diff --git a/yapf/yapflib/unwrapped_line.py b/yapf/yapflib/unwrapped_line.py
index 10e1609..ff51db5 100644
--- a/yapf/yapflib/unwrapped_line.py
+++ b/yapf/yapflib/unwrapped_line.py
@@ -329,6 +329,9 @@
# A string followed by something other than a subscript, closing bracket,
# dot, or a binary op should have a space after it.
return True
+ if format_token.Subtype.SUBSCRIPT_BRACKET in right.subtypes:
+ # It's legal to do this in Python: 'hello'[a]
+ return False
if left.is_binary_op and lval != '**' and _IsUnaryOperator(right):
# Space between the binary operator and the unary operator.
return True
diff --git a/yapftests/reformatter_buganizer_test.py b/yapftests/reformatter_buganizer_test.py
index f0d2427..e4218b1 100644
--- a/yapftests/reformatter_buganizer_test.py
+++ b/yapftests/reformatter_buganizer_test.py
@@ -28,6 +28,20 @@
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
+ def testB73279849(self):
+ unformatted_code = """\
+class A:
+ def _(a):
+ return 'hello' [ a ]
+"""
+ expected_formatted_code = """\
+class A:
+ def _(a):
+ return 'hello'[a]
+"""
+ uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
+
def testB122455211(self):
unformatted_code = """\
_zzzzzzzzzzzzzzzzzzzz = Union[sssssssssssssssssssss.pppppppppppppppp,