Skip to content

Commit

Permalink
feat: support for __iter__
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Sep 24, 2023
1 parent 4af67e4 commit c668b55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions executing/_position_node_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,11 @@ def node_match(node_type: Union[Type, Tuple[Type, ...]], **kwargs: Any) -> bool:
):
return

if node_match(
(ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp, ast.For)
) and inst_match("GET_ITER"):
return

if sys.version_info >= (3, 12):
if node_match(ast.UnaryOp, op=ast.UAdd) and inst_match(
"CALL_INTRINSIC_1", argrepr="INTRINSIC_UNARY_POSITIVE"
Expand Down
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,28 @@ def test_listcomp(self):
result = [calling_expression() for e in [1]]
self.assertIsInstance(result[0], ast.ListComp)

def test_iter(self):
class iter_test:
def __init__(self, typ):
self.typ = typ

def __iter__(self):
assert isinstance(calling_expression(), self.typ)
return iter([1, 2])

iter(iter_test(ast.Call))

if sys.version_info >= (3, 11):

assert [i for i in iter_test(ast.ListComp)] == [1, 2]
assert {i for i in iter_test(ast.SetComp)} == {1, 2}
assert {i: i for i in iter_test(ast.DictComp)} == {1: 1, 2: 2}
assert list(i for i in iter_test(ast.GeneratorExp)) == [1, 2]

for i in iter_test(ast.For):
assert i in (1, 2)


def test_decorator_cache_instruction(self):
frame = inspect.currentframe()

Expand Down

0 comments on commit c668b55

Please sign in to comment.