That's a good scenario, but it doesn't traceback with the SafeGenerator
implementation above.
args = SafeGenerator(iter(['mkdir.py', 'dir']))
progname = next(args)
mandatory_arg = next(args)
# Generator is empty, but "we don't know it yet" (no StopIteration), so
is_exhausted == False
for arg in args:
print('optional arg', next(arg))
# StopIteration is raised, so is_exhausted == True. Still no traceback.
# Added mistake:
print('Parsed args:', ' '.join(args))
# ValueError: can't iterate over an already exhausted generator
And even if there are rare false positives, I'd still love a flag to enable
this warning. I hit this bug every few weeks and it's a pain to troubleshoot.
Worse than strays NaN, None, or Javascript's "undefined", because logging it
shows a normal-looking empty list.
BoppreH
On Mon, Jun 12, 2023, at 10:11 PM, Barry wrote:
>
>
> > On 12 Jun 2023, at 16:55, BoppreH via Python-ideas
> > <[email protected]> wrote:
> >
> > Then the empty list creates hard-to-track bugs. I'm familiar with the
> > iterator protocol and why the behavior above happens, but couldn't it be
> > prevented?
>
> I don’t think so. It is not always a bug that an iterator is empty.
> For example this pattern:
>
> args = iter(sys.argv)
> progname = next(args)
> mandatory_arg = next(args)
> for arg in args:
> print(‘optional arg’, next(arg))
>
> Your proposal will traceback for the for loop if there are no optional args.
>
> Barry
>
>
>
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/2Y4PLP52UWGPGWCRYH5WENLXTLRQBQEO/
Code of Conduct: http://python.org/psf/codeofconduct/