Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stop_iteration_inside_generator.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. Test that no StopIteration is raised inside a generator
  3. """
  4. # pylint: disable=missing-docstring,invalid-name,import-error
  5. import asyncio
  6. class RebornStopIteration(StopIteration):
  7. """
  8. A class inheriting from StopIteration exception
  9. """
  10. # This one is ok
  11. def gen_ok():
  12. yield 1
  13. yield 2
  14. yield 3
  15. return
  16. # pylint should warn about this one
  17. # because of a direct raising of StopIteration inside generator
  18. def gen_stopiter():
  19. yield 1
  20. yield 2
  21. yield 3
  22. raise StopIteration # [stop-iteration-return]
  23. # pylint should warn about this one
  24. # because of a direct raising of an exception inheriting from StopIteration inside generator
  25. def gen_stopiterchild():
  26. yield 1
  27. yield 2
  28. yield 3
  29. raise RebornStopIteration # [stop-iteration-return]
  30. # pylint should warn here
  31. # because of the possibility that next raises a StopIteration exception
  32. def gen_next_raises_stopiter():
  33. g = gen_ok()
  34. while True:
  35. yield next(g) # [stop-iteration-return]
  36. # This one is the same as gen_next_raises_stopiter
  37. # but is ok because the next function is inside
  38. # a try/except block handling StopIteration
  39. def gen_next_inside_try_except():
  40. g = gen_ok()
  41. while True:
  42. try:
  43. yield next(g)
  44. except StopIteration:
  45. return
  46. # This one is the same as gen_next_inside_try_except
  47. # but is not ok because the next function is inside
  48. # a try/except block that don't handle StopIteration
  49. def gen_next_inside_wrong_try_except():
  50. g = gen_ok()
  51. while True:
  52. try:
  53. yield next(g) # [stop-iteration-return]
  54. except ValueError:
  55. return
  56. # This one is the same as gen_next_inside_try_except
  57. # but is not ok because the next function is inside
  58. # a try/except block that handle StopIteration but reraise it
  59. def gen_next_inside_wrong_try_except2():
  60. g = gen_ok()
  61. while True:
  62. try:
  63. yield next(g)
  64. except StopIteration:
  65. raise StopIteration # [stop-iteration-return]
  66. # Those two last are ok
  67. def gen_in_for():
  68. for el in gen_ok():
  69. yield el
  70. def gen_yield_from():
  71. yield from gen_ok()
  72. def gen_dont_crash_on_no_exception():
  73. g = gen_ok()
  74. while True:
  75. try:
  76. yield next(g) # [stop-iteration-return]
  77. except ValueError:
  78. raise
  79. def gen_dont_crash_on_uninferable():
  80. # https://github.com/PyCQA/pylint/issues/1779
  81. yield from iter()
  82. raise asyncio.TimeoutError()