From c6ed94cad3262efc21751c467f251fab1f6e2439 Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Tue, 20 May 2025 15:39:16 -0400 Subject: [PATCH 1/3] Add codecs and fallback_encoder properties instead of add_codec --- bson/codec_options.py | 13 +++++++++++-- doc/changelog.rst | 7 +++++++ test/asynchronous/test_custom_types.py | 9 +++++++++ test/test_custom_types.py | 9 +++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/bson/codec_options.py b/bson/codec_options.py index 258a777a1b..ac93d8743d 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -108,8 +108,7 @@ class TypeCodec(TypeEncoder, TypeDecoder): class TypeRegistry: """Encapsulates type codecs used in encoding and / or decoding BSON, as - well as the fallback encoder. Type registries cannot be modified after - instantiation. + well as the fallback encoder. ``TypeRegistry`` can be initialized with an iterable of type codecs, and a callable for the fallback encoder:: @@ -160,6 +159,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 diff --git a/doc/changelog.rst b/doc/changelog.rst index 80d1c4e2f0..92d4c39c77 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,13 @@ Changelog ========= +Changes in Version 4.14.0 (XXXX/XX/XX) +-------------------------------------- +PyMongo 4.14 brings a number of changes including: + +- Added :meth:`bson.codec_options.TypeRegistry.codecs` and :meth:`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) -------------------------------------- diff --git a/test/asynchronous/test_custom_types.py b/test/asynchronous/test_custom_types.py index 0f9d737afe..0ab9e95fe0 100644 --- a/test/asynchronous/test_custom_types.py +++ b/test/asynchronous/test_custom_types.py @@ -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) diff --git a/test/test_custom_types.py b/test/test_custom_types.py index 08e2a46f8f..bcdc14f2e9 100644 --- a/test/test_custom_types.py +++ b/test/test_custom_types.py @@ -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) From a7dbe0b13cae8c38a1bb205fe7bfe26116abe550 Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Tue, 20 May 2025 15:40:44 -0400 Subject: [PATCH 2/3] Fix TypeRegistry docstring --- bson/codec_options.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bson/codec_options.py b/bson/codec_options.py index ac93d8743d..0428cf843f 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -108,7 +108,8 @@ class TypeCodec(TypeEncoder, TypeDecoder): class TypeRegistry: """Encapsulates type codecs used in encoding and / or decoding BSON, as - well as the fallback encoder. + well as the fallback encoder. Type registries cannot be modified after + instantiation. ``TypeRegistry`` can be initialized with an iterable of type codecs, and a callable for the fallback encoder:: From 951d44bf8e5b8b6dfb46828ec21d41c537666f51 Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Wed, 21 May 2025 09:39:43 -0400 Subject: [PATCH 3/3] Address review --- doc/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index 92d4c39c77..c44cfb41a2 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -5,7 +5,7 @@ Changes in Version 4.14.0 (XXXX/XX/XX) -------------------------------------- PyMongo 4.14 brings a number of changes including: -- Added :meth:`bson.codec_options.TypeRegistry.codecs` and :meth:`bson.codec_options.TypeRegistry.fallback_encoder` properties +- 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)