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