|
36 | 36 | Visitor,
|
37 | 37 | ensure_visible,
|
38 | 38 | is_arith_like,
|
| 39 | + is_async_stmt_or_funcdef, |
39 | 40 | is_atom_with_invisible_parens,
|
40 | 41 | is_docstring,
|
41 | 42 | is_empty_tuple,
|
@@ -110,6 +111,17 @@ def line(self, indent: int = 0) -> Iterator[Line]:
|
110 | 111 | self.current_line.depth += indent
|
111 | 112 | return # Line is empty, don't emit. Creating a new one unnecessary.
|
112 | 113 |
|
| 114 | + if ( |
| 115 | + Preview.improved_async_statements_handling in self.mode |
| 116 | + and len(self.current_line.leaves) == 1 |
| 117 | + and is_async_stmt_or_funcdef(self.current_line.leaves[0]) |
| 118 | + ): |
| 119 | + # Special case for async def/for/with statements. `visit_async_stmt` |
| 120 | + # adds an `ASYNC` leaf then visits the child def/for/with statement |
| 121 | + # nodes. Line yields from those nodes shouldn't treat the former |
| 122 | + # `ASYNC` leaf as a complete line. |
| 123 | + return |
| 124 | + |
113 | 125 | complete_line = self.current_line
|
114 | 126 | self.current_line = Line(mode=self.mode, depth=complete_line.depth + indent)
|
115 | 127 | yield complete_line
|
@@ -301,8 +313,11 @@ def visit_async_stmt(self, node: Node) -> Iterator[Line]:
|
301 | 313 | break
|
302 | 314 |
|
303 | 315 | internal_stmt = next(children)
|
304 |
| - for child in internal_stmt.children: |
305 |
| - yield from self.visit(child) |
| 316 | + if Preview.improved_async_statements_handling in self.mode: |
| 317 | + yield from self.visit(internal_stmt) |
| 318 | + else: |
| 319 | + for child in internal_stmt.children: |
| 320 | + yield from self.visit(child) |
306 | 321 |
|
307 | 322 | def visit_decorators(self, node: Node) -> Iterator[Line]:
|
308 | 323 | """Visit decorators."""
|
|
0 commit comments