-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Update test_dict_version.py from Cpython v3.11.2 #4809
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
Closed
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
""" | ||
Test implementation of the PEP 509: dictionary versioning. | ||
""" | ||
import unittest | ||
from test.support import import_helper | ||
|
||
# PEP 509 is implemented in CPython but other Python implementations | ||
# don't require to implement it | ||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
|
||
class DictVersionTests(unittest.TestCase): | ||
type2test = dict | ||
|
||
def setUp(self): | ||
self.seen_versions = set() | ||
self.dict = None | ||
|
||
def check_version_unique(self, mydict): | ||
version = _testcapi.dict_get_version(mydict) | ||
self.assertNotIn(version, self.seen_versions) | ||
self.seen_versions.add(version) | ||
|
||
def check_version_changed(self, mydict, method, *args, **kw): | ||
result = method(*args, **kw) | ||
self.check_version_unique(mydict) | ||
return result | ||
|
||
def check_version_dont_change(self, mydict, method, *args, **kw): | ||
version1 = _testcapi.dict_get_version(mydict) | ||
self.seen_versions.add(version1) | ||
|
||
result = method(*args, **kw) | ||
|
||
version2 = _testcapi.dict_get_version(mydict) | ||
self.assertEqual(version2, version1, "version changed") | ||
|
||
return result | ||
|
||
def new_dict(self, *args, **kw): | ||
d = self.type2test(*args, **kw) | ||
self.check_version_unique(d) | ||
return d | ||
|
||
def test_constructor(self): | ||
# new empty dictionaries must all have an unique version | ||
empty1 = self.new_dict() | ||
empty2 = self.new_dict() | ||
empty3 = self.new_dict() | ||
|
||
# non-empty dictionaries must also have an unique version | ||
nonempty1 = self.new_dict(x='x') | ||
nonempty2 = self.new_dict(x='x', y='y') | ||
|
||
def test_copy(self): | ||
d = self.new_dict(a=1, b=2) | ||
|
||
d2 = self.check_version_dont_change(d, d.copy) | ||
|
||
# dict.copy() must create a dictionary with a new unique version | ||
self.check_version_unique(d2) | ||
|
||
def test_setitem(self): | ||
d = self.new_dict() | ||
|
||
# creating new keys must change the version | ||
self.check_version_changed(d, d.__setitem__, 'x', 'x') | ||
self.check_version_changed(d, d.__setitem__, 'y', 'y') | ||
|
||
# changing values must change the version | ||
self.check_version_changed(d, d.__setitem__, 'x', 1) | ||
self.check_version_changed(d, d.__setitem__, 'y', 2) | ||
|
||
def test_setitem_same_value(self): | ||
value = object() | ||
d = self.new_dict() | ||
|
||
# setting a key must change the version | ||
self.check_version_changed(d, d.__setitem__, 'key', value) | ||
|
||
# setting a key to the same value with dict.__setitem__ | ||
# must change the version | ||
self.check_version_dont_change(d, d.__setitem__, 'key', value) | ||
|
||
# setting a key to the same value with dict.update | ||
# must change the version | ||
self.check_version_dont_change(d, d.update, key=value) | ||
|
||
d2 = self.new_dict(key=value) | ||
self.check_version_dont_change(d, d.update, d2) | ||
|
||
def test_setitem_equal(self): | ||
class AlwaysEqual: | ||
def __eq__(self, other): | ||
return True | ||
|
||
value1 = AlwaysEqual() | ||
value2 = AlwaysEqual() | ||
self.assertTrue(value1 == value2) | ||
self.assertFalse(value1 != value2) | ||
self.assertIsNot(value1, value2) | ||
|
||
d = self.new_dict() | ||
self.check_version_changed(d, d.__setitem__, 'key', value1) | ||
self.assertIs(d['key'], value1) | ||
|
||
# setting a key to a value equal to the current value | ||
# with dict.__setitem__() must change the version | ||
self.check_version_changed(d, d.__setitem__, 'key', value2) | ||
self.assertIs(d['key'], value2) | ||
|
||
# setting a key to a value equal to the current value | ||
# with dict.update() must change the version | ||
self.check_version_changed(d, d.update, key=value1) | ||
self.assertIs(d['key'], value1) | ||
|
||
d2 = self.new_dict(key=value2) | ||
self.check_version_changed(d, d.update, d2) | ||
self.assertIs(d['key'], value2) | ||
|
||
def test_setdefault(self): | ||
d = self.new_dict() | ||
|
||
# setting a key with dict.setdefault() must change the version | ||
self.check_version_changed(d, d.setdefault, 'key', 'value1') | ||
|
||
# don't change the version if the key already exists | ||
self.check_version_dont_change(d, d.setdefault, 'key', 'value2') | ||
|
||
def test_delitem(self): | ||
d = self.new_dict(key='value') | ||
|
||
# deleting a key with dict.__delitem__() must change the version | ||
self.check_version_changed(d, d.__delitem__, 'key') | ||
|
||
# don't change the version if the key doesn't exist | ||
self.check_version_dont_change(d, self.assertRaises, KeyError, | ||
d.__delitem__, 'key') | ||
|
||
def test_pop(self): | ||
d = self.new_dict(key='value') | ||
|
||
# pop() must change the version if the key exists | ||
self.check_version_changed(d, d.pop, 'key') | ||
|
||
# pop() must not change the version if the key does not exist | ||
self.check_version_dont_change(d, self.assertRaises, KeyError, | ||
d.pop, 'key') | ||
|
||
def test_popitem(self): | ||
d = self.new_dict(key='value') | ||
|
||
# popitem() must change the version if the dict is not empty | ||
self.check_version_changed(d, d.popitem) | ||
|
||
# popitem() must not change the version if the dict is empty | ||
self.check_version_dont_change(d, self.assertRaises, KeyError, | ||
d.popitem) | ||
|
||
def test_update(self): | ||
d = self.new_dict(key='value') | ||
|
||
# update() calling with no argument must not change the version | ||
self.check_version_dont_change(d, d.update) | ||
|
||
# update() must change the version | ||
self.check_version_changed(d, d.update, key='new value') | ||
|
||
d2 = self.new_dict(key='value 3') | ||
self.check_version_changed(d, d.update, d2) | ||
|
||
def test_clear(self): | ||
d = self.new_dict(key='value') | ||
|
||
# clear() must change the version if the dict is not empty | ||
self.check_version_changed(d, d.clear) | ||
|
||
# clear() must not change the version if the dict is empty | ||
self.check_version_dont_change(d, d.clear) | ||
|
||
|
||
class Dict(dict): | ||
pass | ||
|
||
|
||
class DictSubtypeVersionTests(DictVersionTests): | ||
type2test = Dict | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
I don't believe we implement it, unsure if this test file actually gets executed tbh.