-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for #4264: --line-ranges formats entire file when ranges are at EOF #4273
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# flags: --line-ranges=6-1000 | ||
# NOTE: If you need to modify this file, pay special attention to the --line-ranges= | ||
# flag above as it's formatting specifically these lines. | ||
def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo3(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo4(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
|
||
# output | ||
# flags: --line-ranges=6-1000 | ||
# NOTE: If you need to modify this file, pay special attention to the --line-ranges= | ||
# flag above as it's formatting specifically these lines. | ||
def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo3( | ||
parameter_1, | ||
parameter_2, | ||
parameter_3, | ||
parameter_4, | ||
parameter_5, | ||
parameter_6, | ||
parameter_7, | ||
): | ||
pass | ||
|
||
|
||
def foo4( | ||
parameter_1, | ||
parameter_2, | ||
parameter_3, | ||
parameter_4, | ||
parameter_5, | ||
parameter_6, | ||
parameter_7, | ||
): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# flags: --line-ranges=5000-6000 | ||
# NOTE: If you need to modify this file, pay special attention to the --line-ranges= | ||
# flag above as it's formatting specifically these lines, in this case none. | ||
def foo1(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo2(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo3(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
def foo4(parameter_1, parameter_2, parameter_3, parameter_4, parameter_5, parameter_6, parameter_7): pass | ||
|
||
# Adding some unformated code covering a wide range of syntaxes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would simply remove the lines below, as this test case just need to verify a completely out-of-range input doesn't format |
||
|
||
if True: | ||
# Incorrectly indented prefix comments. | ||
pass | ||
|
||
import typing | ||
from typing import ( | ||
Any , | ||
) | ||
class MyClass( object): # Trailing comment with extra leading space. | ||
#NOTE: The following indentation is incorrect: | ||
@decor( 1 * 3 ) | ||
def my_func( arg): | ||
pass | ||
|
||
try: # Trailing comment with extra leading space. | ||
for i in range(10): # Trailing comment with extra leading space. | ||
while condition: | ||
if something: | ||
then_something( ) | ||
elif something_else: | ||
then_something_else( ) | ||
except ValueError as e: | ||
unformatted( ) | ||
finally: | ||
unformatted( ) | ||
|
||
async def test_async_unformatted( ): # Trailing comment with extra leading space. | ||
async for i in some_iter( unformatted ): # Trailing comment with extra leading space. | ||
await asyncio.sleep( 1 ) | ||
async with some_context( unformatted ): | ||
print( "unformatted" ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
import pytest | ||
|
||
from black.ranges import adjusted_lines | ||
from black.ranges import adjusted_lines, sanitized_lines | ||
|
||
|
||
@pytest.mark.parametrize( | ||
|
@@ -183,3 +183,60 @@ def test_diffs(lines: List[Tuple[int, int]], adjusted: List[Tuple[int, int]]) -> | |
12. # last line changed | ||
""" | ||
assert adjusted == adjusted_lines(lines, original_source, modified_source) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"lines,sanitized", | ||
[ | ||
( | ||
[(1, 4)], | ||
[(1, 4)], | ||
), | ||
( | ||
[(2, 3)], | ||
[(2, 3)], | ||
), | ||
( | ||
[(2, 10)], | ||
[(2, 4)], | ||
), | ||
( | ||
[(0, 3)], | ||
[(1, 3)], | ||
), | ||
( | ||
[(0, 10)], | ||
[(1, 4)], | ||
), | ||
( | ||
[(-2, 3)], | ||
[(1, 3)], | ||
), | ||
( | ||
[(0, 0)], | ||
[], | ||
), | ||
( | ||
[(-2, -1)], | ||
[], | ||
), | ||
( | ||
[(-1, 0)], | ||
[], | ||
), | ||
( | ||
[(3, 1), (1, 3), (5, 6)], | ||
[(1, 3)], | ||
), | ||
], | ||
) | ||
def test_sanitize( | ||
lines: List[Tuple[int, int]], sanitized: List[Tuple[int, int]] | ||
) -> None: | ||
source = """\ | ||
1. import re | ||
2. def func(arg1, | ||
3. arg2, arg3): | ||
4. pass | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a case for source not ending with a newline? |
||
assert sanitized == sanitized_lines(lines, source) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too strong an opinion, it can be more efficient to do a
count("\n")
but then you also need to add1
when it doesn't end with a new line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's really quite a difference
I resisted the temptation to write
src_contents.count("\n") + src_contents[-1] != "\n"
😄