Skip to content

Commit b60eb60

Browse files
committed
address comments part 1
1 parent 76a68b2 commit b60eb60

File tree

6 files changed

+37
-47
lines changed

6 files changed

+37
-47
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ def __init__(
757757
raise TypeError(f"port must be an instance of int, not {type(port)}")
758758
self._host = host
759759
self._port = port
760+
self._topology = None
760761

761762
# _pool_class, _monitor_class, and _condition_class are for deep
762763
# customization of PyMongo, e.g. Motor.
@@ -1229,20 +1230,20 @@ def eq_props(self):
12291230

12301231
def __eq__(self, other: Any) -> bool:
12311232
if isinstance(other, self.__class__):
1232-
if hasattr(self, "_topology") and hasattr(other, "_topology"):
1233-
return self._topology == other._topology
1234-
else:
1233+
if self._topology is None:
12351234
return self.eq_props() == other.eq_props()
1235+
else:
1236+
return self._topology == other._topology
12361237
return NotImplemented
12371238

12381239
def __ne__(self, other: Any) -> bool:
12391240
return not self == other
12401241

12411242
def __hash__(self) -> int:
1242-
if hasattr(self, "_topology"):
1243-
return hash(self._topology)
1244-
else:
1243+
if self._topology is None:
12451244
raise hash(self.eq_props())
1245+
else:
1246+
return hash(self._topology)
12461247

12471248
def _repr_helper(self) -> str:
12481249
def option_repr(option: str, value: Any) -> str:
@@ -1258,16 +1259,16 @@ def option_repr(option: str, value: Any) -> str:
12581259
return f"{option}={value!r}"
12591260

12601261
# Host first...
1261-
if hasattr(self, "_topology"):
1262+
if self._topology is None:
1263+
options = ["host={self._host}", "port={self._port}"]
1264+
else:
12621265
options = [
12631266
"host=%r"
12641267
% [
12651268
"%s:%d" % (host, port) if port is not None else host
12661269
for host, port in self._topology_settings.seeds
12671270
]
12681271
]
1269-
else:
1270-
options = ["host={self._host}", "port={self._port}"]
12711272
# ... then everything in self._constructor_args...
12721273
options.extend(
12731274
option_repr(key, self._options._options[key]) for key in self._constructor_args
@@ -1673,7 +1674,7 @@ async def close(self) -> None:
16731674
.. versionchanged:: 3.6
16741675
End all server sessions created by this client.
16751676
"""
1676-
if hasattr(self, "_topology"):
1677+
if self._topology is not None:
16771678
session_ids = self._topology.pop_all_sessions()
16781679
if session_ids:
16791680
await self._end_sessions(session_ids)

pymongo/asynchronous/uri_parser.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import sys
43
from typing import Any, Optional
54
from urllib.parse import unquote_plus
65

@@ -171,14 +170,3 @@ async def _parse_srv(
171170
"nodelist": nodes,
172171
"options": options,
173172
}
174-
175-
176-
if __name__ == "__main__":
177-
import pprint
178-
179-
try:
180-
if _IS_SYNC:
181-
pprint.pprint(parse_uri(sys.argv[1])) # noqa: T203
182-
except InvalidURI as exc:
183-
print(exc) # noqa: T201
184-
sys.exit(0)

pymongo/srv_resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025-present MongoDB, Inc.
1+
# Copyright 2019-present MongoDB, Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

pymongo/synchronous/mongo_client.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ def __init__(
755755
raise TypeError(f"port must be an instance of int, not {type(port)}")
756756
self._host = host
757757
self._port = port
758+
self._topology = None
758759

759760
# _pool_class, _monitor_class, and _condition_class are for deep
760761
# customization of PyMongo, e.g. Motor.
@@ -1227,20 +1228,20 @@ def eq_props(self):
12271228

12281229
def __eq__(self, other: Any) -> bool:
12291230
if isinstance(other, self.__class__):
1230-
if hasattr(self, "_topology") and hasattr(other, "_topology"):
1231-
return self._topology == other._topology
1232-
else:
1231+
if self._topology is None:
12331232
return self.eq_props() == other.eq_props()
1233+
else:
1234+
return self._topology == other._topology
12341235
return NotImplemented
12351236

12361237
def __ne__(self, other: Any) -> bool:
12371238
return not self == other
12381239

12391240
def __hash__(self) -> int:
1240-
if hasattr(self, "_topology"):
1241-
return hash(self._topology)
1242-
else:
1241+
if self._topology is None:
12431242
raise hash(self.eq_props())
1243+
else:
1244+
return hash(self._topology)
12441245

12451246
def _repr_helper(self) -> str:
12461247
def option_repr(option: str, value: Any) -> str:
@@ -1256,16 +1257,16 @@ def option_repr(option: str, value: Any) -> str:
12561257
return f"{option}={value!r}"
12571258

12581259
# Host first...
1259-
if hasattr(self, "_topology"):
1260+
if self._topology is None:
1261+
options = ["host={self._host}", "port={self._port}"]
1262+
else:
12601263
options = [
12611264
"host=%r"
12621265
% [
12631266
"%s:%d" % (host, port) if port is not None else host
12641267
for host, port in self._topology_settings.seeds
12651268
]
12661269
]
1267-
else:
1268-
options = ["host={self._host}", "port={self._port}"]
12691270
# ... then everything in self._constructor_args...
12701271
options.extend(
12711272
option_repr(key, self._options._options[key]) for key in self._constructor_args
@@ -1667,7 +1668,7 @@ def close(self) -> None:
16671668
.. versionchanged:: 3.6
16681669
End all server sessions created by this client.
16691670
"""
1670-
if hasattr(self, "_topology"):
1671+
if self._topology is not None:
16711672
session_ids = self._topology.pop_all_sessions()
16721673
if session_ids:
16731674
self._end_sessions(session_ids)

pymongo/synchronous/uri_parser.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import sys
43
from typing import Any, Optional
54
from urllib.parse import unquote_plus
65

@@ -171,14 +170,3 @@ def _parse_srv(
171170
"nodelist": nodes,
172171
"options": options,
173172
}
174-
175-
176-
if __name__ == "__main__":
177-
import pprint
178-
179-
try:
180-
if _IS_SYNC:
181-
pprint.pprint(parse_uri(sys.argv[1])) # noqa: T203
182-
except InvalidURI as exc:
183-
print(exc) # noqa: T201
184-
sys.exit(0)

pymongo/uri_parser.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025-present MongoDB, Inc.
1+
# Copyright 2011-present MongoDB, Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,9 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Re-import of synchronous Uri Parser API for compatibility."""
15+
"""Re-import of synchronous URI Parser API for compatibility."""
1616
from __future__ import annotations
1717

18+
import sys
19+
20+
from pymongo.errors import InvalidURI
1821
from pymongo.synchronous.uri_parser import * # noqa: F403
1922
from pymongo.synchronous.uri_parser import __doc__ as original_doc
2023
from pymongo.uri_parser_shared import * # noqa: F403
@@ -29,3 +32,12 @@
2932
"split_hosts",
3033
"parse_uri",
3134
]
35+
36+
if __name__ == "__main__":
37+
import pprint
38+
39+
try:
40+
pprint.pprint(parse_uri(sys.argv[1])) # noqa: F405, T203
41+
except InvalidURI as exc:
42+
print(exc) # noqa: T201
43+
sys.exit(0)

0 commit comments

Comments
 (0)