|
| 1 | +--- |
| 2 | +source: crates/ruff_python_formatter/tests/fixtures.rs |
| 3 | +input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_skip/compound_one_liners.py |
| 4 | +--- |
| 5 | +## Input |
| 6 | +```python |
| 7 | +# Test cases for fmt: skip on compound statements that fit on one line |
| 8 | +
|
| 9 | +# Basic single-line compound statements |
| 10 | +def simple_func(): return "hello" # fmt: skip |
| 11 | +if True: print("condition met") # fmt: skip |
| 12 | +for i in range(5): print(i) # fmt: skip |
| 13 | +while x < 10: x += 1 # fmt: skip |
| 14 | +
|
| 15 | +# With expressions that would normally trigger formatting |
| 16 | +def long_params(a, b, c, d, e, f, g): return a + b + c + d + e + f + g # fmt: skip |
| 17 | +if some_very_long_condition_that_might_wrap: do_something_else_that_is_long() # fmt: skip |
| 18 | +
|
| 19 | +# Nested compound statements (outer should be preserved) |
| 20 | +if True: |
| 21 | + for i in range(10): print(i) # fmt: skip |
| 22 | +
|
| 23 | +# Multiple statements in body (should not apply - multiline) |
| 24 | +if True: |
| 25 | + x = 1 |
| 26 | + y = 2 # fmt: skip |
| 27 | +
|
| 28 | +# With decorators - decorated function on one line |
| 29 | +@overload |
| 30 | +def decorated_func(x: int) -> str: return str(x) # fmt: skip |
| 31 | +
|
| 32 | +@property |
| 33 | +def prop_method(self): return self._value # fmt: skip |
| 34 | +
|
| 35 | +# Class definitions on one line |
| 36 | +class SimpleClass: pass # fmt: skip |
| 37 | +class GenericClass(Generic[T]): pass # fmt: skip |
| 38 | +
|
| 39 | +# Try/except blocks |
| 40 | +try: risky_operation() # fmt: skip |
| 41 | +except ValueError: handle_error() # fmt: skip |
| 42 | +except: handle_any_error() # fmt: skip |
| 43 | +else: success_case() # fmt: skip |
| 44 | +finally: cleanup() # fmt: skip |
| 45 | +
|
| 46 | +# Match statements (Python 3.10+) |
| 47 | +match value: |
| 48 | + case 1: print("one") # fmt: skip |
| 49 | + case _: print("other") # fmt: skip |
| 50 | +
|
| 51 | +# With statements |
| 52 | +with open("file.txt") as f: content = f.read() # fmt: skip |
| 53 | +with context_manager() as cm: result = cm.process() # fmt: skip |
| 54 | +
|
| 55 | +# Async variants |
| 56 | +async def async_func(): return await some_call() # fmt: skip |
| 57 | +async for item in async_iterator(): await process(item) # fmt: skip |
| 58 | +async with async_context() as ctx: await ctx.work() # fmt: skip |
| 59 | +
|
| 60 | +# Complex expressions that would normally format |
| 61 | +def complex_expr(): return [x for x in range(100) if x % 2 == 0 and x > 50] # fmt: skip |
| 62 | +if condition_a and condition_b or (condition_c and not condition_d): execute_complex_logic() # fmt: skip |
| 63 | +
|
| 64 | +# Edge case: comment positioning |
| 65 | +def func_with_comment(): # some comment |
| 66 | + return "value" # fmt: skip |
| 67 | +
|
| 68 | +# Edge case: multiple fmt: skip (only last one should matter) |
| 69 | +def multiple_skip(): return "test" # fmt: skip # fmt: skip |
| 70 | +
|
| 71 | +# Should NOT be affected (already multiline) |
| 72 | +def multiline_func(): |
| 73 | + return "this should format normally" |
| 74 | +
|
| 75 | +if long_condition_that_spans \ |
| 76 | + and continues_on_next_line: |
| 77 | + print("multiline condition") |
| 78 | +
|
| 79 | +# Mix of skipped and non-skipped |
| 80 | +for i in range(10): print(f"item {i}") # fmt: skip |
| 81 | +for j in range(5): |
| 82 | + print(f"formatted item {j}") |
| 83 | +
|
| 84 | +# With trailing comma that would normally be removed |
| 85 | +def trailing_comma_func(a, b, c,): return a + b + c # fmt: skip |
| 86 | +
|
| 87 | +# Dictionary/list comprehensions |
| 88 | +def dict_comp(): return {k: v for k, v in items.items() if v is not None} # fmt: skip |
| 89 | +def list_comp(): return [x * 2 for x in numbers if x > threshold_value] # fmt: skip |
| 90 | +
|
| 91 | +# Lambda in one-liner |
| 92 | +def with_lambda(): return lambda x, y, z: x + y + z if all([x, y, z]) else None # fmt: skip |
| 93 | +
|
| 94 | +# String formatting that would normally be reformatted |
| 95 | +def format_string(): return f"Hello {name}, you have {count} items in your cart totaling ${total:.2f}" # fmt: skip |
| 96 | +
|
| 97 | +# loop else clauses |
| 98 | +for i in range(2): print(i) # fmt: skip |
| 99 | +else: print("this") # fmt: skip |
| 100 | +
|
| 101 | +
|
| 102 | +while foo(): print(i) # fmt: skip |
| 103 | +else: print("this") # fmt: skip |
| 104 | +
|
| 105 | +# again but only the first skip |
| 106 | +for i in range(2): print(i) # fmt: skip |
| 107 | +else: print("this") |
| 108 | +
|
| 109 | +
|
| 110 | +while foo(): print(i) # fmt: skip |
| 111 | +else: print("this") |
| 112 | +
|
| 113 | +# again but only the second skip |
| 114 | +for i in range(2): print(i) |
| 115 | +else: print("this") # fmt: skip |
| 116 | +
|
| 117 | +
|
| 118 | +while foo(): print(i) |
| 119 | +else: print("this") # fmt: skip |
| 120 | +
|
| 121 | +# multiple statements in body |
| 122 | +if True: print("this"); print("that") # fmt: skip |
| 123 | +``` |
| 124 | + |
| 125 | +## Outputs |
| 126 | +### Output 1 |
| 127 | +``` |
| 128 | +indent-style = space |
| 129 | +line-width = 88 |
| 130 | +indent-width = 4 |
| 131 | +quote-style = Double |
| 132 | +line-ending = LineFeed |
| 133 | +magic-trailing-comma = Respect |
| 134 | +docstring-code = Disabled |
| 135 | +docstring-code-line-width = "dynamic" |
| 136 | +preview = Enabled |
| 137 | +target_version = 3.9 |
| 138 | +source_type = Python |
| 139 | +``` |
| 140 | + |
| 141 | +```python |
| 142 | +# Test cases for fmt: skip on compound statements that fit on one line |
| 143 | +
|
| 144 | +# Basic single-line compound statements |
| 145 | +def simple_func(): return "hello" # fmt: skip |
| 146 | +
|
| 147 | +
|
| 148 | +if True: print("condition met") # fmt: skip |
| 149 | +for i in range(5): print(i) # fmt: skip |
| 150 | +while x < 10: x += 1 # fmt: skip |
| 151 | +
|
| 152 | +
|
| 153 | +# With expressions that would normally trigger formatting |
| 154 | +def long_params(a, b, c, d, e, f, g): return a + b + c + d + e + f + g # fmt: skip |
| 155 | +
|
| 156 | +
|
| 157 | +if some_very_long_condition_that_might_wrap: do_something_else_that_is_long() # fmt: skip |
| 158 | +
|
| 159 | +# Nested compound statements (outer should be preserved) |
| 160 | +if True: |
| 161 | + for i in range(10): print(i) # fmt: skip |
| 162 | +
|
| 163 | +# Multiple statements in body (should not apply - multiline) |
| 164 | +if True: |
| 165 | + x = 1 |
| 166 | + y = 2 # fmt: skip |
| 167 | +
|
| 168 | +
|
| 169 | +# With decorators - decorated function on one line |
| 170 | +@overload |
| 171 | +def decorated_func(x: int) -> str: return str(x) # fmt: skip |
| 172 | +
|
| 173 | +
|
| 174 | +@property |
| 175 | +def prop_method(self): return self._value # fmt: skip |
| 176 | +
|
| 177 | +
|
| 178 | +# Class definitions on one line |
| 179 | +class SimpleClass: pass # fmt: skip |
| 180 | +
|
| 181 | +
|
| 182 | +class GenericClass(Generic[T]): pass # fmt: skip |
| 183 | +
|
| 184 | +
|
| 185 | +# Try/except blocks |
| 186 | +try: risky_operation() # fmt: skip |
| 187 | +except ValueError: handle_error() # fmt: skip |
| 188 | +except: handle_any_error() # fmt: skip |
| 189 | +else: success_case() # fmt: skip |
| 190 | +finally: cleanup() # fmt: skip |
| 191 | +
|
| 192 | +# Match statements (Python 3.10+) |
| 193 | +match value: |
| 194 | + case 1: print("one") # fmt: skip |
| 195 | + case _: print("other") # fmt: skip |
| 196 | +
|
| 197 | +# With statements |
| 198 | +with open("file.txt") as f: content = f.read() # fmt: skip |
| 199 | +with context_manager() as cm: result = cm.process() # fmt: skip |
| 200 | +
|
| 201 | +
|
| 202 | +# Async variants |
| 203 | +async def async_func(): return await some_call() # fmt: skip |
| 204 | +
|
| 205 | +
|
| 206 | +async for item in async_iterator(): await process(item) # fmt: skip |
| 207 | +async with async_context() as ctx: await ctx.work() # fmt: skip |
| 208 | +
|
| 209 | +
|
| 210 | +# Complex expressions that would normally format |
| 211 | +def complex_expr(): return [x for x in range(100) if x % 2 == 0 and x > 50] # fmt: skip |
| 212 | +
|
| 213 | +
|
| 214 | +if condition_a and condition_b or (condition_c and not condition_d): execute_complex_logic() # fmt: skip |
| 215 | +
|
| 216 | +
|
| 217 | +# Edge case: comment positioning |
| 218 | +def func_with_comment(): # some comment |
| 219 | + return "value" # fmt: skip |
| 220 | +
|
| 221 | +
|
| 222 | +# Edge case: multiple fmt: skip (only last one should matter) |
| 223 | +def multiple_skip(): return "test" # fmt: skip # fmt: skip |
| 224 | +
|
| 225 | +
|
| 226 | +# Should NOT be affected (already multiline) |
| 227 | +def multiline_func(): |
| 228 | + return "this should format normally" |
| 229 | +
|
| 230 | +
|
| 231 | +if long_condition_that_spans and continues_on_next_line: |
| 232 | + print("multiline condition") |
| 233 | +
|
| 234 | +# Mix of skipped and non-skipped |
| 235 | +for i in range(10): print(f"item {i}") # fmt: skip |
| 236 | +for j in range(5): |
| 237 | + print(f"formatted item {j}") |
| 238 | +
|
| 239 | +
|
| 240 | +# With trailing comma that would normally be removed |
| 241 | +def trailing_comma_func(a, b, c,): return a + b + c # fmt: skip |
| 242 | +
|
| 243 | +
|
| 244 | +# Dictionary/list comprehensions |
| 245 | +def dict_comp(): return {k: v for k, v in items.items() if v is not None} # fmt: skip |
| 246 | +
|
| 247 | +
|
| 248 | +def list_comp(): return [x * 2 for x in numbers if x > threshold_value] # fmt: skip |
| 249 | +
|
| 250 | +
|
| 251 | +# Lambda in one-liner |
| 252 | +def with_lambda(): return lambda x, y, z: x + y + z if all([x, y, z]) else None # fmt: skip |
| 253 | +
|
| 254 | +
|
| 255 | +# String formatting that would normally be reformatted |
| 256 | +def format_string(): return f"Hello {name}, you have {count} items in your cart totaling ${total:.2f}" # fmt: skip |
| 257 | +
|
| 258 | +
|
| 259 | +# loop else clauses |
| 260 | +for i in range(2): print(i) # fmt: skip |
| 261 | +else: print("this") # fmt: skip |
| 262 | +
|
| 263 | +
|
| 264 | +while foo(): print(i) # fmt: skip |
| 265 | +else: print("this") # fmt: skip |
| 266 | +
|
| 267 | +# again but only the first skip |
| 268 | +for i in range(2): print(i) # fmt: skip |
| 269 | +else: |
| 270 | + print("this") |
| 271 | +
|
| 272 | +
|
| 273 | +while foo(): print(i) # fmt: skip |
| 274 | +else: |
| 275 | + print("this") |
| 276 | +
|
| 277 | +# again but only the second skip |
| 278 | +for i in range(2): |
| 279 | + print(i) |
| 280 | +else: print("this") # fmt: skip |
| 281 | +
|
| 282 | +
|
| 283 | +while foo(): |
| 284 | + print(i) |
| 285 | +else: print("this") # fmt: skip |
| 286 | +
|
| 287 | +# multiple statements in body |
| 288 | +if True: print("this"); print("that") # fmt: skip |
| 289 | +``` |
0 commit comments