Skip to content

Commit c6ed94c

Browse files
committed
Add codecs and fallback_encoder properties instead of add_codec
1 parent 106343a commit c6ed94c

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

bson/codec_options.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ class TypeCodec(TypeEncoder, TypeDecoder):
108108

109109
class TypeRegistry:
110110
"""Encapsulates type codecs used in encoding and / or decoding BSON, as
111-
well as the fallback encoder. Type registries cannot be modified after
112-
instantiation.
111+
well as the fallback encoder.
113112
114113
``TypeRegistry`` can be initialized with an iterable of type codecs, and
115114
a callable for the fallback encoder::
@@ -160,6 +159,16 @@ def __init__(
160159
f"Expected an instance of {TypeEncoder.__name__}, {TypeDecoder.__name__}, or {TypeCodec.__name__}, got {codec!r} instead"
161160
)
162161

162+
@property
163+
def codecs(self) -> list[TypeEncoder | TypeDecoder | TypeCodec]:
164+
"""The list of type codecs in this registry."""
165+
return self.__type_codecs
166+
167+
@property
168+
def fallback_encoder(self) -> Optional[_Fallback]:
169+
"""The fallback encoder in this registry."""
170+
return self._fallback_encoder
171+
163172
def _validate_type_encoder(self, codec: _Codec) -> None:
164173
from bson import _BUILT_IN_TYPES
165174

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
Changes in Version 4.14.0 (XXXX/XX/XX)
5+
--------------------------------------
6+
PyMongo 4.14 brings a number of changes including:
7+
8+
- Added :meth:`bson.codec_options.TypeRegistry.codecs` and :meth:`bson.codec_options.TypeRegistry.fallback_encoder` properties
9+
to allow users to directly access the type codecs and fallback encoder for a given :class:`bson.codec_options.TypeRegistry`.
10+
411
Changes in Version 4.13.0 (2025/05/14)
512
--------------------------------------
613

test/asynchronous/test_custom_types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,15 @@ def test_initialize_fail(self):
579579
with self.assertRaisesRegex(TypeError, err_msg):
580580
TypeRegistry(fallback_encoder="hello") # type: ignore[arg-type]
581581

582+
def test_type_registry_codecs(self):
583+
codec_instances = [codec() for codec in self.codecs]
584+
type_registry = TypeRegistry(codec_instances)
585+
self.assertEqual(type_registry.codecs, codec_instances)
586+
587+
def test_type_registry_fallback(self):
588+
type_registry = TypeRegistry(fallback_encoder=self.fallback_encoder)
589+
self.assertEqual(type_registry.fallback_encoder, self.fallback_encoder)
590+
582591
def test_type_registry_repr(self):
583592
codec_instances = [codec() for codec in self.codecs]
584593
type_registry = TypeRegistry(codec_instances)

test/test_custom_types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,15 @@ def test_initialize_fail(self):
579579
with self.assertRaisesRegex(TypeError, err_msg):
580580
TypeRegistry(fallback_encoder="hello") # type: ignore[arg-type]
581581

582+
def test_type_registry_codecs(self):
583+
codec_instances = [codec() for codec in self.codecs]
584+
type_registry = TypeRegistry(codec_instances)
585+
self.assertEqual(type_registry.codecs, codec_instances)
586+
587+
def test_type_registry_fallback(self):
588+
type_registry = TypeRegistry(fallback_encoder=self.fallback_encoder)
589+
self.assertEqual(type_registry.fallback_encoder, self.fallback_encoder)
590+
582591
def test_type_registry_repr(self):
583592
codec_instances = [codec() for codec in self.codecs]
584593
type_registry = TypeRegistry(codec_instances)

0 commit comments

Comments
 (0)