Skip to content

Commit 2c09e14

Browse files
committed
fix: implement type comments for for loops
1 parent 31bbb9c commit 2c09e14

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/unparser.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ impl Unparser {
211211
}
212212
self.fill("def ");
213213
self.write_str(&node.name);
214+
214215
if node.type_params.len() > 0 {
215216
self.write_str("[");
216217
let mut type_params_iter = node.type_params.iter().peekable();
@@ -407,6 +408,10 @@ impl Unparser {
407408
self.write_str(" in ");
408409
self.unparse_expr(&node.iter);
409410
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+
}
410415
self.block(|block_self| {
411416
for value in &node.body {
412417
block_self.unparse_stmt(value);
@@ -427,6 +432,10 @@ impl Unparser {
427432
self.write_str(" in ");
428433
self.unparse_expr(&node.iter);
429434
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+
}
430439
self.block(|block_self| {
431440
for value in &node.body {
432441
block_self.unparse_stmt(value);

test_files/for_loop.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
for name_3 in name_0: # type: ignore[name-defined] # noqa: F821
2+
pass
3+
4+
5+
async def main():
6+
async for name_3 in name_0: # type: ignore[name-defined] # noqa: F821
7+
pass

0 commit comments

Comments
 (0)