Skip to content
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- Remove support for pre-python 3.7 `await/async` as soft keywords/variable names
(#4676)
- Fix crash on parenthesized expression inside a type parameter bound (#4684)
- Fix crash when using line ranges excluding indented single line decorated items
(#4670)

### Preview style

Expand Down
13 changes: 13 additions & 0 deletions src/black/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ def _convert_node_to_standalone_comment(node: LN) -> None:
first.prefix = ""
index = node.remove()
if index is not None:
# Because of the special handling of multiple decorators, if the decorated
# item is a single line then there will be a missing newline between the
# decorator and item, so add it back. This doesn't affect any other case
# since a decorated item with a newline would hit the earlier suite case
# in _convert_unchanged_line_by_line that correctly handles the newlines.
if node.type == syms.decorated:
# A leaf of type decorated wouldn't make sense, since it should always
# have at least the decorator + the decorated item, so if this assert
# hits that means there's a problem in the parser.
assert isinstance(node, Node)
# 1 will always be the correct index since before this function is
# called all the decorators are collapsed into a single leaf
node.insert_child(1, Leaf(NEWLINE, "\n"))
# Remove the '\n', as STANDALONE_COMMENT will have '\n' appended when
# generating the formatted code.
value = str(node)[:-1]
Expand Down
8 changes: 8 additions & 0 deletions tests/data/cases/line_ranges_decorator_edge_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# flags: --line-ranges=6-7
class Foo:

@overload
def foo(): ...

def fox(self):
print()
Loading