PYTHON-5061 - Add an API to extend the bson TypeRegistry (#2345)

This commit is contained in:
Noah Stapp 2025-05-21 13:45:36 -04:00 committed by GitHub
parent 106343a6a2
commit 717fb47c17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 0 deletions

View File

@ -160,6 +160,16 @@ class TypeRegistry:
f"Expected an instance of {TypeEncoder.__name__}, {TypeDecoder.__name__}, or {TypeCodec.__name__}, got {codec!r} instead"
)
@property
def codecs(self) -> list[TypeEncoder | TypeDecoder | TypeCodec]:
"""The list of type codecs in this registry."""
return self.__type_codecs
@property
def fallback_encoder(self) -> Optional[_Fallback]:
"""The fallback encoder in this registry."""
return self._fallback_encoder
def _validate_type_encoder(self, codec: _Codec) -> None:
from bson import _BUILT_IN_TYPES

View File

@ -1,6 +1,13 @@
Changelog
=========
Changes in Version 4.14.0 (XXXX/XX/XX)
--------------------------------------
PyMongo 4.14 brings a number of changes including:
- Added :attr:`bson.codec_options.TypeRegistry.codecs` and :attr:`bson.codec_options.TypeRegistry.fallback_encoder` properties
to allow users to directly access the type codecs and fallback encoder for a given :class:`bson.codec_options.TypeRegistry`.
Changes in Version 4.13.0 (2025/05/14)
--------------------------------------

View File

@ -579,6 +579,15 @@ class TestTypeRegistry(unittest.TestCase):
with self.assertRaisesRegex(TypeError, err_msg):
TypeRegistry(fallback_encoder="hello") # type: ignore[arg-type]
def test_type_registry_codecs(self):
codec_instances = [codec() for codec in self.codecs]
type_registry = TypeRegistry(codec_instances)
self.assertEqual(type_registry.codecs, codec_instances)
def test_type_registry_fallback(self):
type_registry = TypeRegistry(fallback_encoder=self.fallback_encoder)
self.assertEqual(type_registry.fallback_encoder, self.fallback_encoder)
def test_type_registry_repr(self):
codec_instances = [codec() for codec in self.codecs]
type_registry = TypeRegistry(codec_instances)

View File

@ -579,6 +579,15 @@ class TestTypeRegistry(unittest.TestCase):
with self.assertRaisesRegex(TypeError, err_msg):
TypeRegistry(fallback_encoder="hello") # type: ignore[arg-type]
def test_type_registry_codecs(self):
codec_instances = [codec() for codec in self.codecs]
type_registry = TypeRegistry(codec_instances)
self.assertEqual(type_registry.codecs, codec_instances)
def test_type_registry_fallback(self):
type_registry = TypeRegistry(fallback_encoder=self.fallback_encoder)
self.assertEqual(type_registry.fallback_encoder, self.fallback_encoder)
def test_type_registry_repr(self):
codec_instances = [codec() for codec in self.codecs]
type_registry = TypeRegistry(codec_instances)