-
Notifications
You must be signed in to change notification settings - Fork 179
Check for duplicate dictionary keys #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
""" | ||
Tests for dict duplicate keys Pyflakes behavior. | ||
""" | ||
|
||
from sys import version_info | ||
|
||
from pyflakes import messages as m | ||
from pyflakes.test.harness import TestCase, skipIf | ||
|
||
|
||
class Test(TestCase): | ||
|
||
def test_duplicate_keys(self): | ||
self.flakes( | ||
"{'yes': 1, 'yes': 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
@skipIf(version_info < (3,), | ||
"bytes and strings with same 'value' are not equal in python3") | ||
@skipIf(version_info[0:2] == (3, 2), | ||
"python3.2 does not allow u"" literal string definition") | ||
def test_duplicate_keys_bytes_vs_unicode_py3(self): | ||
self.flakes("{b'a': 1, u'a': 2}") | ||
|
||
@skipIf(version_info < (3,), | ||
"bytes and strings with same 'value' are not equal in python3") | ||
@skipIf(version_info[0:2] == (3, 2), | ||
"python3.2 does not allow u"" literal string definition") | ||
def test_duplicate_values_bytes_vs_unicode_py3(self): | ||
self.flakes( | ||
"{1: b'a', 1: u'a'}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
@skipIf(version_info >= (3,), | ||
"bytes and strings with same 'value' are equal in python2") | ||
def test_duplicate_keys_bytes_vs_unicode_py2(self): | ||
self.flakes( | ||
"{b'a': 1, u'a': 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
@skipIf(version_info >= (3,), | ||
"bytes and strings with same 'value' are equal in python2") | ||
def test_duplicate_values_bytes_vs_unicode_py2(self): | ||
self.flakes("{1: b'a', 1: u'a'}") | ||
|
||
def test_multiple_duplicate_keys(self): | ||
self.flakes( | ||
"{'yes': 1, 'yes': 2, 'no': 2, 'no': 3}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_in_function(self): | ||
self.flakes( | ||
''' | ||
def f(thing): | ||
pass | ||
f({'yes': 1, 'yes': 2}) | ||
''', | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_in_lambda(self): | ||
self.flakes( | ||
"lambda x: {(0,1): 1, (0,1): 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_tuples(self): | ||
self.flakes( | ||
"{(0,1): 1, (0,1): 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_tuples_int_and_float(self): | ||
self.flakes( | ||
"{(0,1): 1, (0,1.0): 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_ints(self): | ||
self.flakes( | ||
"{1: 1, 1: 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_bools(self): | ||
self.flakes( | ||
"{True: 1, True: 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_bools_false(self): | ||
# Needed to ensure 2.x correctly coerces these from variables | ||
self.flakes( | ||
"{False: 1, False: 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_keys_none(self): | ||
self.flakes( | ||
"{None: 1, None: 2}", | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_variable_keys(self): | ||
self.flakes( | ||
''' | ||
a = 1 | ||
{a: 1, a: 2} | ||
''', | ||
m.MultiValueRepeatedKeyVariable, | ||
m.MultiValueRepeatedKeyVariable, | ||
) | ||
|
||
def test_duplicate_variable_values(self): | ||
self.flakes( | ||
''' | ||
a = 1 | ||
b = 2 | ||
{1: a, 1: b} | ||
''', | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_variable_values_same_value(self): | ||
# Current behaviour is not to look up variable values. This is to | ||
# confirm that. | ||
self.flakes( | ||
''' | ||
a = 1 | ||
b = 1 | ||
{1: a, 1: b} | ||
''', | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_duplicate_key_float_and_int(self): | ||
""" | ||
These do look like different values, but when it comes to their use as | ||
keys, they compare as equal and so are actually duplicates. | ||
The literal dict {1: 1, 1.0: 1} actually becomes {1.0: 1}. | ||
""" | ||
self.flakes( | ||
''' | ||
{1: 1, 1.0: 2} | ||
''', | ||
m.MultiValueRepeatedKeyLiteral, | ||
m.MultiValueRepeatedKeyLiteral, | ||
) | ||
|
||
def test_no_duplicate_key_error_same_value(self): | ||
self.flakes(''' | ||
{'yes': 1, 'yes': 1} | ||
''') | ||
|
||
def test_no_duplicate_key_errors(self): | ||
self.flakes(''' | ||
{'yes': 1, 'no': 2} | ||
''') | ||
|
||
def test_no_duplicate_keys_tuples_same_first_element(self): | ||
self.flakes("{(0,1): 1, (0,2): 1}") | ||
|
||
def test_no_duplicate_key_errors_func_call(self): | ||
self.flakes(''' | ||
def test(thing): | ||
pass | ||
test({True: 1, None: 2, False: 1}) | ||
''') | ||
|
||
def test_no_duplicate_key_errors_bool_or_none(self): | ||
self.flakes("{True: 1, None: 2, False: 1}") | ||
|
||
def test_no_duplicate_key_errors_ints(self): | ||
self.flakes(''' | ||
{1: 1, 2: 1} | ||
''') | ||
|
||
def test_no_duplicate_key_errors_vars(self): | ||
self.flakes(''' | ||
test = 'yes' | ||
rest = 'yes' | ||
{test: 1, rest: 2} | ||
''') | ||
|
||
def test_no_duplicate_key_errors_tuples(self): | ||
self.flakes(''' | ||
{(0,1): 1, (0,2): 1} | ||
''') | ||
|
||
def test_no_duplicate_key_errors_instance_attributes(self): | ||
self.flakes(''' | ||
class Test(): | ||
pass | ||
f = Test() | ||
f.a = 1 | ||
{f.a: 1, f.a: 1} | ||
''') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useful to special case
{}
by bailing out before building the following array?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a very quick and dirty test (timing 10000 runs of a func which does result = [str(key) for key in item.keys()] with no gating, a test for == {} and a test for len(thing) == 0), it seems like the list comprehension finishes just as fast as the gated versions (possibly faster actually, though that may be an artefact of the way the test was constructed).
Probably not worth it given that it gives no obvious gain in efficiency anyway.