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_assignment_operation.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Checks that value used in a subscript support assignments
  3. (i.e. defines __setitem__ 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. numbers[0] = 42
  11. bytearray(b"123")[0] = 42
  12. dict(a=1, b=2)['a'] = 42
  13. (1, 2, 3)[0] = 42 # [unsupported-assignment-operation]
  14. # list/dict comprehensions are fine
  15. [x for x in range(10)][0] = 42
  16. {x: 10 - x for x in range(10)}[0] = 42
  17. # instances
  18. class NonSubscriptable(object):
  19. pass
  20. class Subscriptable(object):
  21. def __setitem__(self, key, value):
  22. return key + value
  23. NonSubscriptable()[0] = 24 # [unsupported-assignment-operation]
  24. NonSubscriptable[0] = 24 # [unsupported-assignment-operation]
  25. Subscriptable()[0] = 24
  26. Subscriptable[0] = 24 # [unsupported-assignment-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. powers_of_two()[0] = 42 # [unsupported-assignment-operation]
  34. powers_of_two[0] = 42 # [unsupported-assignment-operation]
  35. # check that primitive non subscriptable types are caught
  36. True[0] = 24 # [unsupported-assignment-operation]
  37. None[0] = 42 # [unsupported-assignment-operation]
  38. 8.5[0] = 24 # [unsupported-assignment-operation]
  39. 10[0] = 24 # [unsupported-assignment-operation]
  40. # sets are not subscriptable
  41. {x ** 2 for x in range(10)}[0] = 24 # [unsupported-assignment-operation]
  42. set(numbers)[0] = 24 # [unsupported-assignment-operation]
  43. frozenset(numbers)[0] = 42 # [unsupported-assignment-operation]
  44. # skip instances with unknown base classes
  45. from some_missing_module import LibSubscriptable
  46. class MaybeSubscriptable(LibSubscriptable):
  47. pass
  48. MaybeSubscriptable()[0] = 42
  49. # subscriptable classes (through metaclasses)
  50. class MetaSubscriptable(type):
  51. def __setitem__(cls, key, value):
  52. return key + value
  53. class SubscriptableClass(six.with_metaclass(MetaSubscriptable, object)):
  54. pass
  55. SubscriptableClass[0] = 24
  56. SubscriptableClass()[0] = 24 # [unsupported-assignment-operation]
  57. # functions are not subscriptable
  58. def test(*args, **kwargs):
  59. return args, kwargs
  60. test()[0] = 24 # [unsupported-assignment-operation]
  61. test[0] = 24 # [unsupported-assignment-operation]
  62. # deque
  63. from collections import deque
  64. deq = deque(maxlen=10)
  65. deq.append(42)
  66. deq[0] = 42
  67. # tuples assignment
  68. values = [1, 2, 3, 4]
  69. (values[0], values[1]) = 3, 4
  70. (values[0], SubscriptableClass()[0]) = 42, 42 # [unsupported-assignment-operation]