From d251f4241a3b159892e8c771449d72516545b7bb Mon Sep 17 00:00:00 2001 From: RinCodeForge927 Date: Tue, 30 Dec 2025 21:22:23 +0700 Subject: [PATCH 1/5] refactor: use _asdict() in _options_dict() as per PYTHON-2442 Replaced manual dictionary creation with the more idiomatic _asdict() method in both CodecOptions and JSONOptions. --- bson/codec_options.py | 11 +---------- bson/json_util.py | 2 +- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/bson/codec_options.py b/bson/codec_options.py index add5416a5b..abf9097d07 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -468,16 +468,7 @@ def _arguments_repr(self) -> str: def _options_dict(self) -> dict[str, Any]: """Dictionary of the arguments used to create this object.""" - # TODO: PYTHON-2442 use _asdict() instead - return { - "document_class": self.document_class, - "tz_aware": self.tz_aware, - "uuid_representation": self.uuid_representation, - "unicode_decode_error_handler": self.unicode_decode_error_handler, - "tzinfo": self.tzinfo, - "type_registry": self.type_registry, - "datetime_conversion": self.datetime_conversion, - } + return self._asdict() def __repr__(self) -> str: return f"{self.__class__.__name__}({self._arguments_repr()})" diff --git a/bson/json_util.py b/bson/json_util.py index 8151226a26..1e1aeecf9e 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -383,7 +383,6 @@ def _arguments_repr(self) -> str: ) def _options_dict(self) -> dict[Any, Any]: - # TODO: PYTHON-2442 use _asdict() instead options_dict = super()._options_dict() options_dict.update( { @@ -394,6 +393,7 @@ def _options_dict(self) -> dict[Any, Any]: } ) return options_dict + return options_dict def with_options(self, **kwargs: Any) -> JSONOptions: """ From a55fc4ac90cdb885020f9f11ff8331cb7d180aaf Mon Sep 17 00:00:00 2001 From: RinCodeForge927 Date: Tue, 30 Dec 2025 21:52:29 +0700 Subject: [PATCH 2/5] fix: remove duplicate return statement in _options_dict --- bson/json_util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bson/json_util.py b/bson/json_util.py index 1e1aeecf9e..6a409a5e93 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -393,7 +393,6 @@ def _options_dict(self) -> dict[Any, Any]: } ) return options_dict - return options_dict def with_options(self, **kwargs: Any) -> JSONOptions: """ From eedff69d4d567e7e674e0808dd6f14ef73baec30 Mon Sep 17 00:00:00 2001 From: RinCodeForge927 Date: Tue, 30 Dec 2025 22:19:39 +0700 Subject: [PATCH 3/5] refactor: remove unused _options_dict method As suggested in PR review, _options_dict is redundant. Replaced its usage in JSONOptions.with_options with _asdict(). --- bson/codec_options.py | 4 +--- bson/json_util.py | 14 ++------------ 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/bson/codec_options.py b/bson/codec_options.py index abf9097d07..6fbf792d5c 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -466,9 +466,7 @@ def _arguments_repr(self) -> str: ) ) - def _options_dict(self) -> dict[str, Any]: - """Dictionary of the arguments used to create this object.""" - return self._asdict() + def __repr__(self) -> str: return f"{self.__class__.__name__}({self._arguments_repr()})" diff --git a/bson/json_util.py b/bson/json_util.py index 6a409a5e93..c234e33866 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -382,17 +382,7 @@ def _arguments_repr(self) -> str: ) ) - def _options_dict(self) -> dict[Any, Any]: - options_dict = super()._options_dict() - options_dict.update( - { - "strict_number_long": self.strict_number_long, - "datetime_representation": self.datetime_representation, - "strict_uuid": self.strict_uuid, - "json_mode": self.json_mode, - } - ) - return options_dict + def with_options(self, **kwargs: Any) -> JSONOptions: """ @@ -407,7 +397,7 @@ def with_options(self, **kwargs: Any) -> JSONOptions: .. versionadded:: 3.12 """ - opts = self._options_dict() + opts = self._asdict() for opt in ("strict_number_long", "datetime_representation", "strict_uuid", "json_mode"): opts[opt] = kwargs.get(opt, getattr(self, opt)) opts.update(kwargs) From 2c96df8f06099a49790434d95d1b2ecdd1f8e8b5 Mon Sep 17 00:00:00 2001 From: RinCodeForge927 Date: Tue, 30 Dec 2025 22:39:45 +0700 Subject: [PATCH 4/5] refactor: remove unused _options_dict entirely Following reviewer feedback, the private method _options_dict has been removed from both CodecOptions and JSONOptions. Call sites have been updated to use _asdict() directly. --- bson/codec_options.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bson/codec_options.py b/bson/codec_options.py index 6fbf792d5c..7938296592 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -273,8 +273,7 @@ def with_options(self, **kwargs: Any) -> CodecOptions[Any]: def _arguments_repr(self) -> str: ... - def _options_dict(self) -> dict[Any, Any]: - ... + # NamedTuple API @classmethod @@ -483,7 +482,7 @@ def with_options(self, **kwargs: Any) -> CodecOptions: .. versionadded:: 3.5 """ - opts = self._options_dict() + opts = self._asdict() opts.update(kwargs) return CodecOptions(**opts) From 7947983d26b7bb58d14c25a6733443ed88a9ebd5 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 30 Dec 2025 09:53:17 -0600 Subject: [PATCH 5/5] lint --- bson/codec_options.py | 4 ---- bson/json_util.py | 2 -- 2 files changed, 6 deletions(-) diff --git a/bson/codec_options.py b/bson/codec_options.py index 7938296592..569ac55026 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -273,8 +273,6 @@ def with_options(self, **kwargs: Any) -> CodecOptions[Any]: def _arguments_repr(self) -> str: ... - - # NamedTuple API @classmethod def _make(cls, obj: Iterable[Any]) -> CodecOptions[_DocumentType]: @@ -465,8 +463,6 @@ def _arguments_repr(self) -> str: ) ) - - def __repr__(self) -> str: return f"{self.__class__.__name__}({self._arguments_repr()})" diff --git a/bson/json_util.py b/bson/json_util.py index c234e33866..0b59c406dd 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -382,8 +382,6 @@ def _arguments_repr(self) -> str: ) ) - - def with_options(self, **kwargs: Any) -> JSONOptions: """ Make a copy of this JSONOptions, overriding some options::