Unexpected PendingDeprecationWarning

MRAB python at mrabarnett.plus.com
Tue Nov 22 22:02:22 EST 2016


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/




More information about the Python-list mailing list