Skip to content

Commit 64d9bc8

Browse files
committed
fix: improve function defs and f-strings
1 parent d1a4b79 commit 64d9bc8

5 files changed

Lines changed: 75 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ rustpython-literal = ">=0.4.0"
1616
rustpython-parser = "0.4.0"
1717
rustpython-ast = { version = "0.4.0", features = ["fold"] }
1818
rand = "0.8.5"
19+
pretty_assertions = "1.4.1"

src/unparser.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ impl Unparser {
116116
self.source += str_
117117
}
118118

119+
fn write_type_comment(&mut self, type_comment: &Option<String>) {
120+
if let Some(str_) = type_comment {
121+
self.write_str(" # type: ignore");
122+
self.write_str(str_);
123+
}
124+
}
125+
119126
fn block<F>(&mut self, f: F)
120127
where
121128
F: FnOnce(&mut Self),
@@ -233,6 +240,7 @@ impl Unparser {
233240
self.unparse_expr(&returns);
234241
}
235242
self.write_str(":");
243+
self.write_type_comment(&node.type_comment);
236244
self.block(|block_self| {
237245
for value in &node.body {
238246
block_self.unparse_stmt(&value);
@@ -268,6 +276,7 @@ impl Unparser {
268276
self.unparse_expr(&returns);
269277
}
270278
self.write_str(":");
279+
self.write_type_comment(&node.type_comment);
271280
self.block(|block_self| {
272281
for value in &node.body {
273282
block_self.unparse_stmt(&value);
@@ -359,9 +368,7 @@ impl Unparser {
359368
}
360369
self.write_str(" = ");
361370
self.unparse_expr(&node.value);
362-
if node.type_comment.is_some() {
363-
self.write_str(&node.type_comment.as_ref().unwrap());
364-
}
371+
self.write_type_comment(&node.type_comment);
365372
}
366373

367374
fn unparse_stmt_type_alias(&mut self, node: &StmtTypeAlias<TextRange>) {
@@ -408,10 +415,7 @@ impl Unparser {
408415
self.write_str(" in ");
409416
self.unparse_expr(&node.iter);
410417
self.write_str(":");
411-
if let Some(type_comment) = &node.type_comment {
412-
self.write_str(" #type: ignore");
413-
self.write_str(type_comment);
414-
}
418+
self.write_type_comment(&node.type_comment);
415419
self.block(|block_self| {
416420
for value in &node.body {
417421
block_self.unparse_stmt(value);
@@ -432,10 +436,7 @@ impl Unparser {
432436
self.write_str(" in ");
433437
self.unparse_expr(&node.iter);
434438
self.write_str(":");
435-
if let Some(type_comment) = &node.type_comment {
436-
self.write_str(" #type: ignore");
437-
self.write_str(type_comment);
438-
}
439+
self.write_type_comment(&node.type_comment);
439440
self.block(|block_self| {
440441
for value in &node.body {
441442
block_self.unparse_stmt(value);
@@ -992,7 +993,8 @@ impl Unparser {
992993
match expr {
993994
Expr::Constant(ExprConstant { value, .. }) => {
994995
if let Constant::Str(str_) = value {
995-
inner_unparser.write_str(str_);
996+
let escaped = str_.replace('{', "{{").replace('}', "}}");
997+
inner_unparser.write_str(&escaped);
996998
} else {
997999
unreachable!()
9981000
}
@@ -1202,16 +1204,13 @@ impl Unparser {
12021204

12031205
if posonly_iter.peek().is_some() {
12041206
self.write_str(", ");
1205-
} else if args_iter.peek().is_some()
1206-
|| node.vararg.is_some()
1207-
|| node.vararg.is_some()
1208-
|| kw_iter.peek().is_some()
1209-
|| node.kwarg.is_some()
1210-
{
1211-
self.write_str(", /, ");
12121207
}
12131208
}
12141209

1210+
if node.posonlyargs.len() > 0 {
1211+
self.write_str(", /,");
1212+
}
1213+
12151214
while let Some(arg) = args_iter.next() {
12161215
self.unparse_arg(arg.as_arg());
12171216
if let Some(default) = &arg.default {
@@ -1230,13 +1229,16 @@ impl Unparser {
12301229
if let Some(vararg) = &node.vararg {
12311230
self.write_str("*");
12321231
self.write_str(&vararg.arg);
1232+
12331233
if let Some(annotation) = &vararg.annotation {
12341234
self.write_str(": ");
12351235
self.unparse_expr(annotation);
12361236
}
12371237
if kw_iter.peek().is_some() || node.kwarg.is_some() {
12381238
self.write_str(", ");
12391239
}
1240+
} else if node.kwonlyargs.len() > 0 {
1241+
self.write_str("*, ");
12401242
}
12411243

12421244
while let Some(kw) = kw_iter.next() {

test_files/function_def.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,32 @@ async def name_5[*name_2]( # type: ignore
2020
name_5: str = "",
2121
) -> str:
2222
return ""
23+
24+
25+
def type_comment() -> str: # type: ignore[misc]
26+
return ""
27+
28+
29+
async def type_comment_async(): # type: ignore[misc]
30+
return ""
31+
32+
33+
def name_8(
34+
name_5: name_4, # type: ignore
35+
name_3,
36+
name_4,
37+
/,
38+
*,
39+
name_1=name_2 <= name_1, # type: ignore # noqa
40+
name_0=~name_5, # type: ignore
41+
):
42+
pass
43+
44+
45+
async def name_5[**name_5, *name_3, **name_2]( # type: ignore
46+
name_1: name_1, # type: ignore # noqa
47+
name_2,
48+
name_0: name_2, # type: ignore
49+
/, # type: ignore
50+
) -> name_3: # type: ignore
51+
name_1

test_files/simple_f_string.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
f"{world!a:}None{empty_f_string!s:}None"
44
answer = 42.000001
55
f"{answer:.03f}"
6+
f"'\"'''\"\"\"{{}}\\" # noqa
67
if __name__ == "__main__":
78
print(f"Hello {world}!")

0 commit comments

Comments
 (0)