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.

invalid_sequence_index.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. """Errors for invalid sequence indices"""
  2. # pylint: disable=too-few-public-methods, no-self-use, import-error, missing-docstring
  3. import six
  4. from unknown import Unknown
  5. TESTLIST = [1, 2, 3]
  6. TESTTUPLE = (1, 2, 3)
  7. TESTSTR = '123'
  8. # getitem tests with bad indices
  9. def function1():
  10. """list index is a function"""
  11. return TESTLIST[id] # [invalid-sequence-index]
  12. def function2():
  13. """list index is None"""
  14. return TESTLIST[None] # [invalid-sequence-index]
  15. def function3():
  16. """list index is a float expression"""
  17. return TESTLIST[float(0)] # [invalid-sequence-index]
  18. def function4():
  19. """list index is a str constant"""
  20. return TESTLIST['0'] # [invalid-sequence-index]
  21. def function5():
  22. """list index does not implement __index__"""
  23. class NonIndexType(object):
  24. """Class without __index__ method"""
  25. pass
  26. return TESTLIST[NonIndexType()] # [invalid-sequence-index]
  27. def function6():
  28. """Tuple index is None"""
  29. return TESTTUPLE[None] # [invalid-sequence-index]
  30. def function7():
  31. """String index is None"""
  32. return TESTSTR[None] # [invalid-sequence-index]
  33. def function8():
  34. """Index of subclass of tuple is None"""
  35. class TupleTest(tuple):
  36. """Subclass of tuple"""
  37. pass
  38. return TupleTest()[None] # [invalid-sequence-index]
  39. # getitem tests with good indices
  40. def function9():
  41. """list index is an int constant"""
  42. return TESTLIST[0] # no error
  43. def function10():
  44. """list index is a integer expression"""
  45. return TESTLIST[int(0.0)] # no error
  46. def function11():
  47. """list index is a slice"""
  48. return TESTLIST[slice(1, 2, 3)] # no error
  49. def function12():
  50. """list index implements __index__"""
  51. class IndexType(object):
  52. """Class with __index__ method"""
  53. def __index__(self):
  54. """Allow objects of this class to be used as slice indices"""
  55. return 0
  56. return TESTLIST[IndexType()] # no error
  57. def function13():
  58. """list index implements __index__ in a superclass"""
  59. class IndexType(object):
  60. """Class with __index__ method"""
  61. def __index__(self):
  62. """Allow objects of this class to be used as slice indices"""
  63. return 0
  64. class IndexSubType(IndexType):
  65. """Class with __index__ in parent"""
  66. pass
  67. return TESTLIST[IndexSubType()] # no error
  68. def function14():
  69. """Tuple index is an int constant"""
  70. return TESTTUPLE[0]
  71. def function15():
  72. """String index is an int constant"""
  73. return TESTSTR[0]
  74. def function16():
  75. """Index of subclass of tuple is an int constant"""
  76. class TupleTest(tuple):
  77. """Subclass of tuple"""
  78. pass
  79. return TupleTest()[0] # no error
  80. def function17():
  81. """Index of subclass of tuple with custom __getitem__ is None"""
  82. class TupleTest(tuple):
  83. """Subclass of tuple with custom __getitem__"""
  84. def __getitem__(self, index):
  85. """Allow non-integer indices"""
  86. return 0
  87. return TupleTest()[None] # no error
  88. def function18():
  89. """Index of subclass of tuple with __getitem__ in superclass is None"""
  90. class TupleTest(tuple):
  91. """Subclass of tuple with custom __getitem__"""
  92. def __getitem__(self, index):
  93. """Allow non-integer indices"""
  94. return 0
  95. class SubTupleTest(TupleTest):
  96. """Subclass of a subclass of tuple"""
  97. pass
  98. return SubTupleTest()[None] # no error
  99. # Test with set and delete statements
  100. def function19():
  101. """Set with None and integer indices"""
  102. TESTLIST[None] = 0 # [invalid-sequence-index]
  103. TESTLIST[0] = 0 # no error
  104. def function20():
  105. """Delete with None and integer indicies"""
  106. del TESTLIST[None] # [invalid-sequence-index]
  107. del TESTLIST[0] # no error
  108. def function21():
  109. """Set and delete on a subclass of list"""
  110. class ListTest(list):
  111. """Inherit all list get/set/del handlers"""
  112. pass
  113. test = ListTest()
  114. # Set and delete with invalid indices
  115. test[None] = 0 # [invalid-sequence-index]
  116. del test[None] # [invalid-sequence-index]
  117. # Set and delete with valid indices
  118. test[0] = 0 # no error
  119. del test[0] # no error
  120. def function22():
  121. """Get, set, and delete on a subclass of list that overrides __setitem__"""
  122. class ListTest(list):
  123. """Override setitem but not get or del"""
  124. def __setitem__(self, key, value):
  125. pass
  126. test = ListTest()
  127. # failure on the getitem with None
  128. test[None][0] = 0 # [invalid-sequence-index]
  129. # failure on the getitem with None
  130. del test[None] # [invalid-sequence-index]
  131. test[0][0] = 0 # getitem with int and setitem with int, no error
  132. test[None] = 0 # setitem overridden, no error
  133. test[0] = 0 # setitem with int, no error
  134. del test[0] # delitem with int, no error
  135. def function23():
  136. """Get, set, and delete on a subclass of list that overrides __delitem__"""
  137. class ListTest(list):
  138. """Override delitem but not get or set"""
  139. def __delitem__(self, key):
  140. pass
  141. test = ListTest()
  142. # failure on the getitem with None
  143. test[None][0] = 0 # [invalid-sequence-index]
  144. # setitem with invalid index
  145. test[None] = 0 # [invalid-sequence-index]
  146. test[0][0] = 0 # getitem with int and setitem with int, no error
  147. test[0] = 0 # setitem with int, no error
  148. del test[None] # delitem overridden, no error
  149. del test[0] # delitem with int, no error
  150. def function24():
  151. """Get, set, and delete on a subclass of list that overrides __getitem__"""
  152. class ListTest(list):
  153. """Override gelitem but not del or set"""
  154. def __getitem__(self, key):
  155. pass
  156. test = ListTest()
  157. # setitem with invalid index
  158. test[None] = 0 # [invalid-sequence-index]
  159. # delitem with invalid index
  160. del test[None] # [invalid-sequence-index]
  161. test[None][0] = 0 # getitem overridden, no error
  162. test[0][0] = 0 # getitem with int and setitem with int, no error
  163. test[0] = 0 # setitem with int, no error
  164. del test[0] # delitem with int, no error
  165. # Teest ExtSlice usage
  166. def function25():
  167. """Extended slice used with a list"""
  168. return TESTLIST[..., 0] # [invalid-sequence-index]
  169. def function26():
  170. """Extended slice used with an object that implements __getitem__"""
  171. class ExtSliceTest(object):
  172. """Permit extslice syntax by implementing __getitem__"""
  173. def __getitem__(self, index):
  174. return 0
  175. return ExtSliceTest()[..., 0] # no error
  176. def function27():
  177. """Don't warn in the case where the indexed object has unknown base classes."""
  178. class UnknownBase(Unknown):
  179. pass
  180. slices = UnknownBase["aaaa"] + UnknownBase()[object]
  181. ext_slices = UnknownBase[..., 0] + UnknownBase()[..., 0]
  182. return slices, ext_slices
  183. def function28():
  184. """Don't emit for classes with the right implementation."""
  185. class Meta(type):
  186. def __getitem__(cls, arg):
  187. return 24
  188. @six.add_metaclass(Meta)
  189. class Works(object):
  190. pass
  191. @six.add_metaclass(Meta)
  192. class Error(list):
  193. pass
  194. return Works['hello'] + Error['hello']