Skip to content

Commit 1f779de

Browse files
Fix line ranges decorator edge case (#4670)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 203fd6b commit 1f779de

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- Remove support for pre-python 3.7 `await/async` as soft keywords/variable names
2323
(#4676)
2424
- Fix crash on parenthesized expression inside a type parameter bound (#4684)
25+
- Fix crash when using line ranges excluding indented single line decorated items
26+
(#4670)
2527

2628
### Preview style
2729

src/black/ranges.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,19 @@ def _convert_node_to_standalone_comment(node: LN) -> None:
330330
first.prefix = ""
331331
index = node.remove()
332332
if index is not None:
333+
# Because of the special handling of multiple decorators, if the decorated
334+
# item is a single line then there will be a missing newline between the
335+
# decorator and item, so add it back. This doesn't affect any other case
336+
# since a decorated item with a newline would hit the earlier suite case
337+
# in _convert_unchanged_line_by_line that correctly handles the newlines.
338+
if node.type == syms.decorated:
339+
# A leaf of type decorated wouldn't make sense, since it should always
340+
# have at least the decorator + the decorated item, so if this assert
341+
# hits that means there's a problem in the parser.
342+
assert isinstance(node, Node)
343+
# 1 will always be the correct index since before this function is
344+
# called all the decorators are collapsed into a single leaf
345+
node.insert_child(1, Leaf(NEWLINE, "\n"))
333346
# Remove the '\n', as STANDALONE_COMMENT will have '\n' appended when
334347
# generating the formatted code.
335348
value = str(node)[:-1]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# flags: --line-ranges=6-7
2+
class Foo:
3+
4+
@overload
5+
def foo(): ...
6+
7+
def fox(self):
8+
print()

0 commit comments

Comments
 (0)