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.

iterable_context.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. """
  2. Checks that primitive values are not used in an
  3. iterating/mapping context.
  4. """
  5. # pylint: disable=missing-docstring,invalid-name,too-few-public-methods,no-init,no-self-use,import-error,unused-argument,bad-mcs-method-argument,wrong-import-position,no-else-return
  6. from __future__ import print_function
  7. # primitives
  8. numbers = [1, 2, 3]
  9. for i in numbers:
  10. pass
  11. for i in iter(numbers):
  12. pass
  13. for i in "123":
  14. pass
  15. for i in u"123":
  16. pass
  17. for i in b"123":
  18. pass
  19. for i in bytearray(b"123"):
  20. pass
  21. for i in set(numbers):
  22. pass
  23. for i in frozenset(numbers):
  24. pass
  25. for i in dict(a=1, b=2):
  26. pass
  27. # comprehensions
  28. for i in [x for x in range(10)]:
  29. pass
  30. for i in {x for x in range(1, 100, 2)}:
  31. pass
  32. for i in {x: 10 - x for x in range(10)}:
  33. pass
  34. # generators
  35. def powers_of_two():
  36. k = 0
  37. while k < 10:
  38. yield 2 ** k
  39. k += 1
  40. for i in powers_of_two():
  41. pass
  42. for i in powers_of_two: # [not-an-iterable]
  43. pass
  44. # check for custom iterators
  45. class A(object):
  46. pass
  47. class B(object):
  48. def __iter__(self):
  49. return self
  50. def __next__(self):
  51. return 1
  52. def next(self):
  53. return 1
  54. class C(object):
  55. "old-style iterator"
  56. def __getitem__(self, k):
  57. if k > 10:
  58. raise IndexError
  59. return k + 1
  60. def __len__(self):
  61. return 10
  62. for i in C():
  63. print(i)
  64. def test(*args):
  65. print(args)
  66. test(*A()) # [not-an-iterable]
  67. test(*B())
  68. test(*B) # [not-an-iterable]
  69. for i in A(): # [not-an-iterable]
  70. pass
  71. for i in B():
  72. pass
  73. for i in B: # [not-an-iterable]
  74. pass
  75. for i in range: # [not-an-iterable]
  76. pass
  77. # check that primitive non-iterable types are caught
  78. for i in True: # [not-an-iterable]
  79. pass
  80. for i in None: # [not-an-iterable]
  81. pass
  82. for i in 8.5: # [not-an-iterable]
  83. pass
  84. for i in 10: # [not-an-iterable]
  85. pass
  86. # skip uninferable instances
  87. from some_missing_module import Iterable
  88. class MyClass(Iterable):
  89. pass
  90. m = MyClass()
  91. for i in m:
  92. print(i)
  93. # skip checks if statement is inside mixin/base/abstract class
  94. class ManagedAccessViewMixin(object):
  95. access_requirements = None
  96. def get_access_requirements(self):
  97. return self.access_requirements
  98. def dispatch(self, *_args, **_kwargs):
  99. klasses = self.get_access_requirements()
  100. # no error should be emitted here
  101. for requirement in klasses:
  102. print(requirement)
  103. class BaseType(object):
  104. valid_values = None
  105. def validate(self, value):
  106. if self.valid_values is None:
  107. return True
  108. else:
  109. # error should not be emitted here
  110. for v in self.valid_values:
  111. if value == v:
  112. return True
  113. return False
  114. class AbstractUrlMarkManager(object):
  115. def __init__(self):
  116. self._lineparser = None
  117. self._init_lineparser()
  118. # error should not be emitted here
  119. for line in self._lineparser:
  120. print(line)
  121. def _init_lineparser(self):
  122. raise NotImplementedError
  123. # class is not named as abstract
  124. # but still is deduceably abstract
  125. class UrlMarkManager(object):
  126. def __init__(self):
  127. self._lineparser = None
  128. self._init_lineparser()
  129. # error should not be emitted here
  130. for line in self._lineparser:
  131. print(line)
  132. def _init_lineparser(self):
  133. raise NotImplementedError
  134. class HasDynamicGetattr(object):
  135. def __init__(self):
  136. self._obj = []
  137. def __getattr__(self, attr):
  138. return getattr(self._obj, attr)
  139. for elem in HasDynamicGetattr():
  140. pass