PYTHON-2442: Refactor: use _asdict() in _options_dict() (#2670)

Co-authored-by: Steven Silvester <steve.silvester@mongodb.com>
This commit is contained in:
Rin 2025-12-30 23:41:37 +07:00 committed by GitHub
parent fdb1f7ea4a
commit 6585d9cb51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 31 deletions

View File

@ -273,9 +273,6 @@ if TYPE_CHECKING:
def _arguments_repr(self) -> str:
...
def _options_dict(self) -> dict[Any, Any]:
...
# NamedTuple API
@classmethod
def _make(cls, obj: Iterable[Any]) -> CodecOptions[_DocumentType]:
@ -466,19 +463,6 @@ else:
)
)
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,
}
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._arguments_repr()})"
@ -494,7 +478,7 @@ else:
.. versionadded:: 3.5
"""
opts = self._options_dict()
opts = self._asdict()
opts.update(kwargs)
return CodecOptions(**opts)

View File

@ -382,19 +382,6 @@ class JSONOptions(_BASE_CLASS):
)
)
def _options_dict(self) -> dict[Any, Any]:
# TODO: PYTHON-2442 use _asdict() instead
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:
"""
Make a copy of this JSONOptions, overriding some options::
@ -408,7 +395,7 @@ class JSONOptions(_BASE_CLASS):
.. 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)