Skip to content

inconsistency in list-generator comprehension with yield(-from) #66163

Closed

Description

@httpsgithubcomhakril
BPO 21964
Nosy @ncoghlan, @vstinner, @ezio-melotti, @serhiy-storchaka, @https://github.com/hakril, @iritkatriel
Superseder
  • bpo-10544: yield expression inside generator expression does nothing
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-12-11.11:04:09.084>
    created_at = <Date 2014-07-11.23:06:27.360>
    labels = ['interpreter-core', 'type-bug']
    title = 'inconsistency in list-generator comprehension with yield(-from)'
    updated_at = <Date 2021-12-11.11:04:09.080>
    user = 'https://github.com/httpsgithubcomhakril'

    bugs.python.org fields:

    activity = <Date 2021-12-11.11:04:09.080>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-11.11:04:09.084>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2014-07-11.23:06:27.360>
    creator = 'hakril'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 21964
    keywords = []
    message_count = 7.0
    messages = ['222811', '222877', '222898', '223026', '223605', '408251', '408294']
    nosy_count = 7.0
    nosy_names = ['gcewing', 'ncoghlan', 'vstinner', 'ezio.melotti', 'serhiy.storchaka', 'hakril', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '10544'
    type = 'behavior'
    url = 'https://bugs.python.org/issue21964'
    versions = ['Python 3.4', 'Python 3.5']

    Activity

    1. httpsgithubcomhakril commented on Jul 11, 2014

      httpsgithubcomhakrilmannequin
      MannequinAuthor

      Will playing with generators and yield from I found some inconsistency.

      list comprehension with yield(-from) would return a generator.
      generator comprehension would yield some None in the middle of the expected values.

      Examples:
      l = ["abc", range(3)]
      g1 = [(yield from i) for i in l]
      print(g)
      <generator object <listcomp> at 0x7f5ebd58b690>
      print(list(g))
      ['a', 'b', 'c', 0, 1, 2] # this result is super cool !

          g2 = ((yield from i) for i in l)
          print(g2)
          <generator object <genexpr> at 0x7f5ebd58b6e0>
          print(list(g2))
          ['a', 'b', 'c', None, 0, 1, 2, None]

      For g1: it returns a generator because the listcomp contains a yield from.

      For g2 it append None because it yield the return value of yield from i.
      It could be rewritten as:
      def comp(x):
      for i in x:
      yield (yield from i)

    2. added
      interpreter-core
      type-bug
      on Jul 11, 2014
    3. ezio-melotti commented on Jul 12, 2014

      @ezio-melotti
      Member

      There seem to be two issues here:

      list comprehension with yield(-from) would return a generator.

      This is somewhat surprising, and I'm not sure it's expected/documented.
      The example you provided seems to behave reasonably, so I don't think we should change the behavior (unless there is some actual bug in similar example). If anything, we could document this, but I'm not sure if it's worth doing it and where could be added.

      generator comprehension would yield some None in the middle
      of the expected values.

      This also seems expected, and the behavior is consistent with the equivalent generator function.

    4. ncoghlan commented on Jul 13, 2014

      @ncoghlan
      Contributor

      It's a side effect of the hidden closure that provides the new scope for the iteration variable - that's an ordinary function object, so using yield or yield from turns it into a generator expression instead. Generator expressions are already generators, so using yield or yield from just adds more yield points beyond the implied ones.

      I've never figured out a good way to document it - it's a natural consequence of the comprehension's closure. An explicit mention in the reference docs for comprehensions may be worth adding.

    5. httpsgithubcomhakril commented on Jul 14, 2014

      httpsgithubcomhakrilmannequin
      MannequinAuthor

      I found something else, I think it worth mentioning it.

      This side-effect allows to create generators that return other values that None. And the CPython's behavior is not the same for all versions:

          >>> {(yield i) : i for i in range(2)}
          <generator object <dictcomp> at 0x7f0b98f41410>
          >>> list(_)
          # python3.(3,4,5)
          [0, 1]
          # python3.2
          [0, 1, {None: 1}] #  python3.2 appends the generator's return value.
    6. vstinner commented on Jul 21, 2014

      @vstinner
      Member

      For g1: it returns a generator because the listcomp contains a yield from.

      IMO it's a bug: [... for ... in ...] must create a list.

    7. iritkatriel commented on Dec 10, 2021

      @iritkatriel
      Member

      I think this issue was resolved by now. This is what happens on 3.11:

      >>> l = ["abc", range(3)]
      >>> g = [(yield from i) for i in l]
        File "<stdin>", line 1
      SyntaxError: 'yield' inside list comprehension
      
      >>> g2 = ((yield from i) for i in l)
        File "<stdin>", line 1
      SyntaxError: 'yield' inside generator expression
    8. serhiy-storchaka commented on Dec 11, 2021

      @serhiy-storchaka
      Member

      Resolved in bpo-10544.

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-coretype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions