|
| 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 | +# Examples with more comments |
| 125 | +
|
| 126 | +try: risky_operation() # fmt: skip |
| 127 | +# leading 1 |
| 128 | +except ValueError: handle_error() # fmt: skip |
| 129 | +# leading 2 |
| 130 | +except: handle_any_error() # fmt: skip |
| 131 | +# leading 3 |
| 132 | +else: success_case() # fmt: skip |
| 133 | +# leading 4 |
| 134 | +finally: cleanup() # fmt: skip |
| 135 | +# trailing |
| 136 | +
|
| 137 | +# multi-line before colon (should remain as is) |
| 138 | +if ( |
| 139 | + long_condition |
| 140 | +): a + b # fmt: skip |
| 141 | +``` |
| 142 | + |
| 143 | +## Output |
| 144 | +```python |
| 145 | +# Test cases for fmt: skip on compound statements that fit on one line |
| 146 | +
|
| 147 | +# Basic single-line compound statements |
| 148 | +def simple_func(): return "hello" # fmt: skip |
| 149 | +
|
| 150 | +
|
| 151 | +if True: print("condition met") # fmt: skip |
| 152 | +for i in range(5): print(i) # fmt: skip |
| 153 | +while x < 10: x += 1 # fmt: skip |
| 154 | +
|
| 155 | +
|
| 156 | +# With expressions that would normally trigger formatting |
| 157 | +def long_params(a, b, c, d, e, f, g): return a + b + c + d + e + f + g # fmt: skip |
| 158 | +
|
| 159 | +
|
| 160 | +if some_very_long_condition_that_might_wrap: do_something_else_that_is_long() # fmt: skip |
| 161 | +
|
| 162 | +# Nested compound statements (outer should be preserved) |
| 163 | +if True: |
| 164 | + for i in range(10): print(i) # fmt: skip |
| 165 | +
|
| 166 | +# Multiple statements in body (should not apply - multiline) |
| 167 | +if True: |
| 168 | + x = 1 |
| 169 | + y = 2 # fmt: skip |
| 170 | +
|
| 171 | +
|
| 172 | +# With decorators - decorated function on one line |
| 173 | +@overload |
| 174 | +def decorated_func(x: int) -> str: return str(x) # fmt: skip |
| 175 | +
|
| 176 | +
|
| 177 | +@property |
| 178 | +def prop_method(self): return self._value # fmt: skip |
| 179 | +
|
| 180 | +
|
| 181 | +# Class definitions on one line |
| 182 | +class SimpleClass: pass # fmt: skip |
| 183 | +
|
| 184 | +
|
| 185 | +class GenericClass(Generic[T]): pass # fmt: skip |
| 186 | +
|
| 187 | +
|
| 188 | +# Try/except blocks |
| 189 | +try: risky_operation() # fmt: skip |
| 190 | +except ValueError: handle_error() # fmt: skip |
| 191 | +except: handle_any_error() # fmt: skip |
| 192 | +else: success_case() # fmt: skip |
| 193 | +finally: cleanup() # fmt: skip |
| 194 | +
|
| 195 | +# Match statements (Python 3.10+) |
| 196 | +match value: |
| 197 | + case 1: print("one") # fmt: skip |
| 198 | + case _: print("other") # fmt: skip |
| 199 | +
|
| 200 | +# With statements |
| 201 | +with open("file.txt") as f: content = f.read() # fmt: skip |
| 202 | +with context_manager() as cm: result = cm.process() # fmt: skip |
| 203 | +
|
| 204 | +
|
| 205 | +# Async variants |
| 206 | +async def async_func(): return await some_call() # fmt: skip |
| 207 | +
|
| 208 | +
|
| 209 | +async for item in async_iterator(): await process(item) # fmt: skip |
| 210 | +async with async_context() as ctx: await ctx.work() # fmt: skip |
| 211 | +
|
| 212 | +
|
| 213 | +# Complex expressions that would normally format |
| 214 | +def complex_expr(): return [x for x in range(100) if x % 2 == 0 and x > 50] # fmt: skip |
| 215 | +
|
| 216 | +
|
| 217 | +if condition_a and condition_b or (condition_c and not condition_d): execute_complex_logic() # fmt: skip |
| 218 | +
|
| 219 | +
|
| 220 | +# Edge case: comment positioning |
| 221 | +def func_with_comment(): # some comment |
| 222 | + return "value" # fmt: skip |
| 223 | +
|
| 224 | +
|
| 225 | +# Edge case: multiple fmt: skip (only last one should matter) |
| 226 | +def multiple_skip(): return "test" # fmt: skip # fmt: skip |
| 227 | +
|
| 228 | +
|
| 229 | +# Should NOT be affected (already multiline) |
| 230 | +def multiline_func(): |
| 231 | + return "this should format normally" |
| 232 | +
|
| 233 | +
|
| 234 | +if long_condition_that_spans and continues_on_next_line: |
| 235 | + print("multiline condition") |
| 236 | +
|
| 237 | +# Mix of skipped and non-skipped |
| 238 | +for i in range(10): print(f"item {i}") # fmt: skip |
| 239 | +for j in range(5): |
| 240 | + print(f"formatted item {j}") |
| 241 | +
|
| 242 | +
|
| 243 | +# With trailing comma that would normally be removed |
| 244 | +def trailing_comma_func(a, b, c,): return a + b + c # fmt: skip |
| 245 | +
|
| 246 | +
|
| 247 | +# Dictionary/list comprehensions |
| 248 | +def dict_comp(): return {k: v for k, v in items.items() if v is not None} # fmt: skip |
| 249 | +
|
| 250 | +
|
| 251 | +def list_comp(): return [x * 2 for x in numbers if x > threshold_value] # fmt: skip |
| 252 | +
|
| 253 | +
|
| 254 | +# Lambda in one-liner |
| 255 | +def with_lambda(): return lambda x, y, z: x + y + z if all([x, y, z]) else None # fmt: skip |
| 256 | +
|
| 257 | +
|
| 258 | +# String formatting that would normally be reformatted |
| 259 | +def format_string(): return f"Hello {name}, you have {count} items in your cart totaling ${total:.2f}" # fmt: skip |
| 260 | +
|
| 261 | +
|
| 262 | +# loop else clauses |
| 263 | +for i in range(2): print(i) # fmt: skip |
| 264 | +else: print("this") # fmt: skip |
| 265 | +
|
| 266 | +
|
| 267 | +while foo(): print(i) # fmt: skip |
| 268 | +else: print("this") # fmt: skip |
| 269 | +
|
| 270 | +# again but only the first skip |
| 271 | +for i in range(2): print(i) # fmt: skip |
| 272 | +else: |
| 273 | + print("this") |
| 274 | +
|
| 275 | +
|
| 276 | +while foo(): print(i) # fmt: skip |
| 277 | +else: |
| 278 | + print("this") |
| 279 | +
|
| 280 | +# again but only the second skip |
| 281 | +for i in range(2): |
| 282 | + print(i) |
| 283 | +else: print("this") # fmt: skip |
| 284 | +
|
| 285 | +
|
| 286 | +while foo(): |
| 287 | + print(i) |
| 288 | +else: print("this") # fmt: skip |
| 289 | +
|
| 290 | +# multiple statements in body |
| 291 | +if True: print("this"); print("that") # fmt: skip |
| 292 | +
|
| 293 | +# Examples with more comments |
| 294 | +
|
| 295 | +try: risky_operation() # fmt: skip |
| 296 | +# leading 1 |
| 297 | +except ValueError: handle_error() # fmt: skip |
| 298 | +# leading 2 |
| 299 | +except: handle_any_error() # fmt: skip |
| 300 | +# leading 3 |
| 301 | +else: success_case() # fmt: skip |
| 302 | +# leading 4 |
| 303 | +finally: cleanup() # fmt: skip |
| 304 | +# trailing |
| 305 | +
|
| 306 | +# multi-line before colon (should remain as is) |
| 307 | +if ( |
| 308 | + long_condition |
| 309 | +): a + b # fmt: skip |
| 310 | +``` |
0 commit comments