StopIteration
detected in a generator PYL-R1708256 if state["pymode"][-1][0]:
257 return
258 # add a whitespace token after a redirection, if we need to
259 next_tok = next(state["stream"])260 if next_tok.start == token.end:
261 yield _new_token("WS", "", token.end)
262 yield from handle_token(state, next_tok)
According to PEP479, the raise of StopIteration
to end the loop of a generator may lead to hard to find bugs. This PEP specifies that raise StopIteration
has to be replaced by a simple return
statement.
def some_generator(n):
for i in range(n):
yield i
if i >= 5:
raise StopIteration # Will raise an exception
def some_generator(n):
for i in range(n):
yield i
if i >= 5:
return # Will close the generator