PYTHON-5362 WriteConcern repr should be eval-able (#2338)

This commit is contained in:
Shane Harvey 2025-05-13 13:37:18 -07:00 committed by GitHub
parent 2374f3811a
commit 4cc5e89ebf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,21 @@
Changelog Changelog
========= =========
Changes in Version 4.13.0 (2025/05/14)
--------------------------------------
PyMongo 4.13 brings a number of changes including:
- Fixed a bug where :class:`pymongo.write_concern.WriteConcern` repr was not eval-able
when using ``w="majority"``.
Issues Resolved
...............
See the `PyMongo 4.13 release notes in JIRA`_ for the list of resolved issues
in this release.
.. _PyMongo 4.13 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=42509
Changes in Version 4.12.1 (2025/04/29) Changes in Version 4.12.1 (2025/04/29)
-------------------------------------- --------------------------------------

View File

@ -127,7 +127,7 @@ class WriteConcern:
def __repr__(self) -> str: def __repr__(self) -> str:
return "WriteConcern({})".format( return "WriteConcern({})".format(
", ".join("{}={}".format(*kvt) for kvt in self.__document.items()) ", ".join(f"{k}={v!r}" for k, v in self.__document.items())
) )
def __eq__(self, other: Any) -> bool: def __eq__(self, other: Any) -> bool:

View File

@ -67,6 +67,19 @@ class TestWriteConcern(unittest.TestCase):
_fake_type = collections.namedtuple("NotAWriteConcern", ["document"]) # type: ignore _fake_type = collections.namedtuple("NotAWriteConcern", ["document"]) # type: ignore
self.assertNotEqual(WriteConcern(j=True), _fake_type({"j": True})) self.assertNotEqual(WriteConcern(j=True), _fake_type({"j": True}))
def assertRepr(self, obj):
new_obj = eval(repr(obj))
self.assertEqual(type(new_obj), type(obj))
self.assertEqual(repr(new_obj), repr(obj))
def test_repr(self):
concern = WriteConcern(j=True, wtimeout=3000, w="majority", fsync=False)
self.assertRepr(concern)
self.assertEqual(
repr(concern),
"WriteConcern(wtimeout=3000, j=True, fsync=False, w='majority')",
)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()