mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:45:24 +00:00

## Summary Often, when fixing, we need to dedent a block of code (e.g., if we remove an `if` and dedent its body). Today, we use LibCST to parse and adjust the indentation, which is really expensive -- but this is only really necessary if the block contains a multiline string, since naively adjusting the indentation for such a string can change the whitespace _within_ the string. This PR uses a simple dedent implementation for cases in which the block doesn't intersect with a multi-line string (or an f-string, since we don't support tracking multi-line strings for f-strings right now). We could improve this even further by using the ranges to guide the dedent function, such that we don't apply the dedent if the line starts within a multiline string. But that would also need to take f-strings into account, which is a little tricky. ## Test Plan `cargo test`
227 lines
3.6 KiB
Python
227 lines
3.6 KiB
Python
import sys
|
|
|
|
if sys.version_info < (3,0):
|
|
print("py2")
|
|
else:
|
|
print("py3")
|
|
|
|
if sys.version_info < (3,0):
|
|
if True:
|
|
print("py2!")
|
|
else:
|
|
print("???")
|
|
else:
|
|
print("py3")
|
|
|
|
if sys.version_info < (3,0): print("PY2!")
|
|
else: print("PY3!")
|
|
|
|
if True:
|
|
if sys.version_info < (3,0):
|
|
print("PY2")
|
|
else:
|
|
print("PY3")
|
|
|
|
if sys.version_info < (3,0): print(1 if True else 3)
|
|
else:
|
|
print("py3")
|
|
|
|
if sys.version_info < (3,0):
|
|
def f():
|
|
print("py2")
|
|
else:
|
|
def f():
|
|
print("py3")
|
|
print("This the next")
|
|
|
|
if sys.version_info > (3,0):
|
|
print("py3")
|
|
else:
|
|
print("py2")
|
|
|
|
|
|
x = 1
|
|
|
|
if sys.version_info > (3,0):
|
|
print("py3")
|
|
else:
|
|
print("py2")
|
|
# ohai
|
|
|
|
x = 1
|
|
|
|
if sys.version_info > (3,0): print("py3")
|
|
else: print("py2")
|
|
|
|
if sys.version_info > (3,):
|
|
print("py3")
|
|
else:
|
|
print("py2")
|
|
|
|
if True:
|
|
if sys.version_info > (3,):
|
|
print("py3")
|
|
else:
|
|
print("py2")
|
|
|
|
if sys.version_info < (3,):
|
|
print("py2")
|
|
else:
|
|
print("py3")
|
|
|
|
def f():
|
|
if sys.version_info < (3,0):
|
|
try:
|
|
yield
|
|
finally:
|
|
pass
|
|
else:
|
|
yield
|
|
|
|
|
|
class C:
|
|
def g():
|
|
pass
|
|
|
|
if sys.version_info < (3,0):
|
|
def f(py2):
|
|
pass
|
|
else:
|
|
def f(py3):
|
|
pass
|
|
|
|
def h():
|
|
pass
|
|
|
|
if True:
|
|
if sys.version_info < (3,0):
|
|
2
|
|
else:
|
|
3
|
|
|
|
# comment
|
|
|
|
if sys.version_info < (3,0):
|
|
def f():
|
|
print("py2")
|
|
def g():
|
|
print("py2")
|
|
else:
|
|
def f():
|
|
print("py3")
|
|
def g():
|
|
print("py3")
|
|
|
|
if True:
|
|
if sys.version_info > (3,):
|
|
print(3)
|
|
# comment
|
|
print(2+3)
|
|
|
|
if True:
|
|
if sys.version_info > (3,): print(3)
|
|
|
|
if True:
|
|
if sys.version_info > (3,):
|
|
print(3)
|
|
|
|
|
|
if True:
|
|
if sys.version_info <= (3, 0):
|
|
expected_error = []
|
|
else:
|
|
expected_error = [
|
|
"<stdin>:1:5: Generator expression must be parenthesized",
|
|
"max(1 for i in range(10), key=lambda x: x+1)",
|
|
" ^",
|
|
]
|
|
|
|
|
|
if sys.version_info <= (3, 0):
|
|
expected_error = []
|
|
else:
|
|
expected_error = [
|
|
"<stdin>:1:5: Generator expression must be parenthesized",
|
|
"max(1 for i in range(10), key=lambda x: x+1)",
|
|
" ^",
|
|
]
|
|
|
|
|
|
if sys.version_info > (3,0):
|
|
"""this
|
|
is valid"""
|
|
|
|
"""the indentation on
|
|
this line is significant"""
|
|
|
|
"this is" \
|
|
"allowed too"
|
|
|
|
("so is"
|
|
"this for some reason")
|
|
|
|
if sys.version_info > (3, 0): expected_error = \
|
|
[]
|
|
|
|
if sys.version_info > (3, 0): expected_error = []
|
|
|
|
if sys.version_info > (3, 0): \
|
|
expected_error = []
|
|
|
|
if True:
|
|
if sys.version_info > (3, 0): expected_error = \
|
|
[]
|
|
|
|
if True:
|
|
if sys.version_info > (3, 0): expected_error = []
|
|
|
|
if True:
|
|
if sys.version_info > (3, 0): \
|
|
expected_error = []
|
|
|
|
if sys.version_info < (3,12):
|
|
print("py3")
|
|
|
|
if sys.version_info <= (3,12):
|
|
print("py3")
|
|
|
|
if sys.version_info <= (3,12):
|
|
print("py3")
|
|
|
|
if sys.version_info == 10000000:
|
|
print("py3")
|
|
|
|
if sys.version_info < (3,10000000):
|
|
print("py3")
|
|
|
|
if sys.version_info <= (3,10000000):
|
|
print("py3")
|
|
|
|
if sys.version_info > (3,12):
|
|
print("py3")
|
|
|
|
if sys.version_info >= (3,12):
|
|
print("py3")
|
|
|
|
# Slices on `sys.version_info` should be treated equivalently.
|
|
if sys.version_info[:2] >= (3,0):
|
|
print("py3")
|
|
|
|
if sys.version_info[:3] >= (3,0):
|
|
print("py3")
|
|
|
|
if sys.version_info[:2] > (3,13):
|
|
print("py3")
|
|
|
|
if sys.version_info[:3] > (3,13):
|
|
print("py3")
|
|
|
|
if sys.version_info > (3,0):
|
|
f"this is\
|
|
allowed too"
|
|
|
|
f"""the indentation on
|
|
this line is significant"""
|
|
|
|
"this is\
|
|
allowed too"
|