Skip to content

In itertools.chain.from_iterable() there is no cls argument #62501

Description

@py-user
mannequin
BPO 18301
Nosy @rhettinger, @terryjreedy, @ezio-melotti, @bitdancer, @py-user, @serhiy-storchaka
Files
  • issue18301.diff
  • 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 = 'https://github.com/rhettinger'
    closed_at = <Date 2013-09-09.06:57:17.906>
    created_at = <Date 2013-06-25.18:58:43.184>
    labels = ['type-feature', 'docs']
    title = 'In itertools.chain.from_iterable() there is no cls argument'
    updated_at = <Date 2013-09-09.06:57:17.905>
    user = 'https://github.com/py-user'

    bugs.python.org fields:

    activity = <Date 2013-09-09.06:57:17.905>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2013-09-09.06:57:17.906>
    closer = 'rhettinger'
    components = ['Documentation']
    creation = <Date 2013-06-25.18:58:43.184>
    creator = 'py.user'
    dependencies = []
    files = ['30702']
    hgrepos = []
    issue_num = 18301
    keywords = ['patch']
    message_count = 11.0
    messages = ['191874', '191875', '191884', '192009', '192015', '194535', '194668', '194701', '194712', '195291', '197352']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'terry.reedy', 'ezio.melotti', 'r.david.murray', 'docs@python', 'py.user', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue18301'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. py-user commented on Jun 25, 2013

      py-usermannequin
      MannequinAuthor

      http://docs.python.org/2/library/itertools.html#itertools.chain.from_iterable

      >>> class A:
      ...   @classmethod
      ...   def from_iterable(iterables):
      ...     for it in iterables:
      ...       for element in it:
      ...         yield element
      ... 
      >>> A.from_iterable(['ABC', 'DEF'])
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: from_iterable() takes 1 positional argument but 2 were given
      >>>
    2. added
      docsDocumentation in the Doc dir
      type-featureA feature request or enhancement
      on Jun 25, 2013
    3. py-user commented on Jun 25, 2013

      py-usermannequin
      MannequinAuthor
    4. bitdancer commented on Jun 25, 2013

      @bitdancer
      Member

      It is implemented as a classmethod, but the "equivalent" code doesn't need to be part of the class all. I'm not sure what should be done here (say @staticmethod? Leave the decorator off?). We should probably see what Raymond thinks. I lean toward the latter, that's the way it is in the python2 docs, and it doesn't seem to have caused any confusion.

    5. terryjreedy commented on Jun 28, 2013

      @terryjreedy
      Member

      The 2.7 doc says 'Roughly equivalent to' rather than 'Equivalent to'.
      The undecorated Python version of from_iterable actually works as an attribute of the Python version of chain: chain.from_iterable = from_iterable. I would just remove the decorator. The entire itertools doc exploits that fact that generator functions are analogous to classes, but it does not work to mix the two in the way that the 3.x chain entry does.

    6. serhiy-storchaka commented on Jun 28, 2013

      @serhiy-storchaka
      Member

      Perhaps it should be staticmethod, not classmethod.

    7. serhiy-storchaka commented on Aug 6, 2013

      @serhiy-storchaka
      Member

      It should be a classmethod.

      >>> import itertools
      >>> class C(itertools.chain): pass
      ... 
      >>> type(C.from_iterable(['ab', 'cd']))
      <class '__main__.C'>

      The patch LGTM.

    8. ezio-melotti commented on Aug 8, 2013

      @ezio-melotti
      Member

      I would just remove the decorator.

      +1

    9. py-user commented on Aug 8, 2013

      py-usermannequin
      MannequinAuthor
      >>> import itertools
      >>> 
      >>> class A(itertools.chain):
      ...     def from_iter(arg):
      ...         return A(iter(arg))
      ... 
      >>> class B(A):
      ...     pass
      ... 
      >>> B('a', 'b')
      <__main__.B object at 0x7f40116d7730>
      >>> B.from_iter(['a', 'b'])
      <__main__.A object at 0x7f40116d7780>
      >>>

      it should be B

    10. py-user commented on Aug 9, 2013

      py-usermannequin
      MannequinAuthor

      changed iter(arg) to *arg

      >>> import itertools
      >>> 
      >>> class A(itertools.chain):
      ...     @classmethod
      ...     def from_iter(cls, arg):
      ...         return cls(*arg)
      ... 
      >>> class B(A):
      ...     pass
      ... 
      >>> B('ab', 'cd')
      <__main__.B object at 0x7fc280e93cd0>
      >>> b = B.from_iter(['ab', 'cd'])
      >>> b
      <__main__.B object at 0x7fc280e93d20>
      >>> next(b)
      'a'
      >>> next(b)
      'b'
      >>> next(b)
      'c'
      >>> next(b)
      'd'
      >>> next(b)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      StopIteration
      >>>
    11. terryjreedy commented on Aug 15, 2013

      @terryjreedy
      Member

      My counter proposal bpo-18752 is that chain.from_iterable become a deprecated alias for a new function, chain_iterable. With '@classmethod' removed, the current Python equivalent would work for chain_iterable.

    12. python-dev commented on Sep 9, 2013

      python-devmannequin
      Mannequin

      New changeset 29fa1f418796 by Raymond Hettinger in branch '3.3':
      bpo-18301: The classmethod decorator didn't fit well with the rough-equivalent example code.
      http://hg.python.org/cpython/rev/29fa1f418796

    13. 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

    Labels

    docsDocumentation in the Doc dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions