Skip to content

Commit 7d4f8e5

Browse files
authored
Improve FormatExprCall dummy (#5290)
This solves an instability when formatting cpython. It also introduces another one, but i think it's still a worthwhile change for now. There's no proper testing since this is just a dummy.
1 parent 2c63f8c commit 7d4f8e5

File tree

37 files changed

+403
-374
lines changed

37 files changed

+403
-374
lines changed

crates/ruff_python_formatter/src/expression/expr_call.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ use rustpython_parser::ast::ExprCall;
1010
pub struct FormatExprCall;
1111

1212
impl FormatNodeRule<ExprCall> for FormatExprCall {
13-
fn fmt_fields(&self, _item: &ExprCall, f: &mut PyFormatter) -> FormatResult<()> {
14-
write!(
15-
f,
16-
[not_yet_implemented_custom_text("NOT_IMPLEMENTED_call()")]
17-
)
13+
fn fmt_fields(&self, item: &ExprCall, f: &mut PyFormatter) -> FormatResult<()> {
14+
if item.args.is_empty() && item.keywords.is_empty() {
15+
write!(
16+
f,
17+
[not_yet_implemented_custom_text("NOT_IMPLEMENTED_call()")]
18+
)
19+
} else {
20+
write!(
21+
f,
22+
[not_yet_implemented_custom_text(
23+
"NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)"
24+
)]
25+
)
26+
}
1827
}
1928
}
2029

crates/ruff_python_formatter/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ mod tests {
247247
let input = r#"
248248
# preceding
249249
if True:
250-
print( "hi" )
250+
pass
251251
# trailing
252252
"#;
253253
let expected = r#"# preceding
254254
if True:
255-
NOT_IMPLEMENTED_call()
255+
pass
256256
# trailing
257257
"#;
258258
let actual = format_module(input)?.as_code().to_string();

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__attribute_access_on_number_literals_py.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ y = 100(no)
6262
+x = (123456789.123456789E123456789).real
6363
+x = NOT_IMPLEMENTED_call()
6464
+x = 123456789J.real
65-
+x = NOT_IMPLEMENTED_call()
65+
+x = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
6666
+x = NOT_IMPLEMENTED_call()
6767
+x = NOT_IMPLEMENTED_call()
6868
+x = (0O777).real
@@ -74,7 +74,7 @@ y = 100(no)
7474
7575
y = 100[no]
7676
-y = 100(no)
77-
+y = NOT_IMPLEMENTED_call()
77+
+y = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
7878
```
7979

8080
## Ruff Output
@@ -90,7 +90,7 @@ x = NOT_IMPLEMENTED_call()
9090
x = (123456789.123456789E123456789).real
9191
x = NOT_IMPLEMENTED_call()
9292
x = 123456789J.real
93-
x = NOT_IMPLEMENTED_call()
93+
x = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
9494
x = NOT_IMPLEMENTED_call()
9595
x = NOT_IMPLEMENTED_call()
9696
x = (0O777).real
@@ -101,7 +101,7 @@ if (10).real:
101101
...
102102
103103
y = 100[no]
104-
y = NOT_IMPLEMENTED_call()
104+
y = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
105105
```
106106

107107
## Black Output

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__beginning_backslash_py.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ print("hello, world")
2222
+++ Ruff
2323
@@ -1 +1 @@
2424
-print("hello, world")
25-
+NOT_IMPLEMENTED_call()
25+
+NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
2626
```
2727

2828
## Ruff Output
2929

3030
```py
31-
NOT_IMPLEMENTED_call()
31+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
3232
```
3333

3434
## Black Output

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__collections_py.snap

+8-8
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ if True:
178178
179179
division_result_tuple = (6 / 2,)
180180
-print("foo %r", (foo.bar,))
181-
+NOT_IMPLEMENTED_call()
181+
+NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
182182
183183
if True:
184184
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
@@ -204,9 +204,9 @@ if True:
204204
- "Delay": 5,
205205
- },
206206
- )
207-
+ NOT_IMPLEMENTED_call()
208-
+ NOT_IMPLEMENTED_call()
209-
+ NOT_IMPLEMENTED_call()
207+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
208+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
209+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
210210
```
211211

212212
## Ruff Output
@@ -259,7 +259,7 @@ for (x,) in (1,), (2,), (3,):
259259
[1, 2, 3]
260260
261261
division_result_tuple = (6 / 2,)
262-
NOT_IMPLEMENTED_call()
262+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
263263
264264
if True:
265265
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
@@ -268,9 +268,9 @@ if True:
268268
)
269269
270270
if True:
271-
NOT_IMPLEMENTED_call()
272-
NOT_IMPLEMENTED_call()
273-
NOT_IMPLEMENTED_call()
271+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
272+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
273+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
274274
```
275275

276276
## Black Output

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments2_py.snap

+14-14
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,19 @@ instruction()#comment with bad spacing
243243
Cheese,
244244
- Cheese("Wensleydale"),
245245
- SubBytes(b"spam"),
246-
+ NOT_IMPLEMENTED_call(),
247-
+ NOT_IMPLEMENTED_call(),
246+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg),
247+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg),
248248
]
249249
250250
-if "PYTHON" in os.environ:
251251
- add_compiler(compiler_from_env())
252252
+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right:
253-
+ NOT_IMPLEMENTED_call()
253+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
254254
else:
255255
# for compiler in compilers.values():
256256
# add_compiler(compiler)
257257
- add_compiler(compilers[(7.0, 32)])
258-
+ NOT_IMPLEMENTED_call()
258+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
259259
# add_compiler(compilers[(7.1, 64)])
260260
261261
@@ -307,7 +307,7 @@ instruction()#comment with bad spacing
307307
-""",
308308
- arg3=True,
309309
- )
310-
+ NOT_IMPLEMENTED_call()
310+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
311311
312312
############################################################################
313313
@@ -342,7 +342,7 @@ instruction()#comment with bad spacing
342342
- # right
343343
- if element is not None
344344
- ]
345-
+ NOT_IMPLEMENTED_call()
345+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
346346
+ lcomp = [i for i in []]
347347
+ lcomp2 = [i for i in []]
348348
+ lcomp3 = [i for i in []]
@@ -357,7 +357,7 @@ instruction()#comment with bad spacing
357357
- syms.simple_stmt,
358358
- [Node(statement, result), Leaf(token.NEWLINE, "\n")], # FIXME: \r\n?
359359
- )
360-
+ return NOT_IMPLEMENTED_call()
360+
+ return NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
361361
362362
363363
-CONFIG_FILES = (
@@ -434,16 +434,16 @@ not_shareables = [
434434
"NOT_YET_IMPLEMENTED_STRING",
435435
# user-defined types and objects
436436
Cheese,
437-
NOT_IMPLEMENTED_call(),
438-
NOT_IMPLEMENTED_call(),
437+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg),
438+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg),
439439
]
440440
441441
if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right:
442-
NOT_IMPLEMENTED_call()
442+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
443443
else:
444444
# for compiler in compilers.values():
445445
# add_compiler(compiler)
446-
NOT_IMPLEMENTED_call()
446+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
447447
# add_compiler(compilers[(7.1, 64)])
448448
449449
@@ -489,11 +489,11 @@ def inline_comments_in_brackets_ruin_everything():
489489
]
490490
491491
# no newline after
492-
NOT_IMPLEMENTED_call()
492+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
493493
494494
############################################################################
495495
496-
NOT_IMPLEMENTED_call()
496+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
497497
lcomp = [i for i in []]
498498
lcomp2 = [i for i in []]
499499
lcomp3 = [i for i in []]
@@ -505,7 +505,7 @@ def inline_comments_in_brackets_ruin_everything():
505505
# and round and round we go
506506
507507
# let's return
508-
return NOT_IMPLEMENTED_call()
508+
return NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
509509
510510
511511
CONFIG_FILES = [CONFIG_FILE] + SHARED_CONFIG_FILES + USER_CONFIG_FILES # type: Final

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments3_py.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def func():
8080
+ lcomp3 = [i for i in []]
8181
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
8282
- if isinstance(exc_value, MultiError):
83-
+ if NOT_IMPLEMENTED_call():
83+
+ if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg):
8484
embedded = []
8585
for exc in exc_value.exceptions:
8686
- if exc not in _seen:
@@ -98,7 +98,7 @@ def func():
9898
- # This should be left alone (after)
9999
- )
100100
+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right:
101-
+ NOT_IMPLEMENTED_call()
101+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
102102
103103
# everything is fine if the expression isn't nested
104104
- traceback.TracebackException.from_exception(
@@ -110,7 +110,7 @@ def func():
110110
- # shared between sub-exceptions are not omitted
111111
- _seen=set(_seen),
112112
- )
113-
+ NOT_IMPLEMENTED_call()
113+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
114114
115115
116116
# %%
@@ -127,14 +127,14 @@ def func():
127127
x = "NOT_YET_IMPLEMENTED_STRING"
128128
lcomp3 = [i for i in []]
129129
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
130-
if NOT_IMPLEMENTED_call():
130+
if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg):
131131
embedded = []
132132
for exc in exc_value.exceptions:
133133
if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right:
134-
NOT_IMPLEMENTED_call()
134+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
135135
136136
# everything is fine if the expression isn't nested
137-
NOT_IMPLEMENTED_call()
137+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
138138
139139
140140
# %%

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments4_py.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def foo3(list_a, list_b):
195195
- )
196196
- .filter(User.xyz.is_(None))
197197
- )
198-
+ return NOT_IMPLEMENTED_call()
198+
+ return NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
199199
200200
201201
def foo3(list_a, list_b):
@@ -206,7 +206,7 @@ def foo3(list_a, list_b):
206206
- db.or_(User.field_a.astext.in_(list_a), User.field_b.astext.in_(list_b))
207207
- )
208208
- .filter(User.xyz.is_(None))
209-
+ NOT_IMPLEMENTED_call()
209+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
210210
)
211211
```
212212

@@ -227,13 +227,13 @@ def foo(list_a, list_b):
227227
228228
def foo2(list_a, list_b):
229229
# Standalone comment reasonably placed.
230-
return NOT_IMPLEMENTED_call()
230+
return NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
231231
232232
233233
def foo3(list_a, list_b):
234234
return (
235235
# Standalone comment but weirdly placed.
236-
NOT_IMPLEMENTED_call()
236+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
237237
)
238238
```
239239

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments5_py.snap

+8-8
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ if __name__ == "__main__":
9999
# This one is properly standalone now.
100100
101101
-for i in range(100):
102-
+for i in NOT_IMPLEMENTED_call():
102+
+for i in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg):
103103
# first we do this
104104
- if i % 33 == 0:
105105
+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right:
106106
break
107107
108108
# then we do this
109109
- print(i)
110-
+ NOT_IMPLEMENTED_call()
110+
+ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
111111
# and finally we loop around
112112
113113
-with open(some_temp_file) as f:
@@ -132,7 +132,7 @@ if __name__ == "__main__":
132132
@deco1
133133
# leading 2
134134
-@deco2(with_args=True)
135-
+@NOT_IMPLEMENTED_call()
135+
+@NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
136136
# leading 3
137137
@deco3
138138
def decorated1():
@@ -141,7 +141,7 @@ if __name__ == "__main__":
141141
@deco1
142142
# leading 2
143143
-@deco2(with_args=True)
144-
+@NOT_IMPLEMENTED_call()
144+
+@NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
145145
# leading function comment
146146
def decorated1():
147147
...
@@ -168,13 +168,13 @@ while True:
168168
169169
# This one is properly standalone now.
170170
171-
for i in NOT_IMPLEMENTED_call():
171+
for i in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg):
172172
# first we do this
173173
if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right:
174174
break
175175
176176
# then we do this
177-
NOT_IMPLEMENTED_call()
177+
NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
178178
# and finally we loop around
179179
180180
NOT_YET_IMPLEMENTED_StmtWith
@@ -196,7 +196,7 @@ def wat():
196196
# leading 1
197197
@deco1
198198
# leading 2
199-
@NOT_IMPLEMENTED_call()
199+
@NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
200200
# leading 3
201201
@deco3
202202
def decorated1():
@@ -206,7 +206,7 @@ def decorated1():
206206
# leading 1
207207
@deco1
208208
# leading 2
209-
@NOT_IMPLEMENTED_call()
209+
@NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)
210210
# leading function comment
211211
def decorated1():
212212
...

0 commit comments

Comments
 (0)