Improve slice formatting (#5922)

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

- Remove space when start of slice is empty
- Treat unary op except `not` as simple expression

## Test Plan

Add some simple tests for unary op expressions in slice

Closes #5673
This commit is contained in:
Luc Khai Hai 2023-07-21 00:05:18 +09:00 committed by GitHub
parent d351761f5d
commit b866cbb33d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 212 deletions

View file

@ -323,32 +323,6 @@ last_call()
) # note: no trailing comma pre-3.6
call(*gidgets[:2])
call(a, *gidgets[:2])
@@ -152,13 +159,13 @@
slice[0:1]
slice[0:1:2]
slice[:]
-slice[:-1]
+slice[ : -1]
slice[1:]
-slice[::-1]
+slice[ :: -1]
slice[d :: d + 1]
slice[:c, c - 1]
numpy[:, 0:1]
-numpy[:, :-1]
+numpy[:, : -1]
numpy[0, :]
numpy[:, i]
numpy[0, :2]
@@ -172,7 +179,7 @@
numpy[1 : c + 1, c]
numpy[-(c + 1) :, d]
numpy[:, l[-2]]
-numpy[:, ::-1]
+numpy[:, :: -1]
numpy[np.newaxis, :]
(str or None) if (sys.version_info[0] > (3,)) else (str or bytes or None)
{"2.7": dead, "3.7": long_live or die_hard}
@@ -207,25 +214,15 @@
)
what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set(
@ -596,13 +570,13 @@ slice[0]
slice[0:1]
slice[0:1:2]
slice[:]
slice[ : -1]
slice[:-1]
slice[1:]
slice[ :: -1]
slice[::-1]
slice[d :: d + 1]
slice[:c, c - 1]
numpy[:, 0:1]
numpy[:, : -1]
numpy[:, :-1]
numpy[0, :]
numpy[:, i]
numpy[0, :2]
@ -616,7 +590,7 @@ numpy[:, [i]]
numpy[1 : c + 1, c]
numpy[-(c + 1) :, d]
numpy[:, l[-2]]
numpy[:, :: -1]
numpy[:, ::-1]
numpy[np.newaxis, :]
(str or None) if (sys.version_info[0] > (3,)) else (str or bytes or None)
{"2.7": dead, "3.7": long_live or die_hard}

View file

@ -211,7 +211,7 @@ d={'a':1,
# Comment 1
# Comment 2
@@ -17,30 +16,44 @@
@@ -17,26 +16,40 @@
# fmt: off
def func_no_args():
@ -264,17 +264,12 @@ d={'a':1,
+ debug: bool = False,
+ **kwargs,
+) -> str:
+ return text[number : -1]
+ return text[number:-1]
+
+
# fmt: on
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
- assert task._cancel_stack[: len(old_stack)] == old_stack
+ assert task._cancel_stack[ : len(old_stack)] == old_stack
def spaces_types(
@@ -63,15 +76,15 @@
something = {
@ -445,13 +440,13 @@ def function_signature_stress_test(
debug: bool = False,
**kwargs,
) -> str:
return text[number : -1]
return text[number:-1]
# fmt: on
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
assert task._cancel_stack[ : len(old_stack)] == old_stack
assert task._cancel_stack[: len(old_stack)] == old_stack
def spaces_types(

View file

@ -117,21 +117,6 @@ def __await__(): return (yield)
def func_no_args():
@@ -41,12 +40,12 @@
debug: bool = False,
**kwargs,
) -> str:
- return text[number:-1]
+ return text[number : -1]
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
- assert task._cancel_stack[: len(old_stack)] == old_stack
+ assert task._cancel_stack[ : len(old_stack)] == old_stack
def spaces_types(
@@ -65,18 +64,14 @@
def spaces2(result=_core.Value(None)):
@ -220,12 +205,12 @@ def function_signature_stress_test(
debug: bool = False,
**kwargs,
) -> str:
return text[number : -1]
return text[number:-1]
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
assert task._cancel_stack[ : len(old_stack)] == old_stack
assert task._cancel_stack[: len(old_stack)] == old_stack
def spaces_types(

View file

@ -1,151 +0,0 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/slices.py
---
## Input
```py
slice[a.b : c.d]
slice[d :: d + 1]
slice[d + 1 :: d]
slice[d::d]
slice[0]
slice[-1]
slice[:-1]
slice[::-1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
slice[:-1:]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
slice[:: [i for i in range(42)]]
async def f():
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -4,19 +4,19 @@
slice[d::d]
slice[0]
slice[-1]
-slice[:-1]
-slice[::-1]
+slice[ : -1]
+slice[ :: -1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
-slice[:-1:]
+slice[ : -1 :]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
-slice[:: [i for i in range(42)]]
+slice[ :: [i for i in range(42)]]
async def f():
@@ -27,5 +27,5 @@
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
-ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
+ham[ : upper_fn(x) : step_fn(x)], ham[ :: step_fn(x)]
ham[lower + offset : upper + offset]
```
## Ruff Output
```py
slice[a.b : c.d]
slice[d :: d + 1]
slice[d + 1 :: d]
slice[d::d]
slice[0]
slice[-1]
slice[ : -1]
slice[ :: -1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
slice[ : -1 :]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
slice[ :: [i for i in range(42)]]
async def f():
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
ham[ : upper_fn(x) : step_fn(x)], ham[ :: step_fn(x)]
ham[lower + offset : upper + offset]
```
## Black Output
```py
slice[a.b : c.d]
slice[d :: d + 1]
slice[d + 1 :: d]
slice[d::d]
slice[0]
slice[-1]
slice[:-1]
slice[::-1]
slice[:c, c - 1]
slice[c, c + 1, d::]
slice[ham[c::d] :: 1]
slice[ham[cheese**2 : -1] : 1 : 1, ham[1:2]]
slice[:-1:]
slice[lambda: None : lambda: None]
slice[lambda x, y, *args, really=2, **kwargs: None :, None::]
slice[1 or 2 : True and False]
slice[not so_simple : 1 < val <= 10]
slice[(1 for i in range(42)) : x]
slice[:: [i for i in range(42)]]
async def f():
slice[await x : [i async for i in arange(42)] : 42]
# These are from PEP-8:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
# ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
```

View file

@ -76,12 +76,15 @@ def a():
e00 = "e"[:]
e01 = "e"[:1]
e02 = "e"[: a()]
e03 = "e"[:-1]
e10 = "e"[1:]
e11 = "e"[1:1]
e12 = "e"[1 : a()]
e13 = "e"[1:-1]
e20 = "e"[a() :]
e21 = "e"[a() : 1]
e22 = "e"[a() : a()]
e23 = "e"[a() : -1]
e200 = "e"[a() :: ]
e201 = "e"[a() :: 1]
e202 = "e"[a() :: a()]
@ -167,13 +170,16 @@ def a():
e00 = "e"[:]
e01 = "e"[:1]
e02 = "e"[ : a()]
e02 = "e"[: a()]
e03 = "e"[:-1]
e10 = "e"[1:]
e11 = "e"[1:1]
e12 = "e"[1 : a()]
e13 = "e"[1:-1]
e20 = "e"[a() :]
e21 = "e"[a() : 1]
e22 = "e"[a() : a()]
e23 = "e"[a() : -1]
e200 = "e"[a() : :]
e201 = "e"[a() :: 1]
e202 = "e"[a() :: a()]