Skip to content

Commit c2086d6

Browse files
Support Python 3.5 async/await statements for Pyflakes.
1 parent 12653ca commit c2086d6

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

pyflakes/checker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,9 @@ def ignore(self, node):
649649
pass
650650

651651
# "stmt" type nodes
652-
DELETE = PRINT = FOR = WHILE = IF = WITH = WITHITEM = RAISE = \
653-
TRYFINALLY = ASSERT = EXEC = EXPR = ASSIGN = handleChildren
652+
DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \
653+
ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = ASSERT = EXEC = \
654+
EXPR = ASSIGN = handleChildren
654655

655656
CONTINUE = BREAK = PASS = ignore
656657

@@ -752,7 +753,7 @@ def YIELD(self, node):
752753
self.scope.isGenerator = True
753754
self.handleNode(node.value, node)
754755

755-
YIELDFROM = YIELD
756+
AWAIT = YIELDFROM = YIELD
756757

757758
def FUNCTIONDEF(self, node):
758759
for deco in node.decorator_list:
@@ -762,6 +763,8 @@ def FUNCTIONDEF(self, node):
762763
if self.withDoctest:
763764
self.deferFunction(lambda: self.handleDoctests(node))
764765

766+
ASYNCFUNCTIONDEF = FUNCTIONDEF
767+
765768
def LAMBDA(self, node):
766769
args = []
767770
annotations = []

pyflakes/test/test_other.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,3 +988,55 @@ def bar():
988988
def test_returnOnly(self):
989989
"""Do not crash on lone "return"."""
990990
self.flakes('return 2')
991+
992+
993+
class TestAsyncStatements(TestCase):
994+
995+
@skipIf(version_info < (3, 5), 'new in Python 3.5')
996+
def test_asyncDef(self):
997+
self.flakes('''
998+
async def bar():
999+
return 42
1000+
''')
1001+
1002+
@skipIf(version_info < (3, 5), 'new in Python 3.5')
1003+
def test_asyncDefAwait(self):
1004+
self.flakes('''
1005+
async def read_data(db):
1006+
await db.fetch('SELECT ...')
1007+
''')
1008+
1009+
@skipIf(version_info < (3, 5), 'new in Python 3.5')
1010+
def test_asyncDefUndefined(self):
1011+
self.flakes('''
1012+
async def bar():
1013+
return foo()
1014+
''', m.UndefinedName)
1015+
1016+
@skipIf(version_info < (3, 5), 'new in Python 3.5')
1017+
def test_asyncFor(self):
1018+
self.flakes('''
1019+
async def read_data(db):
1020+
output = []
1021+
async for row in db.cursor():
1022+
output.append(row)
1023+
return output
1024+
''')
1025+
1026+
@skipIf(version_info < (3, 5), 'new in Python 3.5')
1027+
def test_asyncWith(self):
1028+
self.flakes('''
1029+
async def commit(session, data):
1030+
async with session.transaction():
1031+
await session.update(data)
1032+
''')
1033+
1034+
@skipIf(version_info < (3, 5), 'new in Python 3.5')
1035+
def test_asyncWithItem(self):
1036+
self.flakes('''
1037+
async def commit(session, data):
1038+
async with session.transaction() as trans:
1039+
await trans.begin()
1040+
...
1041+
await trans.end()
1042+
''')

0 commit comments

Comments
 (0)