Skip to content

Commit a0ecbc5

Browse files
authored
Merge pull request github#1998 from taus-semmle/python-support-aiter
Python: Add `__aiter__` as a recognised iterator method.
2 parents 825a3d2 + e1012d8 commit a0ecbc5

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ class ClassValue extends Value {
350350
predicate isIterable() {
351351
this.hasAttribute("__iter__")
352352
or
353+
this.hasAttribute("__aiter__")
354+
or
353355
this.hasAttribute("__getitem__")
354356
}
355357

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
| async_iterator.py:26:11:26:34 | For | $@ of class '$@' may be used in for-loop. | async_iterator.py:26:20:26:33 | ControlFlowNode for MissingAiter() | Non-iterator | async_iterator.py:13:1:13:19 | class MissingAiter | MissingAiter |
12
| statements_test.py:34:5:34:19 | For | $@ of class '$@' may be used in for-loop. | statements_test.py:34:18:34:18 | ControlFlowNode for IntegerLiteral | Non-iterator | file://:0:0:0:0 | builtin-class int | int |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
class AsyncIterator:
3+
def __init__(self):
4+
pass
5+
6+
def __aiter__(self):
7+
return self
8+
9+
async def __anext__(self):
10+
await asyncio.sleep(5)
11+
return 1
12+
13+
class MissingAiter:
14+
def __init__(self, delay, to):
15+
pass
16+
17+
async def __anext__(self):
18+
await asyncio.sleep(5)
19+
return 1
20+
21+
async def good():
22+
async for x in AsyncIterator():
23+
yield x
24+
25+
async def bad():
26+
async for x in MissingAiter():
27+
yield x

0 commit comments

Comments
 (0)