--- 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] slice[::, ::] slice[ # A : # B : # C ] slice[ # A 1: # B 2: # C 3 ] slice[ # A 1 + 2 : # B 3 : # C 4 ] x[ 1: # A 2: # B 3 # C ] ``` ## Black Differences ```diff --- Black +++ Ruff @@ -4,30 +4,30 @@ 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[lambda: None : lambda: None] -slice[lambda x, y, *args, really=2, **kwargs: None :, None::] +slice[ : -1 :] +slice[lambda x: True : lambda x: True] +slice[lambda x: True :, 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 []) : x] +slice[ :: [i for i in []]] async def f(): - slice[await x : [i async for i in arange(42)] : 42] + slice[await x : [i for i in []] : 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[ : upper_fn(x) : step_fn(x)], ham[ :: step_fn(x)] ham[lower + offset : upper + offset] slice[::, ::] @@ -50,10 +50,14 @@ slice[ # A 1 - + 2 : + + 2 : # B - 3 : + 3 : # C 4 ] -x[1:2:3] # A # B # C +x[ + 1: # A + 2: # B + 3 # C +] ``` ## 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 x: True : lambda x: True] slice[lambda x: True :, None::] slice[1 or 2 : True and False] slice[not so_simple : 1 < val <= 10] slice[(i for i in []) : x] slice[ :: [i for i in []]] async def f(): slice[await x : [i for i in []] : 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] slice[::, ::] slice[ # A : # B : # C ] slice[ # A 1: # B 2: # C 3 ] slice[ # A 1 + 2 : # B 3 : # C 4 ] x[ 1: # A 2: # B 3 # C ] ``` ## 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] slice[::, ::] slice[ # A : # B : # C ] slice[ # A 1: # B 2: # C 3 ] slice[ # A 1 + 2 : # B 3 : # C 4 ] x[1:2:3] # A # B # C ```