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.

unsupported_delete_operation.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Checks that value used in a subscript support deletion
  3. (i.e. defines __delitem__ method).
  4. """
  5. # pylint: disable=missing-docstring,pointless-statement,expression-not-assigned,wrong-import-position
  6. # pylint: disable=too-few-public-methods,import-error,invalid-name,wrong-import-order
  7. import six
  8. # primitives
  9. numbers = [1, 2, 3]
  10. del numbers[0]
  11. del bytearray(b"123")[0]
  12. del dict(a=1, b=2)['a']
  13. del (1, 2, 3)[0] # [unsupported-delete-operation]
  14. # list/dict comprehensions are fine
  15. del [x for x in range(10)][0]
  16. del {x: 10 - x for x in range(10)}[0]
  17. # instances
  18. class NonSubscriptable(object):
  19. pass
  20. class Subscriptable(object):
  21. def __delitem__(self, key):
  22. pass
  23. del NonSubscriptable()[0] # [unsupported-delete-operation]
  24. del NonSubscriptable[0] # [unsupported-delete-operation]
  25. del Subscriptable()[0]
  26. del Subscriptable[0] # [unsupported-delete-operation]
  27. # generators are not subscriptable
  28. def powers_of_two():
  29. k = 0
  30. while k < 10:
  31. yield 2 ** k
  32. k += 1
  33. del powers_of_two()[0] # [unsupported-delete-operation]
  34. del powers_of_two[0] # [unsupported-delete-operation]
  35. # check that primitive non subscriptable types are caught
  36. del True[0] # [unsupported-delete-operation]
  37. del None[0] # [unsupported-delete-operation]
  38. del 8.5[0] # [unsupported-delete-operation]
  39. del 10[0] # [unsupported-delete-operation]
  40. # sets are not subscriptable
  41. del {x ** 2 for x in range(10)}[0] # [unsupported-delete-operation]
  42. del set(numbers)[0] # [unsupported-delete-operation]
  43. del frozenset(numbers)[0] # [unsupported-delete-operation]
  44. # skip instances with unknown base classes
  45. from some_missing_module import LibSubscriptable
  46. class MaybeSubscriptable(LibSubscriptable):
  47. pass
  48. del MaybeSubscriptable()[0]
  49. # subscriptable classes (through metaclasses)
  50. class MetaSubscriptable(type):
  51. def __delitem__(cls, key):
  52. pass
  53. class SubscriptableClass(six.with_metaclass(MetaSubscriptable, object)):
  54. pass
  55. del SubscriptableClass[0]
  56. del SubscriptableClass()[0] # [unsupported-delete-operation]
  57. # functions are not subscriptable
  58. def test(*args, **kwargs):
  59. return args, kwargs
  60. del test()[0] # [unsupported-delete-operation]
  61. del test[0] # [unsupported-delete-operation]
  62. # deque
  63. from collections import deque
  64. deq = deque(maxlen=10)
  65. deq.append(42)
  66. del deq[0]
  67. # tuples assignment
  68. values = [1, 2, 3, 4]
  69. del (values[0], values[1])
  70. del (values[0], SubscriptableClass()[0]) # [unsupported-delete-operation]