File tree Expand file tree Collapse file tree 2 files changed +13
-4
lines changed
Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -1190,18 +1190,21 @@ def visit_Tuple(self, node):
11901190
11911191 unop = {"Invert" : "~" , "Not" : "not" , "UAdd" : "+" , "USub" : "-" }
11921192 unop_precedence = {
1193- "~" : _Precedence .FACTOR ,
11941193 "not" : _Precedence .NOT ,
1194+ "~" : _Precedence .FACTOR ,
11951195 "+" : _Precedence .FACTOR ,
1196- "-" : _Precedence .FACTOR
1196+ "-" : _Precedence .FACTOR ,
11971197 }
11981198
11991199 def visit_UnaryOp (self , node ):
12001200 operator = self .unop [node .op .__class__ .__name__ ]
12011201 operator_precedence = self .unop_precedence [operator ]
12021202 with self .require_parens (operator_precedence , node ):
12031203 self .write (operator )
1204- self .write (" " )
1204+ # factor prefixes (+, -, ~) shouldn't be seperated
1205+ # from the value they belong, (e.g: +1 instead of + 1)
1206+ if operator_precedence is not _Precedence .FACTOR :
1207+ self .write (" " )
12051208 self .set_precedence (operator_precedence , node .operand )
12061209 self .traverse (node .operand )
12071210
Original file line number Diff line number Diff line change @@ -347,7 +347,7 @@ def test_simple_expressions_parens(self):
347347 self .check_src_roundtrip ("(1 + 2) / 3" )
348348 self .check_src_roundtrip ("(1 + 2) * 3 + 4 * (5 + 2)" )
349349 self .check_src_roundtrip ("(1 + 2) * 3 + 4 * (5 + 2) ** 2" )
350- self .check_src_roundtrip ("~ x" )
350+ self .check_src_roundtrip ("~x" )
351351 self .check_src_roundtrip ("x and y" )
352352 self .check_src_roundtrip ("x and y and z" )
353353 self .check_src_roundtrip ("x and (y and x)" )
@@ -401,6 +401,12 @@ def test_docstrings_negative_cases(self):
401401 self .check_ast_roundtrip (src )
402402 self .check_src_dont_roundtrip (src )
403403
404+ def test_unary_op_factor (self ):
405+ for prefix in ("+" , "-" , "~" ):
406+ self .check_src_roundtrip (f"{ prefix } 1" )
407+ for prefix in ("not" ,):
408+ self .check_src_roundtrip (f"{ prefix } 1" )
409+
404410class DirectoryTestCase (ASTTestCase ):
405411 """Test roundtrip behaviour on all files in Lib and Lib/test."""
406412
You can’t perform that action at this time.
0 commit comments