@@ -78,7 +78,7 @@ def char_in_string_false(index): return False
7878 ' b=True):\n '
7979 ' pass\n '
8080 )
81- pos0 , pos = 33 , 42 # Start of 'class... ', ' def' lines.
81+ pos0 , pos , pos1 = 33 , 42 , 94 # Start of 'class', 'def', 'pass ' lines.
8282
8383 # Passing no value or non-callable should fail (issue 32989).
8484 with self .assertRaises (TypeError ):
@@ -91,18 +91,18 @@ def char_in_string_false(index): return False
9191 self .assertIsNone (start (is_char_in_string = lambda index : True ))
9292
9393 # Make all text look like it's not in a string. This means that it
94- # found a good start position.
95- eq (start (char_in_string_false ), pos )
94+ # found a good start position: the last statement .
95+ eq (start (char_in_string_false ), pos1 )
9696
9797 # If the beginning of the def line is not in a string, then it
9898 # returns that as the index.
9999 eq (start (is_char_in_string = lambda index : index > pos ), pos )
100100 # If the beginning of the def line is in a string, then it
101101 # looks for a previous index.
102102 eq (start (is_char_in_string = lambda index : index >= pos ), pos0 )
103- # If everything before the 'def' is in a string, then returns None.
104- # The non-continuation def line returns 44 (see below) .
105- eq (start (is_char_in_string = lambda index : index < pos ), None )
103+ # If everything before the 'def' is in a string, then returns
104+ # the start of the 'pass' line .
105+ eq (start (is_char_in_string = lambda index : index < pos ), pos1 )
106106
107107 # Code without extra line break in def line - mostly returns the same
108108 # values.
@@ -111,12 +111,41 @@ def char_in_string_false(index): return False
111111 ' def __init__(self, a, b=True):\n '
112112 ' pass\n '
113113 ) # Does not affect class, def positions.
114- eq (start (char_in_string_false ), pos )
114+ pos1 = 77 # Start of 'pass' line.
115+ eq (start (char_in_string_false ), pos1 )
115116 eq (start (is_char_in_string = lambda index : index > pos ), pos )
116117 eq (start (is_char_in_string = lambda index : index >= pos ), pos0 )
117118 # When the def line isn't split, this returns which doesn't match the
118119 # split line test.
119- eq (start (is_char_in_string = lambda index : index < pos ), pos )
120+ eq (start (is_char_in_string = lambda index : index < pos ), pos1 )
121+
122+ # gh-85560: 'else' of a conditional expression at the start of
123+ # a continuation line does not start a statement.
124+ setcode ('def f():\n '
125+ ' return (1 if x\n '
126+ ' else 0)\n ' )
127+ eq (start (char_in_string_false ), 9 ) # Start of 'return' line.
128+ setcode ('if x:\n '
129+ ' pass\n '
130+ 'else:\n '
131+ ' x = 1\n ' )
132+ eq (start (char_in_string_false ), 15 )
133+ setcode ('if x:\n '
134+ ' pass\n '
135+ 'else : # comment\n '
136+ ' x = 1\n ' )
137+ eq (start (char_in_string_false ), 15 )
138+ # A yield expression can start a continuation line too.
139+ setcode ('def f():\n '
140+ ' x = (\n '
141+ ' yield y)\n ' )
142+ eq (start (char_in_string_false ), 0 )
143+ # Other statements which cannot start a continuation line.
144+ for stmt in ('with x:' , 'del x' , 'global x' , 'nonlocal x' , 'pass' ,
145+ 'finally:' ):
146+ with self .subTest (stmt = stmt ):
147+ setcode (f'if x:\n pass\n { stmt } \n ' )
148+ eq (start (char_in_string_false ), 15 )
120149
121150 def test_set_lo (self ):
122151 code = (
0 commit comments