Thanks, I was not aware of that PEP.
The logic in my function is exactly as desired, so to squelch the warning, I merely wrapped the iteration in a try/except: def adjacent_difference(seq, selector=identity): i = iter(seq) l = selector(next(i)) try: while True: r = selector(next(i)) yield r - l l = r except StopIteration: return On Tue, Nov 22, 2016 at 9:02 PM, MRAB <pyt...@mrabarnett.plus.com> wrote: > On 2016-11-23 02:50, Nathan Ernst wrote: > >> I'm using Python 3.5.2, and the following code (when invoked) causes a >> PendingDeprecationWarning when used in a unit test: >> >> def identity(x): >> return x >> >> def adjacent_difference(seq, selector=identity): >> i = iter(seq) >> l = selector(next(i)) >> while True: >> r = selector(next(i)) >> yield r - l >> l = r >> >> I wrote this to mimic the C++ std algorithm (defined here: >> http://en.cppreference.com/w/cpp/algorithm/adjacent_difference). >> >> What I don't understand is why I get this warning. >> >> The exact error message I get from unittest is: >> PendingDeprecationWarning: generator 'adjacent_difference' raised >> StopIteration >> >> I'd appreciate any insight into what is causing this deprecation warning, >> as I am stumped. >> >> The 'while' loop keeps calling next(i) until it raises StopIteration, and > that kind of behaviour can hide obscure bugs. > > If you want to know the details, you can read the rationale behind the > change in the relevant PEP: > > PEP 479 -- Change StopIteration handling inside generators > https://www.python.org/dev/peps/pep-0479/ > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list