Skip to content

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

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 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
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)
--------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pymongo/write_concern.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def acknowledged(self) -> bool:

def __repr__(self) -> str:
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:
Expand Down
13 changes: 13 additions & 0 deletions test/test_write_concern.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ def test_equality_incompatible_type(self):
_fake_type = collections.namedtuple("NotAWriteConcern", ["document"]) # type: ignore
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__":
unittest.main()
Loading