Skip to content

Commit 8e43b2e

Browse files
committed
Merge branch 'for-filter' of https://github.com/BobTheBuidler/mypy into for-filter
2 parents c39bb4a + c9680dc commit 8e43b2e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

mypyc/irbuild/for_helpers.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,13 +1164,15 @@ class ForFilter(ForGenerator):
11641164
"""Generate optimized IR for a for loop over filter(f, iterable)."""
11651165

11661166
def need_cleanup(self) -> bool:
1167-
# The wrapped for loops might need cleanup. We might generate a
1168-
# redundant cleanup block, but that's okay.
1169-
return True
1167+
return self.gen.need_cleanup()
11701168

11711169
def init(self, index: Lvalue, func: Expression, iterable: Expression) -> None:
11721170
self.filter_func_def = func
1173-
if isinstance(func, NameExpr) and isinstance(func.node, Var) and func.node.fullname == "builtins.None":
1171+
if (
1172+
isinstance(func, NameExpr)
1173+
and isinstance(func.node, Var)
1174+
and func.node.fullname == "builtins.None"
1175+
):
11741176
self.filter_func_val = None
11751177
else:
11761178
self.filter_func_val = self.builder.accept(func)

mypyc/test-data/run-loops.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,36 @@ print(f([0, 1, 2, 3, 4, 5, 6]))
604604
[out]
605605
6
606606

607+
[case testRunForFilterNative]
608+
def f(x: int) -> int:
609+
return x % 2
610+
def g(a: list[int]) -> int:
611+
c = 0
612+
for x in filter(f, a):
613+
c += 1
614+
return c
615+
616+
[file driver.py]
617+
from native import g
618+
print(g([0, 1, 2, 3, 4, 5, 6]))
619+
620+
[out]
621+
3
622+
623+
[case testRunForFilterPrimitiveOp]
624+
def f(a: list[list[int]]) -> int:
625+
c = 0
626+
for x in filter(len, a):
627+
c += 1
628+
return c
629+
630+
[file driver.py]
631+
from native import f
632+
print(f([[], [0, 1], [], [], [2, 3, 4], [5, 6]]))
633+
634+
[out]
635+
3
636+
607637
[case testRunForFilterEdgeCases]
608638
def f(a: list[int]) -> int:
609639
s = 0

0 commit comments

Comments
 (0)