Skip to content

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

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
May 21, 2025
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
10 changes: 10 additions & 0 deletions bson/codec_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ def __init__(
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

Expand Down
7 changes: 7 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -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)
--------------------------------------

Expand Down
9 changes: 9 additions & 0 deletions test/asynchronous/test_custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ def test_initialize_fail(self):
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)
Expand Down
9 changes: 9 additions & 0 deletions test/test_custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ def test_initialize_fail(self):
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)
Expand Down
Loading