Skip to content

Commit 34da931

Browse files
authored
PYTHON-3867 add types to topology.py (#1346)
1 parent 5d6d8ca commit 34da931

11 files changed

+144
-86
lines changed

pymongo/client_session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179

180180
from pymongo.pool import Connection
181181
from pymongo.server import Server
182-
from pymongo.typings import _Address
182+
from pymongo.typings import ClusterTime, _Address
183183

184184

185185
class SessionOptions:
@@ -562,7 +562,7 @@ def session_id(self) -> Mapping[str, Any]:
562562
return self._server_session.session_id
563563

564564
@property
565-
def cluster_time(self) -> Optional[Mapping[str, Any]]:
565+
def cluster_time(self) -> Optional[ClusterTime]:
566566
"""The cluster time returned by the last operation executed
567567
in this session.
568568
"""

pymongo/hello.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from bson.objectid import ObjectId
2323
from pymongo import common
2424
from pymongo.server_type import SERVER_TYPE
25-
from pymongo.typings import _DocumentType
25+
from pymongo.typings import ClusterTime, _DocumentType
2626

2727

2828
class HelloCompat:
@@ -155,7 +155,7 @@ def election_id(self) -> Optional[ObjectId]:
155155
return self._doc.get("electionId")
156156

157157
@property
158-
def cluster_time(self) -> Optional[Mapping[str, Any]]:
158+
def cluster_time(self) -> Optional[ClusterTime]:
159159
return self._doc.get("$clusterTime")
160160

161161
@property

pymongo/mongo_client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
from pymongo.topology import Topology, _ErrorContext
9999
from pymongo.topology_description import TOPOLOGY_TYPE, TopologyDescription
100100
from pymongo.typings import (
101+
ClusterTime,
101102
_Address,
102103
_CollationIn,
103104
_DocumentType,
@@ -1106,10 +1107,10 @@ def primary(self) -> Optional[Tuple[str, int]]:
11061107
.. versionadded:: 3.0
11071108
MongoClient gained this property in version 3.0.
11081109
"""
1109-
return self._topology.get_primary()
1110+
return self._topology.get_primary() # type: ignore[return-value]
11101111

11111112
@property
1112-
def secondaries(self) -> Set[Tuple[str, int]]:
1113+
def secondaries(self) -> Set[_Address]:
11131114
"""The secondary members known to this client.
11141115
11151116
A sequence of (host, port) pairs. Empty if this client is not
@@ -1122,7 +1123,7 @@ def secondaries(self) -> Set[Tuple[str, int]]:
11221123
return self._topology.get_secondaries()
11231124

11241125
@property
1125-
def arbiters(self) -> Set[Tuple[str, int]]:
1126+
def arbiters(self) -> Set[_Address]:
11261127
"""Arbiters in the replica set.
11271128
11281129
A sequence of (host, port) pairs. Empty if this client is not
@@ -1729,7 +1730,7 @@ def _kill_cursors(
17291730
if address:
17301731
# address could be a tuple or _CursorAddress, but
17311732
# select_server_by_address needs (host, port).
1732-
server = topology.select_server_by_address(tuple(address))
1733+
server = topology.select_server_by_address(tuple(address)) # type: ignore[arg-type]
17331734
else:
17341735
# Application called close_cursor() with no address.
17351736
server = topology.select_server(writable_server_selector)
@@ -1906,7 +1907,7 @@ def _send_cluster_time(
19061907
session_time = session.cluster_time if session else None
19071908
if topology_time and session_time:
19081909
if topology_time["clusterTime"] > session_time["clusterTime"]:
1909-
cluster_time = topology_time
1910+
cluster_time: Optional[ClusterTime] = topology_time
19101911
else:
19111912
cluster_time = session_time
19121913
else:
@@ -2271,7 +2272,7 @@ def contribute_socket(self, conn: Connection, completed_handshake: bool = True)
22712272
def handle(
22722273
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException]
22732274
) -> None:
2274-
if self.handled or exc_type is None:
2275+
if self.handled or exc_val is None:
22752276
return
22762277
self.handled = True
22772278
if self.session:
@@ -2285,7 +2286,6 @@ def handle(
22852286
"RetryableWriteError"
22862287
):
22872288
self.session._unpin()
2288-
22892289
err_ctx = _ErrorContext(
22902290
exc_val,
22912291
self.max_wire_version,
@@ -2300,8 +2300,8 @@ def __enter__(self) -> _MongoClientErrorHandler:
23002300

23012301
def __exit__(
23022302
self,
2303-
exc_type: Optional[Type[BaseException]],
2304-
exc_val: Optional[BaseException],
2303+
exc_type: Optional[Type[Exception]],
2304+
exc_val: Optional[Exception],
23052305
exc_tb: Optional[TracebackType],
23062306
) -> None:
23072307
return self.handle(exc_type, exc_val)

pymongo/pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
from pymongo.read_concern import ReadConcern
106106
from pymongo.read_preferences import _ServerMode
107107
from pymongo.server_api import ServerApi
108-
from pymongo.typings import _Address, _CollationIn
108+
from pymongo.typings import ClusterTime, _Address, _CollationIn
109109
from pymongo.write_concern import WriteConcern
110110

111111
try:
@@ -779,7 +779,7 @@ def hello(self) -> Hello[Dict[str, Any]]:
779779

780780
def _hello(
781781
self,
782-
cluster_time: Optional[Mapping[str, Any]],
782+
cluster_time: Optional[ClusterTime],
783783
topology_version: Optional[Any],
784784
heartbeat_frequency: Optional[int],
785785
) -> Hello[Dict[str, Any]]:

pymongo/server_description.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from bson.objectid import ObjectId
2323
from pymongo.hello import Hello
2424
from pymongo.server_type import SERVER_TYPE
25-
from pymongo.typings import _Address
25+
from pymongo.typings import ClusterTime, _Address
2626

2727

2828
class ServerDescription:
@@ -176,7 +176,7 @@ def election_id(self) -> Optional[ObjectId]:
176176
return self._election_id
177177

178178
@property
179-
def cluster_time(self) -> Optional[Mapping[str, Any]]:
179+
def cluster_time(self) -> Optional[ClusterTime]:
180180
return self._cluster_time
181181

182182
@property

pymongo/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ def pool_options(self) -> PoolOptions:
9595
return self._pool_options
9696

9797
@property
98-
def monitor_class(self) -> Optional[Type[monitor.Monitor]]:
98+
def monitor_class(self) -> Type[monitor.Monitor]:
9999
return self._monitor_class
100100

101101
@property
102-
def condition_class(self) -> Optional[Type[threading.Condition]]:
102+
def condition_class(self) -> Type[threading.Condition]:
103103
return self._condition_class
104104

105105
@property

0 commit comments

Comments
 (0)