Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-10544: Deprecate "yield" in comprehensions and generator expressions. #4579

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Nov 27, 2017

@ncoghlan
Copy link
Contributor

@serhiy-storchaka Do you mind if I just edit some of the docs wording directly? I think that will be easier than trying to describe the edits I would recommend.

@serhiy-storchaka
Copy link
Member Author

I will be grateful if you do this.

- updated the comprehension scoping definition to use
  some of the words from the generator expression definition
- added appropriate versionadded/changed directives
-
@@ -201,6 +210,12 @@ or :keyword:`await` expressions it is called an
suspend the execution of the coroutine function in which it appears.
See also :pep:`530`.

.. versionadded:: 3.6
Asynchronous comprehensions were introduced
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed a period.

and then passed as an argument to the implictly nested scope. Subsequent
:keyword:`for` clauses cannot be evaluated in the enclosing scope since they
may depend on the previous :keyword:`for` loop. For example:
``[x*y for x in range(10) for y in bar(x)]``.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use more concrete real-world example? E.g. for y in range(x+1, 10).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lifted this one directly from the generator expression docs, but changing it in both places would definitely be reasonable.

.. versionadded:: 3.6
Asynchronous comprehensions were introduced

.. versionchanged:: 3.7
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be deprecated or deprecated-removed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye, it should - I just forgot there was a specific directive for deprecations. I'll go through and fix all the cases.

@ncoghlan
Copy link
Contributor

OK, I've made my documentation edits.

@gvanrossum I think it would be helpful if you could review the proposed language reference updates directly, or else nominate someone else to do so.

@@ -563,7 +563,10 @@ Deprecated
potentially returning a :term:`generator-iterator` object), while generator
expressions won't attempt to interleave their implicit output with the output
from any explicit yield expressions.
(Contributed by Serhiy Storchaka in :issue:`10544`.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces.


.. versionchanged:: 3.7
.. deprecated:: 3.7
``yield`` and ``yield from`` deprecated in the implicitly nested scope
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed a period.

in comprehensions and generator expressions (aside from the outermost iterable
defined in the leftmost :keyword`for` clause). This ensures that comprehensions
always immediately return a container of the appropriate type (rather than
potentially returning a :term:`generator-iterator` object), while generator
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning, treated as error:
/home/travis/build/python/cpython/Doc/whatsnew/3.7.rst:559:term not in glossary: generator-iterator

Asynchronous comprehensions were introduced.

.. deprecated:: 3.7
``yield`` and ``yield from`` deprecated in the implicitly nested scope
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed a period.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some suggestions to tighten the docs. Thanks to both of you for taking care of this!

@@ -183,8 +183,20 @@ by considering each of the :keyword:`for` or :keyword:`if` clauses a block,
nesting from left to right, and evaluating the expression to produce an element
each time the innermost block is reached.

Note that the comprehension is executed in a separate scope, so names assigned
to in the target list don't "leak" into the enclosing scope.
However, aside from the leftmost :keyword:`for` clause, the comprehension is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "leftmost for clause" is still ambiguous. Does this include the in for a[<expr>] in ...? It should not, but I would assume it's part of "the for clause." Maybe say "the iterable in the leftmost for clause"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using just "iterable" ended up looking odd in context (since executing an iterable could be taken to mean iterating over it).

However, "the iterable expression in the leftmost :keyword:for clause" reads reasonably well to me, and I also made it explicit that the leftmost filter condition is evaluated in the nested scope.

It may be worth our while to explicitly define "outermost iterable" as a formal term in the glossary, but I think that would be better handled as a distinct docs RFE.

@@ -556,6 +556,18 @@ Other CPython Implementation Changes
Deprecated
==========

* Yield expressions (both ``yield`` and ``yield from`` clauses) are now deprecated
in comprehensions and generator expressions (aside from the outermost iterable
defined in the leftmost :keyword:`for` clause). This ensures that comprehensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd drop "defined" here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the changes I made to the language reference, I replaced "the outermost iterable defined in" with "the iterable expression in" here.

- consistently use "leftmost" (never "outermost")
- explicitly exclude the filter condition from eager evaluation
- better align wording for comprehensions and generator expressions
@gvanrossum
Copy link
Member

Docs LGTM now.

to get a more accurate error report */
PyErr_Clear();
PyErr_SetObject(PyExc_SyntaxError, msg);
PyErr_SyntaxLocationObject(st->st_filename,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@serhiy-storchaka
Copy link
Member Author

Ping.

@ncoghlan ncoghlan merged commit 73a7e9b into python:master Dec 1, 2017
@serhiy-storchaka serhiy-storchaka deleted the deprecate-yield-in-comprehensions branch December 1, 2017 06:23
@serhiy-storchaka
Copy link
Member Author

Thank you for your documentation changes @ncoghlan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants