Skip to content

Commit 7299dff

Browse files
authored
PYTHON-3546 bson.CodecOptions docs missing unicode_decode_error_handler=ignore option in newer documentation (#1131)
1 parent 47686c8 commit 7299dff

File tree

1 file changed

+86
-81
lines changed

1 file changed

+86
-81
lines changed

bson/codec_options.py

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -245,87 +245,92 @@ class _BaseCodecOptions(NamedTuple):
245245

246246

247247
class CodecOptions(_BaseCodecOptions):
248-
"""Encapsulates options used encoding and / or decoding BSON.
249-
250-
The `document_class` option is used to define a custom type for use
251-
decoding BSON documents. Access to the underlying raw BSON bytes for
252-
a document is available using the :class:`~bson.raw_bson.RawBSONDocument`
253-
type::
254-
255-
>>> from bson.raw_bson import RawBSONDocument
256-
>>> from bson.codec_options import CodecOptions
257-
>>> codec_options = CodecOptions(document_class=RawBSONDocument)
258-
>>> coll = db.get_collection('test', codec_options=codec_options)
259-
>>> doc = coll.find_one()
260-
>>> doc.raw
261-
'\\x16\\x00\\x00\\x00\\x07_id\\x00[0\\x165\\x91\\x10\\xea\\x14\\xe8\\xc5\\x8b\\x93\\x00'
262-
263-
The document class can be any type that inherits from
264-
:class:`~collections.abc.MutableMapping`::
265-
266-
>>> class AttributeDict(dict):
267-
... # A dict that supports attribute access.
268-
... def __getattr__(self, key):
269-
... return self[key]
270-
... def __setattr__(self, key, value):
271-
... self[key] = value
272-
...
273-
>>> codec_options = CodecOptions(document_class=AttributeDict)
274-
>>> coll = db.get_collection('test', codec_options=codec_options)
275-
>>> doc = coll.find_one()
276-
>>> doc._id
277-
ObjectId('5b3016359110ea14e8c58b93')
278-
279-
See :doc:`/examples/datetimes` for examples using the `tz_aware` and
280-
`tzinfo` options.
281-
282-
See :doc:`/examples/uuid` for examples using the `uuid_representation`
283-
option.
284-
285-
:Parameters:
286-
- `document_class`: BSON documents returned in queries will be decoded
287-
to an instance of this class. Must be a subclass of
288-
:class:`~collections.abc.MutableMapping`. Defaults to :class:`dict`.
289-
- `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone
290-
aware instances of :class:`~datetime.datetime`. Otherwise they will be
291-
naive. Defaults to ``False``.
292-
- `uuid_representation`: The BSON representation to use when encoding
293-
and decoding instances of :class:`~uuid.UUID`. Defaults to
294-
:data:`~bson.binary.UuidRepresentation.UNSPECIFIED`. New
295-
applications should consider setting this to
296-
:data:`~bson.binary.UuidRepresentation.STANDARD` for cross language
297-
compatibility. See :ref:`handling-uuid-data-example` for details.
298-
- `unicode_decode_error_handler`: The error handler to apply when
299-
a Unicode-related error occurs during BSON decoding that would
300-
otherwise raise :exc:`UnicodeDecodeError`. Valid options include
301-
'strict', 'replace', 'backslashreplace', 'surrogateescape', and
302-
'ignore'. Defaults to 'strict'.
303-
- `tzinfo`: A :class:`~datetime.tzinfo` subclass that specifies the
304-
timezone to/from which :class:`~datetime.datetime` objects should be
305-
encoded/decoded.
306-
- `type_registry`: Instance of :class:`TypeRegistry` used to customize
307-
encoding and decoding behavior.
308-
- `datetime_conversion`: Specifies how UTC datetimes should be decoded
309-
within BSON. Valid options include 'datetime_ms' to return as a
310-
DatetimeMS, 'datetime' to return as a datetime.datetime and
311-
raising a ValueError for out-of-range values, 'datetime_auto' to
312-
return DatetimeMS objects when the underlying datetime is
313-
out-of-range and 'datetime_clamp' to clamp to the minimum and
314-
maximum possible datetimes. Defaults to 'datetime'.
315-
.. versionchanged:: 4.0
316-
The default for `uuid_representation` was changed from
317-
:const:`~bson.binary.UuidRepresentation.PYTHON_LEGACY` to
318-
:const:`~bson.binary.UuidRepresentation.UNSPECIFIED`.
319-
320-
.. versionadded:: 3.8
321-
`type_registry` attribute.
322-
323-
.. warning:: Care must be taken when changing
324-
`unicode_decode_error_handler` from its default value ('strict').
325-
The 'replace' and 'ignore' modes should not be used when documents
326-
retrieved from the server will be modified in the client application
327-
and stored back to the server.
328-
"""
248+
"""Encapsulates options used encoding and / or decoding BSON."""
249+
250+
def __init__(self, *args, **kwargs):
251+
"""Encapsulates options used encoding and / or decoding BSON.
252+
253+
The `document_class` option is used to define a custom type for use
254+
decoding BSON documents. Access to the underlying raw BSON bytes for
255+
a document is available using the :class:`~bson.raw_bson.RawBSONDocument`
256+
type::
257+
258+
>>> from bson.raw_bson import RawBSONDocument
259+
>>> from bson.codec_options import CodecOptions
260+
>>> codec_options = CodecOptions(document_class=RawBSONDocument)
261+
>>> coll = db.get_collection('test', codec_options=codec_options)
262+
>>> doc = coll.find_one()
263+
>>> doc.raw
264+
'\\x16\\x00\\x00\\x00\\x07_id\\x00[0\\x165\\x91\\x10\\xea\\x14\\xe8\\xc5\\x8b\\x93\\x00'
265+
266+
The document class can be any type that inherits from
267+
:class:`~collections.abc.MutableMapping`::
268+
269+
>>> class AttributeDict(dict):
270+
... # A dict that supports attribute access.
271+
... def __getattr__(self, key):
272+
... return self[key]
273+
... def __setattr__(self, key, value):
274+
... self[key] = value
275+
...
276+
>>> codec_options = CodecOptions(document_class=AttributeDict)
277+
>>> coll = db.get_collection('test', codec_options=codec_options)
278+
>>> doc = coll.find_one()
279+
>>> doc._id
280+
ObjectId('5b3016359110ea14e8c58b93')
281+
282+
See :doc:`/examples/datetimes` for examples using the `tz_aware` and
283+
`tzinfo` options.
284+
285+
See :doc:`/examples/uuid` for examples using the `uuid_representation`
286+
option.
287+
288+
:Parameters:
289+
- `document_class`: BSON documents returned in queries will be decoded
290+
to an instance of this class. Must be a subclass of
291+
:class:`~collections.abc.MutableMapping`. Defaults to :class:`dict`.
292+
- `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone
293+
aware instances of :class:`~datetime.datetime`. Otherwise they will be
294+
naive. Defaults to ``False``.
295+
- `uuid_representation`: The BSON representation to use when encoding
296+
and decoding instances of :class:`~uuid.UUID`. Defaults to
297+
:data:`~bson.binary.UuidRepresentation.UNSPECIFIED`. New
298+
applications should consider setting this to
299+
:data:`~bson.binary.UuidRepresentation.STANDARD` for cross language
300+
compatibility. See :ref:`handling-uuid-data-example` for details.
301+
- `unicode_decode_error_handler`: The error handler to apply when
302+
a Unicode-related error occurs during BSON decoding that would
303+
otherwise raise :exc:`UnicodeDecodeError`. Valid options include
304+
'strict', 'replace', 'backslashreplace', 'surrogateescape', and
305+
'ignore'. Defaults to 'strict'.
306+
- `tzinfo`: A :class:`~datetime.tzinfo` subclass that specifies the
307+
timezone to/from which :class:`~datetime.datetime` objects should be
308+
encoded/decoded.
309+
- `type_registry`: Instance of :class:`TypeRegistry` used to customize
310+
encoding and decoding behavior.
311+
- `datetime_conversion`: Specifies how UTC datetimes should be decoded
312+
within BSON. Valid options include 'datetime_ms' to return as a
313+
DatetimeMS, 'datetime' to return as a datetime.datetime and
314+
raising a ValueError for out-of-range values, 'datetime_auto' to
315+
return DatetimeMS objects when the underlying datetime is
316+
out-of-range and 'datetime_clamp' to clamp to the minimum and
317+
maximum possible datetimes. Defaults to 'datetime'.
318+
319+
.. versionchanged:: 4.0
320+
The default for `uuid_representation` was changed from
321+
:const:`~bson.binary.UuidRepresentation.PYTHON_LEGACY` to
322+
:const:`~bson.binary.UuidRepresentation.UNSPECIFIED`.
323+
324+
.. versionadded:: 3.8
325+
`type_registry` attribute.
326+
327+
.. warning:: Care must be taken when changing
328+
`unicode_decode_error_handler` from its default value ('strict').
329+
The 'replace' and 'ignore' modes should not be used when documents
330+
retrieved from the server will be modified in the client application
331+
and stored back to the server.
332+
"""
333+
return super().__init__()
329334

330335
def __new__(
331336
cls: Type["CodecOptions"],

0 commit comments

Comments
 (0)