test_dict.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """
  2. Tests for dict duplicate keys Pyflakes behavior.
  3. """
  4. from pyflakes import messages as m
  5. from pyflakes.test.harness import TestCase
  6. class Test(TestCase):
  7. def test_duplicate_keys(self):
  8. self.flakes(
  9. "{'yes': 1, 'yes': 2}",
  10. m.MultiValueRepeatedKeyLiteral,
  11. m.MultiValueRepeatedKeyLiteral,
  12. )
  13. def test_duplicate_keys_bytes_vs_unicode_py3(self):
  14. self.flakes("{b'a': 1, u'a': 2}")
  15. def test_duplicate_values_bytes_vs_unicode_py3(self):
  16. self.flakes(
  17. "{1: b'a', 1: u'a'}",
  18. m.MultiValueRepeatedKeyLiteral,
  19. m.MultiValueRepeatedKeyLiteral,
  20. )
  21. def test_multiple_duplicate_keys(self):
  22. self.flakes(
  23. "{'yes': 1, 'yes': 2, 'no': 2, 'no': 3}",
  24. m.MultiValueRepeatedKeyLiteral,
  25. m.MultiValueRepeatedKeyLiteral,
  26. m.MultiValueRepeatedKeyLiteral,
  27. m.MultiValueRepeatedKeyLiteral,
  28. )
  29. def test_duplicate_keys_in_function(self):
  30. self.flakes(
  31. '''
  32. def f(thing):
  33. pass
  34. f({'yes': 1, 'yes': 2})
  35. ''',
  36. m.MultiValueRepeatedKeyLiteral,
  37. m.MultiValueRepeatedKeyLiteral,
  38. )
  39. def test_duplicate_keys_in_lambda(self):
  40. self.flakes(
  41. "lambda x: {(0,1): 1, (0,1): 2}",
  42. m.MultiValueRepeatedKeyLiteral,
  43. m.MultiValueRepeatedKeyLiteral,
  44. )
  45. def test_duplicate_keys_tuples(self):
  46. self.flakes(
  47. "{(0,1): 1, (0,1): 2}",
  48. m.MultiValueRepeatedKeyLiteral,
  49. m.MultiValueRepeatedKeyLiteral,
  50. )
  51. def test_duplicate_keys_tuples_int_and_float(self):
  52. self.flakes(
  53. "{(0,1): 1, (0,1.0): 2}",
  54. m.MultiValueRepeatedKeyLiteral,
  55. m.MultiValueRepeatedKeyLiteral,
  56. )
  57. def test_duplicate_keys_ints(self):
  58. self.flakes(
  59. "{1: 1, 1: 2}",
  60. m.MultiValueRepeatedKeyLiteral,
  61. m.MultiValueRepeatedKeyLiteral,
  62. )
  63. def test_duplicate_keys_bools(self):
  64. self.flakes(
  65. "{True: 1, True: 2}",
  66. m.MultiValueRepeatedKeyLiteral,
  67. m.MultiValueRepeatedKeyLiteral,
  68. )
  69. def test_duplicate_keys_bools_false(self):
  70. # Needed to ensure 2.x correctly coerces these from variables
  71. self.flakes(
  72. "{False: 1, False: 2}",
  73. m.MultiValueRepeatedKeyLiteral,
  74. m.MultiValueRepeatedKeyLiteral,
  75. )
  76. def test_duplicate_keys_none(self):
  77. self.flakes(
  78. "{None: 1, None: 2}",
  79. m.MultiValueRepeatedKeyLiteral,
  80. m.MultiValueRepeatedKeyLiteral,
  81. )
  82. def test_duplicate_variable_keys(self):
  83. self.flakes(
  84. '''
  85. a = 1
  86. {a: 1, a: 2}
  87. ''',
  88. m.MultiValueRepeatedKeyVariable,
  89. m.MultiValueRepeatedKeyVariable,
  90. )
  91. def test_duplicate_variable_values(self):
  92. self.flakes(
  93. '''
  94. a = 1
  95. b = 2
  96. {1: a, 1: b}
  97. ''',
  98. m.MultiValueRepeatedKeyLiteral,
  99. m.MultiValueRepeatedKeyLiteral,
  100. )
  101. def test_duplicate_variable_values_same_value(self):
  102. # Current behaviour is not to look up variable values. This is to
  103. # confirm that.
  104. self.flakes(
  105. '''
  106. a = 1
  107. b = 1
  108. {1: a, 1: b}
  109. ''',
  110. m.MultiValueRepeatedKeyLiteral,
  111. m.MultiValueRepeatedKeyLiteral,
  112. )
  113. def test_duplicate_key_float_and_int(self):
  114. """
  115. These do look like different values, but when it comes to their use as
  116. keys, they compare as equal and so are actually duplicates.
  117. The literal dict {1: 1, 1.0: 1} actually becomes {1.0: 1}.
  118. """
  119. self.flakes(
  120. '''
  121. {1: 1, 1.0: 2}
  122. ''',
  123. m.MultiValueRepeatedKeyLiteral,
  124. m.MultiValueRepeatedKeyLiteral,
  125. )
  126. def test_no_duplicate_key_error_same_value(self):
  127. self.flakes('''
  128. {'yes': 1, 'yes': 1}
  129. ''')
  130. def test_no_duplicate_key_errors(self):
  131. self.flakes('''
  132. {'yes': 1, 'no': 2}
  133. ''')
  134. def test_no_duplicate_keys_tuples_same_first_element(self):
  135. self.flakes("{(0,1): 1, (0,2): 1}")
  136. def test_no_duplicate_key_errors_func_call(self):
  137. self.flakes('''
  138. def test(thing):
  139. pass
  140. test({True: 1, None: 2, False: 1})
  141. ''')
  142. def test_no_duplicate_key_errors_bool_or_none(self):
  143. self.flakes("{True: 1, None: 2, False: 1}")
  144. def test_no_duplicate_key_errors_ints(self):
  145. self.flakes('''
  146. {1: 1, 2: 1}
  147. ''')
  148. def test_no_duplicate_key_errors_vars(self):
  149. self.flakes('''
  150. test = 'yes'
  151. rest = 'yes'
  152. {test: 1, rest: 2}
  153. ''')
  154. def test_no_duplicate_key_errors_tuples(self):
  155. self.flakes('''
  156. {(0,1): 1, (0,2): 1}
  157. ''')
  158. def test_no_duplicate_key_errors_instance_attributes(self):
  159. self.flakes('''
  160. class Test():
  161. pass
  162. f = Test()
  163. f.a = 1
  164. {f.a: 1, f.a: 1}
  165. ''')