[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Christopher Barker
I know this is not the point of this thread, but when i saw: ``` numbers = (i for i in range(10)) assert 3 in numbers next(numbers)# 4! ``` I was totally surprised that generator expressions support `in` -- WTF? they very much are NOT containers! And, in fact, all iterators support `in` --

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Chris Angelico
On Wed, 14 Jun 2023 at 09:05, BoppreH via Python-ideas wrote: > > @ChrisA: There are already flags for enabling warnings on dangerous bytearray > comparisons[1] or relying on locale-dependent encodings[2], not to mention a > whole Development Mode flag[3] that adds extra checks. Some of those

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread BoppreH via Python-ideas
@ChrisA: There are already flags for enabling warnings on dangerous bytearray comparisons[1] or relying on locale-dependent encodings[2], not to mention a whole Development Mode flag[3] that adds extra checks. Some of those checks affect fewer people than my proposal. A "warn on reused

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Bruce Leban
I think the discussion is sort of missing a very common use case, when a user calls func(iterator) and the function is expecting an iterable but not an iterator. The author of the called code might be thinking that the input is a list but that doen't mean the caller thinks that. Even worse is a

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Dom Grigonis
> > If I wanted sorted numbers, then ValueError wouldn’t help, because I do not > > get sorted numbers. > > I do want sorted numbers, but what can Python do in the face of broken code? > There's a reason it raises errors for 1/0, str.invalid, and len(None). It's > not "helpful" to the

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread David Mertz, Ph.D.
I've also been a Python user for 24 years now. Since long before iterators were a feature of Python. I wrote quite a few widely read articles about iterators when they were introduced, including the first about leverageing iterators for coroutines. I can't say I've NEVER encountered a glitch

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Chris Angelico
On Wed, 14 Jun 2023 at 07:52, BoppreH via Python-ideas wrote: > > Sorry, I'm new to the list and was not aware the burden of proof was so high. > Can you point me to one or two successful posts in Python-ideas where I can > learn how to show there's a real need for a feature? > It's more a

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread BoppreH via Python-ideas
Sorry, I'm new to the list and was not aware the burden of proof was so high. Can you point me to one or two successful posts in Python-ideas where I can learn how to show there's a real need for a feature? On Tue, Jun 13, 2023, at 11:25 PM, Chris Angelico wrote: > On Wed, 14 Jun 2023 at 07:02,

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Chris Angelico
On Wed, 14 Jun 2023 at 07:02, BoppreH via Python-ideas wrote: > > > In close to 10 years of experience with python I have never encountered > > anything like this. > > Here's a small selection of the StackOverflow questions from people who > encountered this exact issue: But now try to find

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread BoppreH via Python-ideas
> In close to 10 years of experience with python I have never encountered > anything like this. Here's a small selection of the StackOverflow questions from people who encountered this exact issue:

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Dom Grigonis
In close to 10 years of experience with python I have never encountered anything like this. If I need to use a list later I never do ANY assignments to it. Why would I? In the last example I would: ``` strings = ['aa', '', 'bbb', 'c’] longest = max(filter(bool, strings), key=len) n_unique =

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread MRAB
On 2023-06-13 16:42, Chris Angelico wrote: On Wed, 14 Jun 2023 at 01:07, BoppreH via Python-ideas wrote: And I have to say I'm surprised by the responses. Does nobody else hit bugs like this and wish they were automatically detected? Nope, I've never had that happen to me, and I *have*

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Chris Angelico
On Wed, 14 Jun 2023 at 01:07, BoppreH via Python-ideas wrote: > And I have to say I'm surprised by the responses. Does nobody else hit bugs > like this and wish they were automatically detected? > Nope, I've never had that happen to me, and I *have* made use of calling iter() on

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread BoppreH via Python-ideas
@ChrisA: Shadowing "iter()" would only help with Barry's example. @Jonathan: Updating documentation is helpful, but I find an automated check better. Too often the most obvious way to accomplish something silently triggers this behavior: strings = ['aa', '', 'bbb', 'c'] strings = filter(bool,

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Chris Angelico
On Tue, 13 Jun 2023 at 21:03, BoppreH via Python-ideas wrote: > > Any thoughts on logging a warning, perhaps behind an opt-in flag? I could not > find a single false positive scenario, so I expect the signal-to-noise ratio > to be high and prevent lots of bugs. > Shadow the iter() function and

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread Jonathan Fine
The original example is: numbers = (i for i in range(5)) assert 5 not in numbers sorted(numbers) I like this example. It provides an opportunity to improve the documentation. The problems goes away if we write any of the following numbers = [i for i in range(5)] numbers =

[Python-ideas] Re: Warn when iterating over an already exhausted generator

2023-06-13 Thread BoppreH via Python-ideas
Any thoughts on logging a warning, perhaps behind an opt-in flag? I could not find a single false positive scenario, so I expect the signal-to-noise ratio to be high and prevent lots of bugs. BoppreH On Tue, Jun 13, 2023, at 1:05 AM, Greg Ewing wrote: > On 13/06/23 9:59 am, Rob Cliffe via

[Python-ideas] Re: yield functionality to match that of await

2023-06-13 Thread Chris Angelico
On Tue, 13 Jun 2023 at 14:54, Greg Ewing wrote: > > On 13/06/23 11:38 am, Chris Angelico wrote: > > (Fun fact: Pike looked at what Python was doing, and came up with a > > concept of "continue functions" > > And I gather that the "async" and "await" keywords came > from C#. Languages are always