Call to
next()
should be wrapped in try-except
607 rv = self._after
608 self._after = missing
609 else:
610 rv = next(self._iterator) 611
612 self.index0 += 1
613 self._before = self._current
Call to
next()
should be wrapped in try-except
372 }
373 and self.stream.look().test("name:context")
374 ):
375 node.with_context = next(self.stream).value == "with" 376 self.stream.skip()
377 return True
378 return False
Call to
next()
should be wrapped in try-except
951 negated = False
952 name = self.stream.expect("name").value
953 while self.stream.current.type == "dot":
954 next(self.stream) 955 name += "." + self.stream.expect("name").value
956 dyn_args = dyn_kwargs = None
957 kwargs = []
Call to
next()
should be wrapped in try-except
945 def parse_test(self, node: nodes.Expr) -> nodes.Expr:
946 token = next(self.stream)
947 if self.stream.current.test("name:not"):
948 next(self.stream) 949 negated = True
950 else:
951 negated = False
Call to
next()
should be wrapped in try-except
943 return node
944
945 def parse_test(self, node: nodes.Expr) -> nodes.Expr:
946 token = next(self.stream) 947 if self.stream.current.test("name:not"):
948 next(self.stream)
949 negated = True
Call to
next()
should be wrapped in try-except
928 token = self.stream.expect("name")
929 name = token.value
930 while self.stream.current.type == "dot":
931 next(self.stream) 932 name += "." + self.stream.expect("name").value
933 if self.stream.current.type == "lparen":
934 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
Call to
next()
should be wrapped in try-except
924 ) -> t.Optional[nodes.Expr]:
925 while self.stream.current.type == "pipe" or start_inline:
926 if not start_inline:
927 next(self.stream) 928 token = self.stream.expect("name")
929 name = token.value
930 while self.stream.current.type == "dot":
Call to
next()
should be wrapped in try-except
889 dyn_args = self.parse_expression()
890 elif self.stream.current.type == "pow":
891 ensure(dyn_kwargs is None)
892 next(self.stream) 893 dyn_kwargs = self.parse_expression()
894 else:
895 if (
Call to
next()
should be wrapped in try-except
885
886 if self.stream.current.type == "mul":
887 ensure(dyn_args is None and dyn_kwargs is None)
888 next(self.stream) 889 dyn_args = self.parse_expression()
890 elif self.stream.current.type == "pow":
891 ensure(dyn_kwargs is None)
Call to
next()
should be wrapped in try-except
853 args.append(None)
854
855 if self.stream.current.type == "colon":
856 next(self.stream) 857 if self.stream.current.type not in ("rbracket", "comma"):
858 args.append(self.parse_expression())
859 else:
Call to
next()
should be wrapped in try-except
842 node = self.parse_expression()
843 if self.stream.current.type != "colon":
844 return node
845 next(self.stream) 846 args = [node]
847
848 if self.stream.current.type == "colon":
Call to
next()
should be wrapped in try-except
836 args: t.List[t.Optional[nodes.Expr]]
837
838 if self.stream.current.type == "colon":
839 next(self.stream) 840 args = [None]
841 else:
842 node = self.parse_expression()
Call to
next()
should be wrapped in try-except
808
809 if token.type == "dot":
810 attr_token = self.stream.current
811 next(self.stream) 812 if attr_token.type == "name":
813 return nodes.Getattr(
814 node, attr_token.value, "load", lineno=token.lineno
Call to
next()
should be wrapped in try-except
803 def parse_subscript(
804 self, node: nodes.Expr
805 ) -> t.Union[nodes.Getattr, nodes.Getitem]:
806 token = next(self.stream) 807 arg: nodes.Expr
808
809 if token.type == "dot":
Call to
next()
should be wrapped in try-except
668 next(self.stream)
669 node = nodes.Const(token.value, lineno=token.lineno)
670 elif token.type == "lparen":
671 next(self.stream) 672 node = self.parse_tuple(explicit_parentheses=True)
673 self.stream.expect("rparen")
674 elif token.type == "lbracket":
Call to
next()
should be wrapped in try-except
665 next(self.stream)
666 node = nodes.Const("".join(buf), lineno=lineno)
667 elif token.type in ("integer", "float"):
668 next(self.stream) 669 node = nodes.Const(token.value, lineno=token.lineno)
670 elif token.type == "lparen":
671 next(self.stream)
Call to
next()
should be wrapped in try-except
662 lineno = token.lineno
663 while self.stream.current.type == "string":
664 buf.append(self.stream.current.value)
665 next(self.stream) 666 node = nodes.Const("".join(buf), lineno=lineno)
667 elif token.type in ("integer", "float"):
668 next(self.stream)
Call to
next()
should be wrapped in try-except
657 node = nodes.Name(token.value, "load", lineno=token.lineno)
658 next(self.stream)
659 elif token.type == "string":
660 next(self.stream) 661 buf = [token.value]
662 lineno = token.lineno
663 while self.stream.current.type == "string":
Call to
next()
should be wrapped in try-except
655 node = nodes.Const(None, lineno=token.lineno)
656 else:
657 node = nodes.Name(token.value, "load", lineno=token.lineno)
658 next(self.stream) 659 elif token.type == "string":
660 next(self.stream)
661 buf = [token.value]
Call to
next()
should be wrapped in try-except
636 next(self.stream)
637 node = nodes.Neg(self.parse_unary(False), lineno=lineno)
638 elif token_type == "add":
639 next(self.stream) 640 node = nodes.Pos(self.parse_unary(False), lineno=lineno)
641 else:
642 node = self.parse_primary()
Call to
next()
should be wrapped in try-except
633 node: nodes.Expr
634
635 if token_type == "sub":
636 next(self.stream) 637 node = nodes.Neg(self.parse_unary(False), lineno=lineno)
638 elif token_type == "add":
639 next(self.stream)
Call to
next()
should be wrapped in try-except
621 lineno = self.stream.current.lineno
622 left = self.parse_unary()
623 while self.stream.current.type == "pow":
624 next(self.stream) 625 right = self.parse_unary()
626 left = nodes.Pow(left, right, lineno=lineno)
627 lineno = self.stream.current.lineno
Call to
next()
should be wrapped in try-except
611 left = self.parse_pow()
612 while self.stream.current.type in ("mul", "div", "floordiv", "mod"):
613 cls = _math_nodes[self.stream.current.type]
614 next(self.stream) 615 right = self.parse_pow()
616 left = cls(left, right, lineno=lineno)
617 lineno = self.stream.current.lineno
Call to
next()
should be wrapped in try-except
600 lineno = self.stream.current.lineno
601 args = [self.parse_math2()]
602 while self.stream.current.type == "tilde":
603 next(self.stream) 604 args.append(self.parse_math2())
605 if len(args) == 1:
606 return args[0]
Call to
next()
should be wrapped in try-except
590 left = self.parse_concat()
591 while self.stream.current.type in ("add", "sub"):
592 cls = _math_nodes[self.stream.current.type]
593 next(self.stream) 594 right = self.parse_concat()
595 left = cls(left, right, lineno=lineno)
596 lineno = self.stream.current.lineno
Description
Calls to next()
should be inside try-except
block.
When the iterator is exhausted, StopIteration
exception is raised. When used inside a generator, this can cause unexpected behavior. If not handled, it will propagate out of the generator causing termination. PEP-479 has been accepted to fix this problem. It will modify the behavior of StopIteration
in generators.
Each call to next()
should be wrapped in a try-except
block to explicitly handle StopIteration
exceptions.
Bad practice
def get_team_structure(team):
for team in class:
students = iter(team)
# StopIteration exception will be raised if team doesn't have members.
leader = next(students)
members = [m for m in students]
yield leader, members
def print_teams(teams):
for leader, members in get_team_structure(teams):
print("Leader: "leader)
for member in members:
print(member)
Recommended:
def get_team_structure(team):
for team in class:
# StopIteration exception will be raised if team has no members.
students = iter(team)
# Added `try` block, to guard against the exception
try:
leader = next(students)
except StopIteration:
# team has no members, skip it
continue
members = [m for m in students]
yield leader, members
def print_teams(teams):
for leader, members in get_team_structure(teams):
print("Leader: "leader)
for member in members:
print(member)
...