Author arigo
Recipients Inyeol.Lee, arigo, belopolsky, benjamin.peterson, danielsh, emptysquare, erickt, esc24, georg.brandl, glyph, gvanrossum, levkivskyi, rhettinger
Date 2017-01-28.11:16:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1485602214.48.0.510848976723.issue10544@psf.upfronthosting.co.za>
In-reply-to
Content
Just to add my comment to this 7-years-old never-resolved issue: in PyPy 3.5, which behaves like Python 3.x in this respect, I made the following constructions give a warning.

    def wrong_listcomp():
        return [(yield 42) for i in j]
    def wrong_gencomp():
        return ((yield 42) for i in j)
    def wrong_dictcomp():
        return {(yield 42):2 for i in j}
    def wrong_setcomp():
        return {(yield 42) for i in j}

SyntaxWarning: 'yield' inside a list or generator comprehension behaves unexpectedly (http://bugs.python.org/issue10544)

The motivation is that none of the constructions above gives the "expected" result.  In more details:

- wrong_listcomp() doesn't even return a list at all.  It's possible to have a clue about why this occurs, but I would say that it is just plain wrong given the ``return [...]`` part of the syntax.  The same is true for wrong_dictcomp() and wrong_setcomp().

- wrong_gencomp() returns a generator as expected.  However, it is a generator that yields two elements for each i in j: first 42, and then whatever was ``send()`` into the generator.  I would say that it is in contradiction with the general idea that this syntax should give a generator that yields one item for each i in j.  In fact, when the user writes such code he might be expecting the "yield" to apply to the function level instead of the genexpr level---but none of the functions above end up being themselves generators.

For completeness, I think there is no problem with "await" instead of "yield" in Python 3.6.

How about fixing CPython to raise SyntaxWarning or even SyntaxError?
History
Date User Action Args
2017-01-28 11:16:54arigosetrecipients: + arigo, gvanrossum, georg.brandl, rhettinger, belopolsky, benjamin.peterson, erickt, glyph, Inyeol.Lee, esc24, danielsh, emptysquare, levkivskyi
2017-01-28 11:16:54arigosetmessageid: <1485602214.48.0.510848976723.issue10544@psf.upfronthosting.co.za>
2017-01-28 11:16:54arigolinkissue10544 messages
2017-01-28 11:16:53arigocreate