[New-bugs-announce] [issue33555] No SyntaxError raised for `return` with argument inside generator

FHTMitchell report at bugs.python.org
Thu May 17 07:33:26 EDT 2018


New submission from FHTMitchell <fergus.htm at gmail.com>:

In python 2.7 if you run the following code you get an error (as you would expect)

Python 2.7.14 | packaged by conda-forge | (default, Dec 25 2017, 01:17:32) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> def f():
...     yield 1
...     return 2
...
  File "<stdin>", line 3
SyntaxError: 'return' with argument inside generator

However, in python 3.6 the error is silently ignored

Python 3.6.4 | packaged by conda-forge | (default, Dec 24 2017, 10:11:43) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     yield 1
...     return 2
...
>>> for i in f():
...     print(i)
...
1

and still is in 3.7

Python 3.7.0b2 (v3.7.0b2:b0ef5c979b, Feb 28 2018, 02:24:20) [MSC v.1912 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     yield 1
...     return 2
...
>>> for i in f():
...     print(i)
...
1

This is a source of confusion
 
https://stackoverflow.com/questions/47831240/why-is-no-value-returned-from-my-generator/
 
especially since the PEP says it is disallowed:

 https://www.python.org/dev/peps/pep-0255/#then-why-not-allow-an-expression-on-return-too

----------
components: Interpreter Core
messages: 316912
nosy: FHTMitchell
priority: normal
severity: normal
status: open
title: No SyntaxError raised for `return` with argument inside generator
type: behavior
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33555>
_______________________________________


More information about the New-bugs-announce mailing list