Skip to content

PYTHON-4690 Add repr for FixedOffset eg FixedOffset(datetime.timedelta(seconds=3600), '+60')) #1806

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 3 commits into from
Aug 23, 2024
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
3 changes: 3 additions & 0 deletions bson/tz_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def __init__(self, offset: Union[float, timedelta], name: str) -> None:
def __getinitargs__(self) -> Tuple[timedelta, str]:
return self.__offset, self.__name

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.__offset!r}, {self.__name!r})"

def utcoffset(self, dt: Optional[datetime]) -> timedelta:
return self.__offset

Expand Down
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PyMongo 4.9 brings a number of improvements including:
:class:`~pymongo.operations.DeleteOne`, and
:class:`~pymongo.operations.DeleteMany` operations, so
they can be used in the new :meth:`~pymongo.mongo_client.MongoClient.bulk_write`.
- Added :func:`repr` support to :class:`bson.tz_util.FixedOffset`.

Issues Resolved
...............
Expand Down
4 changes: 4 additions & 0 deletions test/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ def test_tzinfo(self):
tz = FixedOffset(42, "forty-two")
self.assertRaises(ValueError, CodecOptions, tzinfo=tz)
self.assertEqual(tz, CodecOptions(tz_aware=True, tzinfo=tz).tzinfo)
self.assertEqual(repr(tz), "FixedOffset(datetime.timedelta(seconds=2520), 'forty-two')")
self.assertEqual(
repr(eval(repr(tz))), "FixedOffset(datetime.timedelta(seconds=2520), 'forty-two')"
)

def test_codec_options_repr(self):
r = (
Expand Down
Loading