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.

unsubscriptable_value.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Checks that value used in a subscript supports subscription
  3. (i.e. defines __getitem__ 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]
  11. "123"[0]
  12. u"123"[0]
  13. b"123"[0]
  14. bytearray(b"123")[0]
  15. dict(a=1, b=2)['a']
  16. (1, 2, 3)[0]
  17. # list/dict comprehensions are fine
  18. [x for x in range(10)][0]
  19. {x: 10 - x for x in range(10)}[0]
  20. # instances
  21. class NonSubscriptable(object):
  22. pass
  23. class Subscriptable(object):
  24. def __getitem__(self, key):
  25. return key + key
  26. NonSubscriptable()[0] # [unsubscriptable-object]
  27. NonSubscriptable[0] # [unsubscriptable-object]
  28. Subscriptable()[0]
  29. Subscriptable[0] # [unsubscriptable-object]
  30. # generators are not subscriptable
  31. def powers_of_two():
  32. k = 0
  33. while k < 10:
  34. yield 2 ** k
  35. k += 1
  36. powers_of_two()[0] # [unsubscriptable-object]
  37. powers_of_two[0] # [unsubscriptable-object]
  38. # check that primitive non subscriptable types are caught
  39. True[0] # [unsubscriptable-object]
  40. None[0] # [unsubscriptable-object]
  41. 8.5[0] # [unsubscriptable-object]
  42. 10[0] # [unsubscriptable-object]
  43. # sets are not subscriptable
  44. {x ** 2 for x in range(10)}[0] # [unsubscriptable-object]
  45. set(numbers)[0] # [unsubscriptable-object]
  46. frozenset(numbers)[0] # [unsubscriptable-object]
  47. # skip instances with unknown base classes
  48. from some_missing_module import LibSubscriptable
  49. class MaybeSubscriptable(LibSubscriptable):
  50. pass
  51. MaybeSubscriptable()[0]
  52. # subscriptable classes (through metaclasses)
  53. class MetaSubscriptable(type):
  54. def __getitem__(cls, key):
  55. return key + key
  56. class SubscriptableClass(six.with_metaclass(MetaSubscriptable, object)):
  57. pass
  58. SubscriptableClass[0]
  59. SubscriptableClass()[0] # [unsubscriptable-object]
  60. # functions are not subscriptable
  61. def test(*args, **kwargs):
  62. return args, kwargs
  63. test()[0]
  64. test[0] # [unsubscriptable-object]
  65. # deque
  66. from collections import deque
  67. deq = deque(maxlen=10)
  68. deq.append(42)
  69. deq[0]
  70. class AbstractClass(object):
  71. def __init__(self):
  72. self.ala = {i for i in range(10)}
  73. self.bala = [i for i in range(10)]
  74. self.portocala = None
  75. def test_unsubscriptable(self):
  76. self.bala[0]
  77. self.portocala[0]
  78. class ClassMixin(object):
  79. def __init__(self):
  80. self.ala = {i for i in range(10)}
  81. self.bala = [i for i in range(10)]
  82. self.portocala = None
  83. def test_unsubscriptable(self):
  84. self.bala[0]
  85. self.portocala[0]