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.

cellvar_escaping_loop.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # pylint: disable=print-statement
  2. """Tests for loopvar-in-closure."""
  3. from __future__ import print_function
  4. def good_case():
  5. """No problems here."""
  6. lst = []
  7. for i in range(10):
  8. lst.append(i)
  9. def good_case2():
  10. """No problems here."""
  11. return [i for i in range(10)]
  12. def good_case3():
  13. """No problems here."""
  14. lst = []
  15. for i in range(10): # [unused-variable]
  16. lst.append(lambda i=i: i)
  17. def good_case4():
  18. """No problems here."""
  19. lst = []
  20. for i in range(10):
  21. print(i)
  22. lst.append(lambda i: i)
  23. def good_case5():
  24. """No problems here."""
  25. return (i for i in range(10))
  26. def good_case6():
  27. """Accept use of the variable after the loop.
  28. There's already a warning about possibly undefined loop variables, and
  29. the value will not change any more."""
  30. for i in range(10):
  31. print(i)
  32. return lambda: i # [undefined-loop-variable]
  33. def good_case7():
  34. """Accept use of the variable inside return."""
  35. for i in range(10):
  36. if i == 8:
  37. return lambda: i
  38. return lambda: -1
  39. def good_case8():
  40. """Lambda defined and called in loop."""
  41. for i in range(10):
  42. print((lambda x: i + x)(1))
  43. def good_case9():
  44. """Another eager binding of the cell variable."""
  45. funs = []
  46. for i in range(10):
  47. def func(bound_i=i):
  48. """Ignore."""
  49. return bound_i
  50. funs.append(func)
  51. return funs
  52. def bad_case():
  53. """Closing over a loop variable."""
  54. lst = []
  55. for i in range(10):
  56. print(i)
  57. lst.append(lambda: i) # [cell-var-from-loop]
  58. def bad_case2():
  59. """Closing over a loop variable."""
  60. return [lambda: i for i in range(10)] # [cell-var-from-loop]
  61. def bad_case3():
  62. """Closing over variable defined in loop."""
  63. lst = []
  64. for i in range(10):
  65. j = i * i
  66. lst.append(lambda: j) # [cell-var-from-loop]
  67. return lst
  68. def bad_case4():
  69. """Closing over variable defined in loop."""
  70. lst = []
  71. for i in range(10):
  72. def nested():
  73. """Nested function."""
  74. return i**2 # [cell-var-from-loop]
  75. lst.append(nested)
  76. return lst
  77. def bad_case5():
  78. """Problematic case.
  79. If this function is used as
  80. >>> [x() for x in bad_case5()]
  81. it behaves 'as expected', i.e. the result is range(10).
  82. If it's used with
  83. >>> lst = list(bad_case5())
  84. >>> [x() for x in lst]
  85. the result is [9] * 10 again.
  86. """
  87. return (lambda: i for i in range(10)) # [cell-var-from-loop]
  88. def bad_case6():
  89. """Closing over variable defined in loop."""
  90. lst = []
  91. for i, j in zip(range(10), range(10, 20)):
  92. print(j)
  93. lst.append(lambda: i) # [cell-var-from-loop]
  94. return lst