[issue33939] Raise TypeError in __length_hint__ for consistently infinite iterators

2018-06-22 Thread Erik Bray


Erik Bray  added the comment:

Per the discussion in https://trac.sagemath.org/ticket/24757, it would also be 
nice if some other common iterables like map and filter implemented a similar 
__length_hint__ which would interrogate whatever iterables were give as their 
argument(s).

To summarize the discussion in that ticket, it would be nice to at least know 
if I can expect some iterable to be finite without actually knowing its length. 
 Sure, if I passed in some arbitrary generator there's no way for the 
intepreter to know a priori if it will terminate.  But I would at least like to 
be able to manually mark whether I expect, as an intelligent developer, that 
under normal conditions the generator *should* terminate.  And this information 
should filter up to other iterators that I pass my generator to.  That way I 
can code defensively around whether or not I at least expect an iterator to be 
finite (even if not of a known length).

--
nosy: +erik.bray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33939] Raise TypeError in __length_hint__ for consistently infinite iterators

2018-06-22 Thread Jeroen Demeyer


Jeroen Demeyer  added the comment:

By the way, there is a precedent for OverflowError on infinities:

>>> import math
>>> int(math.inf)
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: cannot convert float infinity to integer

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33939] Raise TypeError in __length_hint__ for consistently infinite iterators

2018-06-22 Thread Jeroen Demeyer


Jeroen Demeyer  added the comment:

I'm certainly in favor of adding a way for infinite iterators to state that 
they are infinite. I just feel like TypeError is way overused in Python. I also 
disagree that it's a category error: it's an iterable, so it does make sense to 
ask for its length.

You should also consider that there may be classes of iterables whose instances 
are sometimes finite and sometimes infinite. Then it's certainly not a category 
error.

So I would like a specific answer for "I'm infinite". If you don't like 
OverflowError, other options are ArithmeticError, IndexError, ValueError or 
simply returning math.inf

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33939] Raise TypeError in __length_hint__ for consistently infinite iterators

2018-06-22 Thread Nick Coghlan


Nick Coghlan  added the comment:

OverflowError covers cases like greater-than-sys.maxsize range instances, where 
the object itself is conceptually finite, but the length can't be represented 
as a C integer.

This case is different: it's a category error where the question being asked 
doesn't even make sense for the affected type.

However, unlike len(), where a missing __len__() implementation inherently 
raises TypeError, backwards compatibility requires operator.length_hint() to 
handle a missing implementation __length_hint__ implementation as equivalent to 
an implementation that returns a length hint of 0.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33939] Raise TypeError in __length_hint__ for consistently infinite iterators

2018-06-22 Thread Jeroen Demeyer


Jeroen Demeyer  added the comment:

Why TypeError? Wouldn't OverflowError be more suitable?

--
nosy: +jdemeyer

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33939] Raise TypeError in __length_hint__ for consistently infinite iterators

2018-06-22 Thread Nick Coghlan


New submission from Nick Coghlan :

This is a simpler proposal born out of https://bugs.python.org/issue31815: 
adding __length_hint__ implementations to some known-infinite iterators in 
itertools that always raise TypeError.

This means that iterator consumers that use the operator.length_hint() API will 
throw an error when asked to consume them, rather than entering an 
uninterruptible infinite loop.

The proposed methods added by this proposal would be:

* itertools.count.__length_hint__
* itertools.cycle.__length_hint__
* itertools.repeat.__length_hint__

Each of these would raise TypeError (to indicate an explicitly non-finite 
length), rather than returning the default of 0 (which length hint API 
consumers typically interpret as indicating an unknown-but-finite length)

--
messages: 320229
nosy: ncoghlan, rhettinger
priority: normal
severity: normal
stage: needs patch
status: open
title: Raise TypeError in __length_hint__ for consistently infinite iterators
type: enhancement
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com