From b371af05997acae1bbced446ceeb065ce1b8e012 Mon Sep 17 00:00:00 2001 From: Alessio Date: Fri, 29 Mar 2024 12:42:02 +0100 Subject: [PATCH 1/2] PYTHON-4298 Fix _apply_local_threshold TypeError --- pymongo/topology_description.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymongo/topology_description.py b/pymongo/topology_description.py index 99243d7ce2..c9c092d86b 100644 --- a/pymongo/topology_description.py +++ b/pymongo/topology_description.py @@ -266,12 +266,12 @@ def _apply_local_threshold(self, selection: Optional[Selection]) -> list[ServerD if not selection: return [] # Round trip time in seconds. - fastest = min(cast(float, s.round_trip_time) for s in selection.server_descriptions) + fastest = min(s.round_trip_time for s in selection.server_descriptions if s.round_trip_time is not None) threshold = self._topology_settings.local_threshold_ms / 1000.0 return [ s for s in selection.server_descriptions - if (cast(float, s.round_trip_time) - fastest) <= threshold + if s.round_trip_time is not None and (s.round_trip_time - fastest) <= threshold ] def apply_selector( From bf996de6d34beb4e116ad7691c95e7a2ef580d66 Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 4 Apr 2024 13:17:02 +0200 Subject: [PATCH 2/2] PYTHON-4298 Raise ConfigurationError if round_trip_time is None --- pymongo/topology_description.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pymongo/topology_description.py b/pymongo/topology_description.py index c9c092d86b..cc2330cbab 100644 --- a/pymongo/topology_description.py +++ b/pymongo/topology_description.py @@ -265,13 +265,19 @@ def srv_max_hosts(self) -> int: def _apply_local_threshold(self, selection: Optional[Selection]) -> list[ServerDescription]: if not selection: return [] + round_trip_times: list[float] = [] + for server in selection.server_descriptions: + if server.round_trip_time is None: + config_err_msg = f"round_trip_time for server {server.address} is unexpectedly None: {self}, servers: {selection.server_descriptions}" + raise ConfigurationError(config_err_msg) + round_trip_times.append(server.round_trip_time) # Round trip time in seconds. - fastest = min(s.round_trip_time for s in selection.server_descriptions if s.round_trip_time is not None) + fastest = min(round_trip_times) threshold = self._topology_settings.local_threshold_ms / 1000.0 return [ s for s in selection.server_descriptions - if s.round_trip_time is not None and (s.round_trip_time - fastest) <= threshold + if (cast(float, s.round_trip_time) - fastest) <= threshold ] def apply_selector(