Skip to content

Commit 58b0feb

Browse files
author
Guido van Rossum
committed
Check Python version before looking up typing.Awaitable.
Ensure 'async def' is a syntax error in Python 2 (at least with the fast parser).
1 parent 6d219a6 commit 58b0feb

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

mypy/checker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,12 @@ def is_generator_return_type(self, typ: Type) -> bool:
292292
True if either Generator or Awaitable is a supertype of `typ`.
293293
"""
294294
gt = self.named_generic_type('typing.Generator', [AnyType(), AnyType(), AnyType()])
295+
if is_subtype(gt, typ):
296+
return True
297+
if self.options.python_version < (3, 5):
298+
return False
295299
at = self.named_generic_type('typing.Awaitable', [AnyType()])
296-
return is_subtype(gt, typ) or is_subtype(at, typ)
300+
return is_subtype(at, typ)
297301

298302
def get_generator_yield_type(self, return_type: Type) -> Type:
299303
"""Given the declared return type of a generator (t), return the type it yields (ty)."""

test-data/unit/check-async-await.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@ main: note: In function "f":
109109
main:3: error: 'yield from' in async function
110110
main: note: In function "g":
111111
main:5: error: 'yield from' in async function
112+
113+
[case testNoAsyncDefInPY2_python2]
114+
# options: fast_parser
115+
async def f(): # E: invalid syntax
116+
pass

test-data/unit/python2eval.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,11 @@ re.subn(upat, u'', u'')[0] + u''
440440
re.subn(ure, lambda m: u'', u'')[0] + u''
441441
re.subn(upat, lambda m: u'', u'')[0] + u''
442442
[out]
443+
444+
[case testYieldRegressionTypingAwaitable_python2]
445+
# Make sure we don't reference typing.Awaitable in Python 2 mode.
446+
def g() -> int:
447+
yield
448+
[out]
449+
_program.py: note: In function "g":
450+
_program.py:2: error: The return type of a generator function should be "Generator" or one of its supertypes

0 commit comments

Comments
 (0)