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_slice_index.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Errors for invalid slice indices"""
  2. # pylint: disable=too-few-public-methods, no-self-use
  3. TESTLIST = [1, 2, 3]
  4. # Invalid indices
  5. def function1():
  6. """functions used as indices"""
  7. return TESTLIST[id:id:] # [invalid-slice-index,invalid-slice-index]
  8. def function2():
  9. """strings used as indices"""
  10. return TESTLIST['0':'1':] # [invalid-slice-index,invalid-slice-index]
  11. def function3():
  12. """class without __index__ used as index"""
  13. class NoIndexTest(object):
  14. """Class with no __index__ method"""
  15. pass
  16. return TESTLIST[NoIndexTest()::] # [invalid-slice-index]
  17. # Valid indices
  18. def function4():
  19. """integers used as indices"""
  20. return TESTLIST[0:0:0] # no error
  21. def function5():
  22. """None used as indices"""
  23. return TESTLIST[None:None:None] # no error
  24. def function6():
  25. """class with __index__ used as index"""
  26. class IndexTest(object):
  27. """Class with __index__ method"""
  28. def __index__(self):
  29. """Allow objects of this class to be used as slice indices"""
  30. return 0
  31. return TESTLIST[IndexTest():None:None] # no error
  32. def function7():
  33. """class with __index__ in superclass used as index"""
  34. class IndexType(object):
  35. """Class with __index__ method"""
  36. def __index__(self):
  37. """Allow objects of this class to be used as slice indices"""
  38. return 0
  39. class IndexSubType(IndexType):
  40. """Class with __index__ in parent"""
  41. pass
  42. return TESTLIST[IndexSubType():None:None] # no error
  43. def function8():
  44. """slice object used as index"""
  45. return TESTLIST[slice(1, 2, 3)] # no error