Skip to content

Commit 379dfb6

Browse files
committed
fix typing
1 parent d616135 commit 379dfb6

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

pymongo/asynchronous/client_session.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ def _txn_read_preference(self) -> Optional[_ServerMode]:
996996
def _materialize(self, logical_session_timeout_minutes: Optional[int] = None) -> None:
997997
if isinstance(self._server_session, _EmptyServerSession):
998998
old = self._server_session
999+
assert self._client._topology is not None
9991000
self._server_session = self._client._topology.get_server_session(
10001001
logical_session_timeout_minutes
10011002
)

pymongo/asynchronous/mongo_client.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +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
760+
self._topology: Optional[Topology] = None
761761

762762
# _pool_class, _monitor_class, and _condition_class are for deep
763763
# customization of PyMongo, e.g. Motor.
@@ -1036,6 +1036,7 @@ def _should_pin_cursor(self, session: Optional[AsyncClientSession]) -> Optional[
10361036

10371037
def _after_fork(self) -> None:
10381038
"""Resets topology in a child after successfully forking."""
1039+
assert self._topology is not None
10391040
self._init_background(self._topology._pid)
10401041
# Reset the session pool to avoid duplicate sessions in the child process.
10411042
self._topology._session_pool.reset()
@@ -1195,6 +1196,7 @@ def topology_description(self) -> TopologyDescription:
11951196
11961197
.. versionadded:: 4.0
11971198
"""
1199+
assert self._topology is not None
11981200
return self._topology.description
11991201

12001202
@property
@@ -1208,6 +1210,7 @@ def nodes(self) -> FrozenSet[_Address]:
12081210
to any servers, or a network partition causes it to lose connection
12091211
to all servers.
12101212
"""
1213+
assert self._topology is not None
12111214
description = self._topology.description
12121215
return frozenset(s.address for s in description.known_servers)
12131216

@@ -1221,7 +1224,7 @@ def options(self) -> ClientOptions:
12211224
"""
12221225
return self._options
12231226

1224-
def eq_props(self):
1227+
def eq_props(self) -> tuple[tuple[_Address, ...], Optional[str], Optional[str], str]:
12251228
return (
12261229
tuple(sorted(self._resolve_srv_info["seeds"])),
12271230
self._options.replica_set_name,
@@ -1242,7 +1245,7 @@ def __ne__(self, other: Any) -> bool:
12421245

12431246
def __hash__(self) -> int:
12441247
if self._topology is None:
1245-
raise hash(self.eq_props())
1248+
return hash(self.eq_props())
12461249
else:
12471250
return hash(self._topology)
12481251

@@ -1387,6 +1390,7 @@ def _ensure_session(
13871390
def _send_cluster_time(
13881391
self, command: MutableMapping[str, Any], session: Optional[AsyncClientSession]
13891392
) -> None:
1393+
assert self._topology is not None
13901394
topology_time = self._topology.max_cluster_time()
13911395
session_time = session.cluster_time if session else None
13921396
if topology_time and session_time:
@@ -1572,6 +1576,7 @@ async def address(self) -> Optional[tuple[str, int]]:
15721576
15731577
.. versionadded:: 3.0
15741578
"""
1579+
assert self._topology is not None
15751580
topology_type = self._topology._description.topology_type
15761581
if (
15771582
topology_type == TOPOLOGY_TYPE.Sharded
@@ -1594,6 +1599,7 @@ async def primary(self) -> Optional[tuple[str, int]]:
15941599
.. versionadded:: 3.0
15951600
AsyncMongoClient gained this property in version 3.0.
15961601
"""
1602+
assert self._topology is not None
15971603
return await self._topology.get_primary() # type: ignore[return-value]
15981604

15991605
@property
@@ -1607,6 +1613,7 @@ async def secondaries(self) -> set[_Address]:
16071613
.. versionadded:: 3.0
16081614
AsyncMongoClient gained this property in version 3.0.
16091615
"""
1616+
assert self._topology is not None
16101617
return await self._topology.get_secondaries()
16111618

16121619
@property
@@ -1617,6 +1624,7 @@ async def arbiters(self) -> set[_Address]:
16171624
connected to a replica set, there are no arbiters, or this client was
16181625
created without the `replicaSet` option.
16191626
"""
1627+
assert self._topology is not None
16201628
return await self._topology.get_arbiters()
16211629

16221630
@property
@@ -1709,10 +1717,12 @@ async def _get_topology(self) -> Topology:
17091717
if self._resolve_srv_info["is_srv"]:
17101718
await self._resolve_srv()
17111719
self._init_background()
1720+
assert self._topology is not None
17121721
await self._topology.open()
17131722
async with self._lock:
17141723
self._kill_cursors_executor.open()
17151724
self._opened = True
1725+
assert self._topology is not None
17161726
return self._topology
17171727

17181728
@contextlib.asynccontextmanager
@@ -1815,6 +1825,7 @@ async def _conn_from_server(
18151825
# Thread safe: if the type is single it cannot change.
18161826
# NOTE: We already opened the Topology when selecting a server so there's no need
18171827
# to call _get_topology() again.
1828+
assert self._topology is not None
18181829
single = self._topology.description.topology_type == TOPOLOGY_TYPE.Single
18191830
async with self._checkout(server, session) as conn:
18201831
if single:
@@ -2154,6 +2165,7 @@ async def _process_kill_cursors(self) -> None:
21542165
"""Process any pending kill cursors requests."""
21552166
address_to_cursor_ids = defaultdict(list)
21562167
pinned_cursors = []
2168+
assert self._topology is not None
21572169

21582170
# Other threads or the GC may append to the queue concurrently.
21592171
while True:
@@ -2195,6 +2207,7 @@ async def _process_periodic_tasks(self) -> None:
21952207
"""Process any pending kill cursors requests and
21962208
maintain connection pool parameters.
21972209
"""
2210+
assert self._topology is not None
21982211
try:
21992212
await self._process_kill_cursors()
22002213
await self._topology.update_pool()
@@ -2210,6 +2223,7 @@ def _return_server_session(
22102223
"""Internal: return a _ServerSession to the pool."""
22112224
if isinstance(server_session, _EmptyServerSession):
22122225
return None
2226+
assert self._topology is not None
22132227
return self._topology.return_server_session(server_session)
22142228

22152229
@contextlib.asynccontextmanager
@@ -2247,6 +2261,7 @@ async def _tmp_session(
22472261
async def _process_response(
22482262
self, reply: Mapping[str, Any], session: Optional[AsyncClientSession]
22492263
) -> None:
2264+
assert self._topology is not None
22502265
await self._topology.receive_cluster_time(reply.get("$clusterTime"))
22512266
if session is not None:
22522267
session._process_response(reply)
@@ -2638,6 +2653,7 @@ async def handle(
26382653
self.completed_handshake,
26392654
self.service_id,
26402655
)
2656+
assert self._client._topology is not None
26412657
await self.client._topology.handle_error(self.server_address, err_ctx)
26422658

26432659
async def __aenter__(self) -> _MongoClientErrorHandler:

pymongo/asynchronous/pool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
13151315
raise
13161316

13171317
if handler:
1318+
assert handler.client._topology is not None
13181319
await handler.client._topology.receive_cluster_time(conn._cluster_time)
13191320

13201321
return conn

pymongo/synchronous/client_session.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,7 @@ def _txn_read_preference(self) -> Optional[_ServerMode]:
991991
def _materialize(self, logical_session_timeout_minutes: Optional[int] = None) -> None:
992992
if isinstance(self._server_session, _EmptyServerSession):
993993
old = self._server_session
994+
assert self._client._topology is not None
994995
self._server_session = self._client._topology.get_server_session(
995996
logical_session_timeout_minutes
996997
)

pymongo/synchronous/mongo_client.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +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
758+
self._topology: Optional[Topology] = None
759759

760760
# _pool_class, _monitor_class, and _condition_class are for deep
761761
# customization of PyMongo, e.g. Motor.
@@ -1034,6 +1034,7 @@ def _should_pin_cursor(self, session: Optional[ClientSession]) -> Optional[bool]
10341034

10351035
def _after_fork(self) -> None:
10361036
"""Resets topology in a child after successfully forking."""
1037+
assert self._topology is not None
10371038
self._init_background(self._topology._pid)
10381039
# Reset the session pool to avoid duplicate sessions in the child process.
10391040
self._topology._session_pool.reset()
@@ -1193,6 +1194,7 @@ def topology_description(self) -> TopologyDescription:
11931194
11941195
.. versionadded:: 4.0
11951196
"""
1197+
assert self._topology is not None
11961198
return self._topology.description
11971199

11981200
@property
@@ -1206,6 +1208,7 @@ def nodes(self) -> FrozenSet[_Address]:
12061208
to any servers, or a network partition causes it to lose connection
12071209
to all servers.
12081210
"""
1211+
assert self._topology is not None
12091212
description = self._topology.description
12101213
return frozenset(s.address for s in description.known_servers)
12111214

@@ -1219,7 +1222,7 @@ def options(self) -> ClientOptions:
12191222
"""
12201223
return self._options
12211224

1222-
def eq_props(self):
1225+
def eq_props(self) -> tuple[tuple[_Address, ...], Optional[str], Optional[str], str]:
12231226
return (
12241227
tuple(sorted(self._resolve_srv_info["seeds"])),
12251228
self._options.replica_set_name,
@@ -1240,7 +1243,7 @@ def __ne__(self, other: Any) -> bool:
12401243

12411244
def __hash__(self) -> int:
12421245
if self._topology is None:
1243-
raise hash(self.eq_props())
1246+
return hash(self.eq_props())
12441247
else:
12451248
return hash(self._topology)
12461249

@@ -1383,6 +1386,7 @@ def _ensure_session(self, session: Optional[ClientSession] = None) -> Optional[C
13831386
def _send_cluster_time(
13841387
self, command: MutableMapping[str, Any], session: Optional[ClientSession]
13851388
) -> None:
1389+
assert self._topology is not None
13861390
topology_time = self._topology.max_cluster_time()
13871391
session_time = session.cluster_time if session else None
13881392
if topology_time and session_time:
@@ -1566,6 +1570,7 @@ def address(self) -> Optional[tuple[str, int]]:
15661570
15671571
.. versionadded:: 3.0
15681572
"""
1573+
assert self._topology is not None
15691574
topology_type = self._topology._description.topology_type
15701575
if (
15711576
topology_type == TOPOLOGY_TYPE.Sharded
@@ -1588,6 +1593,7 @@ def primary(self) -> Optional[tuple[str, int]]:
15881593
.. versionadded:: 3.0
15891594
MongoClient gained this property in version 3.0.
15901595
"""
1596+
assert self._topology is not None
15911597
return self._topology.get_primary() # type: ignore[return-value]
15921598

15931599
@property
@@ -1601,6 +1607,7 @@ def secondaries(self) -> set[_Address]:
16011607
.. versionadded:: 3.0
16021608
MongoClient gained this property in version 3.0.
16031609
"""
1610+
assert self._topology is not None
16041611
return self._topology.get_secondaries()
16051612

16061613
@property
@@ -1611,6 +1618,7 @@ def arbiters(self) -> set[_Address]:
16111618
connected to a replica set, there are no arbiters, or this client was
16121619
created without the `replicaSet` option.
16131620
"""
1621+
assert self._topology is not None
16141622
return self._topology.get_arbiters()
16151623

16161624
@property
@@ -1703,10 +1711,12 @@ def _get_topology(self) -> Topology:
17031711
if self._resolve_srv_info["is_srv"]:
17041712
self._resolve_srv()
17051713
self._init_background()
1714+
assert self._topology is not None
17061715
self._topology.open()
17071716
with self._lock:
17081717
self._kill_cursors_executor.open()
17091718
self._opened = True
1719+
assert self._topology is not None
17101720
return self._topology
17111721

17121722
@contextlib.contextmanager
@@ -1809,6 +1819,7 @@ def _conn_from_server(
18091819
# Thread safe: if the type is single it cannot change.
18101820
# NOTE: We already opened the Topology when selecting a server so there's no need
18111821
# to call _get_topology() again.
1822+
assert self._topology is not None
18121823
single = self._topology.description.topology_type == TOPOLOGY_TYPE.Single
18131824
with self._checkout(server, session) as conn:
18141825
if single:
@@ -2148,6 +2159,7 @@ def _process_kill_cursors(self) -> None:
21482159
"""Process any pending kill cursors requests."""
21492160
address_to_cursor_ids = defaultdict(list)
21502161
pinned_cursors = []
2162+
assert self._topology is not None
21512163

21522164
# Other threads or the GC may append to the queue concurrently.
21532165
while True:
@@ -2189,6 +2201,7 @@ def _process_periodic_tasks(self) -> None:
21892201
"""Process any pending kill cursors requests and
21902202
maintain connection pool parameters.
21912203
"""
2204+
assert self._topology is not None
21922205
try:
21932206
self._process_kill_cursors()
21942207
self._topology.update_pool()
@@ -2204,6 +2217,7 @@ def _return_server_session(
22042217
"""Internal: return a _ServerSession to the pool."""
22052218
if isinstance(server_session, _EmptyServerSession):
22062219
return None
2220+
assert self._topology is not None
22072221
return self._topology.return_server_session(server_session)
22082222

22092223
@contextlib.contextmanager
@@ -2239,6 +2253,7 @@ def _tmp_session(
22392253
yield None
22402254

22412255
def _process_response(self, reply: Mapping[str, Any], session: Optional[ClientSession]) -> None:
2256+
assert self._topology is not None
22422257
self._topology.receive_cluster_time(reply.get("$clusterTime"))
22432258
if session is not None:
22442259
session._process_response(reply)
@@ -2624,6 +2639,7 @@ def handle(
26242639
self.completed_handshake,
26252640
self.service_id,
26262641
)
2642+
assert self._client._topology is not None
26272643
self.client._topology.handle_error(self.server_address, err_ctx)
26282644

26292645
def __enter__(self) -> _MongoClientErrorHandler:

pymongo/synchronous/pool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,7 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
13091309
raise
13101310

13111311
if handler:
1312+
assert handler.client._topology is not None
13121313
handler.client._topology.receive_cluster_time(conn._cluster_time)
13131314

13141315
return conn

0 commit comments

Comments
 (0)