Skip to content

Commit a7579b0

Browse files
authored
PYTHON-3259 Improve migration guide for loads/JSONOptions/tz_aware (#946)
1 parent a624197 commit a7579b0

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

bson/json_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ def loads(s: str, *args: Any, **kwargs: Any) -> Any:
438438
decoding of MongoDB Extended JSON types. Defaults to
439439
:const:`DEFAULT_JSON_OPTIONS`.
440440
441+
.. versionchanged:: 4.0
442+
Now loads :class:`datetime.datetime` instances as naive by default. To
443+
load timezone aware instances utilize the `json_options` parameter.
444+
See :ref:`tz_aware_default_change` for an example.
445+
441446
.. versionchanged:: 3.5
442447
Parses Relaxed and Canonical Extended JSON as well as PyMongo's legacy
443448
format. Now raises ``TypeError`` or ``ValueError`` when parsing JSON

doc/changelog.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ Breaking Changes in 4.0
256256
:class:`~bson.dbref.DBRef`.
257257
- The "tls" install extra is no longer necessary or supported and will be
258258
ignored by pip.
259-
- ``tz_aware``, an argument for :class:`~bson.json_util.JSONOptions`,
260-
now defaults to ``False`` instead of ``True``. ``json_util.loads`` now
261-
decodes datetime as naive by default.
259+
- The ``tz_aware`` argument to :class:`~bson.json_util.JSONOptions`
260+
now defaults to ``False`` instead of ``True``. :meth:`bson.json_util.loads` now
261+
decodes datetime as naive by default. See :ref:`tz_aware_default_change` for more info.
262262
- ``directConnection`` URI option and keyword argument to :class:`~pymongo.mongo_client.MongoClient`
263263
defaults to ``False`` instead of ``None``, allowing for the automatic
264264
discovery of replica sets. This means that if you

doc/migrate-to-pymongo4.rst

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,30 @@ can be changed to this::
253253
client.options.pool_options.min_pool_size
254254
client.options.pool_options.max_idle_time_seconds
255255

256+
.. _tz_aware_default_change:
257+
256258
``tz_aware`` defaults to ``False``
257259
..................................
258260

259-
``tz_aware``, an argument for :class:`~bson.json_util.JSONOptions`,
260-
now defaults to ``False`` instead of ``True``. ``json_util.loads`` now
261-
decodes datetime as naive by default.
261+
The ``tz_aware`` argument to :class:`~bson.json_util.JSONOptions`
262+
now defaults to ``False`` instead of ``True``. :meth:`bson.json_util.loads`
263+
now decodes datetime as naive by default::
264+
265+
>>> from bson import json_util
266+
>>> s = '{"dt": {"$date": "2022-05-09T17:54:00Z"}}'
267+
>>> json_util.loads(s)
268+
{'dt': datetime.datetime(2022, 5, 9, 17, 54)}
269+
270+
To retain the PyMongo 3 behavior set ``tz_aware=True``, for example::
271+
272+
>>> from bson import json_util
273+
>>> opts = json_util.JSONOptions(tz_aware=True)
274+
>>> s = '{"dt": {"$date": "2022-05-09T17:54:00Z"}}'
275+
>>> json_util.loads(s, json_options=opts)
276+
{'dt': datetime.datetime(2022, 5, 9, 17, 54, tzinfo=<bson.tz_util.FixedOffset object at 0x7fd1ebc1add0>)}
277+
278+
This change was made to match the default behavior of
279+
:class:`~bson.codec_options.CodecOptions` and :class:`bson.decode`.
262280

263281
MongoClient cannot execute operations after ``close()``
264282
.......................................................

0 commit comments

Comments
 (0)