Skip to content

Commit 76a68b2

Browse files
committed
change client eq and hash
1 parent 8927a27 commit 76a68b2

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,9 @@ def __init__(
847847
"username": username,
848848
"password": password,
849849
"dbase": dbase,
850+
"seeds": seeds,
850851
"fqdn": fqdn,
852+
"srv_service_name": srv_service_name,
851853
"pool_class": pool_class,
852854
"monitor_class": monitor_class,
853855
"condition_class": condition_class,
@@ -856,6 +858,13 @@ def __init__(
856858
if not is_srv:
857859
self._init_based_on_options(seeds, srv_max_hosts, srv_service_name)
858860

861+
super().__init__(
862+
self._options.codec_options,
863+
self._options.read_preference,
864+
self._options.write_concern,
865+
self._options.read_concern,
866+
)
867+
859868
self._opened = False
860869
self._closed = False
861870
if not is_srv:
@@ -937,12 +946,6 @@ def _init_based_on_options(
937946
self, seeds: Collection[tuple[str, int]], srv_max_hosts: Any, srv_service_name: Any
938947
) -> None:
939948
self._event_listeners = self._options.pool_options._event_listeners
940-
super().__init__(
941-
self._options.codec_options,
942-
self._options.read_preference,
943-
self._options.write_concern,
944-
self._options.read_concern,
945-
)
946949
self._topology_settings = TopologySettings(
947950
seeds=seeds,
948951
replica_set_name=self._options.replica_set_name,
@@ -1216,14 +1219,20 @@ def options(self) -> ClientOptions:
12161219
"""
12171220
return self._options
12181221

1222+
def eq_props(self):
1223+
return (
1224+
tuple(sorted(self._resolve_srv_info["seeds"])),
1225+
self._options.replica_set_name,
1226+
self._resolve_srv_info["fqdn"],
1227+
self._resolve_srv_info["srv_service_name"],
1228+
)
1229+
12191230
def __eq__(self, other: Any) -> bool:
12201231
if isinstance(other, self.__class__):
12211232
if hasattr(self, "_topology") and hasattr(other, "_topology"):
12221233
return self._topology == other._topology
12231234
else:
1224-
raise InvalidOperation(
1225-
"Cannot compare client equality until both clients are connected"
1226-
)
1235+
return self.eq_props() == other.eq_props()
12271236
return NotImplemented
12281237

12291238
def __ne__(self, other: Any) -> bool:
@@ -1233,7 +1242,7 @@ def __hash__(self) -> int:
12331242
if hasattr(self, "_topology"):
12341243
return hash(self._topology)
12351244
else:
1236-
raise InvalidOperation("Cannot hash client until it is connected")
1245+
raise hash(self.eq_props())
12371246

12381247
def _repr_helper(self) -> str:
12391248
def option_repr(option: str, value: Any) -> str:

pymongo/synchronous/mongo_client.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,9 @@ def __init__(
845845
"username": username,
846846
"password": password,
847847
"dbase": dbase,
848+
"seeds": seeds,
848849
"fqdn": fqdn,
850+
"srv_service_name": srv_service_name,
849851
"pool_class": pool_class,
850852
"monitor_class": monitor_class,
851853
"condition_class": condition_class,
@@ -854,6 +856,13 @@ def __init__(
854856
if not is_srv:
855857
self._init_based_on_options(seeds, srv_max_hosts, srv_service_name)
856858

859+
super().__init__(
860+
self._options.codec_options,
861+
self._options.read_preference,
862+
self._options.write_concern,
863+
self._options.read_concern,
864+
)
865+
857866
self._opened = False
858867
self._closed = False
859868
if not is_srv:
@@ -935,12 +944,6 @@ def _init_based_on_options(
935944
self, seeds: Collection[tuple[str, int]], srv_max_hosts: Any, srv_service_name: Any
936945
) -> None:
937946
self._event_listeners = self._options.pool_options._event_listeners
938-
super().__init__(
939-
self._options.codec_options,
940-
self._options.read_preference,
941-
self._options.write_concern,
942-
self._options.read_concern,
943-
)
944947
self._topology_settings = TopologySettings(
945948
seeds=seeds,
946949
replica_set_name=self._options.replica_set_name,
@@ -1214,14 +1217,20 @@ def options(self) -> ClientOptions:
12141217
"""
12151218
return self._options
12161219

1220+
def eq_props(self):
1221+
return (
1222+
tuple(sorted(self._resolve_srv_info["seeds"])),
1223+
self._options.replica_set_name,
1224+
self._resolve_srv_info["fqdn"],
1225+
self._resolve_srv_info["srv_service_name"],
1226+
)
1227+
12171228
def __eq__(self, other: Any) -> bool:
12181229
if isinstance(other, self.__class__):
12191230
if hasattr(self, "_topology") and hasattr(other, "_topology"):
12201231
return self._topology == other._topology
12211232
else:
1222-
raise InvalidOperation(
1223-
"Cannot compare client equality until both clients are connected"
1224-
)
1233+
return self.eq_props() == other.eq_props()
12251234
return NotImplemented
12261235

12271236
def __ne__(self, other: Any) -> bool:
@@ -1231,7 +1240,7 @@ def __hash__(self) -> int:
12311240
if hasattr(self, "_topology"):
12321241
return hash(self._topology)
12331242
else:
1234-
raise InvalidOperation("Cannot hash client until it is connected")
1243+
raise hash(self.eq_props())
12351244

12361245
def _repr_helper(self) -> str:
12371246
def option_repr(option: str, value: Any) -> str:

0 commit comments

Comments
 (0)