Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42435

Re: collections.Iterator __subclasshook__ does not check if next() is callable

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'subject:not': 0.03; 'root': 0.05; 'tests.': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'implemented.': 0.09; 'message- id:@stoneleaf.us': 0.09; 'method,': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'rejected': 0.09; 'url:github': 0.09; '~ethan~': 0.09; 'def': 0.12; 'bug': 0.12; 'wrote': 0.14; '@property': 0.16; 'a()': 0.16; 'a(object):': 0.16; 'byron': 0.16; 'callable': 0.16; 'callable.': 0.16; 'collections': 0.16; 'conforming': 0.16; 'downstream': 0.16; 'incorrect': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'justified': 0.16; 'ruth': 0.16; 'subject:skip:_ 10': 0.16; 'typeerror:': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User- Agent:1': 0.23; 'skip:` 20': 0.24; 'defined': 0.27; 'header:In- Reply-To:1': 0.27; 'url:bugs': 0.29; '(this': 0.29; 'am,': 0.29; 'raymond': 0.30; 'work.': 0.31; 'assumes': 0.31; 'concern': 0.31; 'existence': 0.31; 'libraries': 0.31; 'subject:next': 0.31; 'class': 0.32; 'url:python': 0.33; '(i.e.': 0.33; 'implemented': 0.33; 'not.': 0.33; '"the': 0.34; 'skip:_ 10': 0.34; 'problem': 0.35; "who's": 0.35; 'but': 0.35; 'returning': 0.36; 'next': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'example,': 0.37; 'requirements': 0.37; 'feedback': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'received:173': 0.61; 'determine': 0.67; 'received:69.56': 0.68; 'behavior': 0.77; 'subject:check': 0.84; 'technically': 0.84; 'url:master': 0.84; 'received:gateway02.websitewelcome.com': 0.91
Date Sun, 31 Mar 2013 17:02:18 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: collections.Iterator __subclasshook__ does not check if next() is callable
References <a108caf6-538b-4ca0-978a-97bacc7ca7e8@googlegroups.com>
In-Reply-To <a108caf6-538b-4ca0-978a-97bacc7ca7e8@googlegroups.com>
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender ([173.12.184.235]) [173.12.184.235]:57988
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4044.1364775079.2939.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364775079 news.xs4all.nl 6979 [2001:888:2000:d::a6]:33158
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42435

Show key headers only | View raw


On 03/31/2013 10:57 AM, Byron Ruth wrote:
> I submitted this as bug last night: http://bugs.python.org/issue17584 and was *honored* to be rejected by Raymond Hettinger. However, I would like feedback on whether my concern (this bug) is justified and clarity if not.
>
> Consider:
>
> ```python
> class A(object):
>      def __init__(self):
>          self.r = iter(range(5))
>      def __iter__(self):
>          return self
>      @property
>      def next(self):
>          return next(self.r)
> ```
>
> The `next` method is a property, however:
>
> ```python
> from collections import Iterator
> a = A()
> isinstance(a, Iterator) # True
> next(a) # TypeError: 'int' object is not callable
> ```
>
> I am using `collections.Iterator` as the means to check if the object is an iterator, however I am not sure if that is _root_ problem here. My understanding of the iterator protocol is that is assumes the __iter__ and next *methods* are implemented. In the example, `A.next` is defined as a property, but is still identified as an iterator. To me, this is incorrect behavior since it's not conforming to the iterator protocol requirements (i.e. a `next` method, not a property).

The root problem is that whoever implemented A did so incorrectly.


> Raymond stated: "The design of ABCs are to check for the existence to required named; none of them verify the signature." I think I understand _why_ this is the case.. but I downstream libraries use `collections.Iterator` to determine if an object _is one_: see https://github.com/django/django/blob/master/django/utils/itercompat.py#L22-L31
>
> Who's job is it to check if `next` (and technically `__iter__`) are methods?

The programmer who wrote it, with good tests.

Technically, the issue is not that 'next' is a property, but that it is not returning a callable.  If it did (and the 
callable was appropriate for the iterator), that would also work.

--
~Ethan~

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 10:57 -0700
  Re: collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 12:44 -0700
  Re: collections.Iterator __subclasshook__ does not check if next() is callable Terry Jan Reedy <tjreedy@udel.edu> - 2013-03-31 15:47 -0400
    Re: collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 12:58 -0700
    Re: collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 12:58 -0700
  Re: collections.Iterator __subclasshook__ does not check if next() is callable Ethan Furman <ethan@stoneleaf.us> - 2013-03-31 17:02 -0700

csiph-web