Skip to content

Commit cd9bd92

Browse files
committed
address comments pt1
1 parent 63676b6 commit cd9bd92

File tree

6 files changed

+42
-74
lines changed

6 files changed

+42
-74
lines changed

pymongo/asynchronous/client_session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,6 @@ 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
1000999
self._server_session = self._client._topology.get_server_session(
10011000
logical_session_timeout_minutes
10021001
)

pymongo/asynchronous/mongo_client.py

Lines changed: 21 additions & 35 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: Optional[Topology] = None
760+
self._topology: Topology = None # type: ignore[assignment]
761761

762762
# _pool_class, _monitor_class, and _condition_class are for deep
763763
# customization of PyMongo, e.g. Motor.
@@ -1036,7 +1036,6 @@ 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
10401039
self._init_background(self._topology._pid)
10411040
# Reset the session pool to avoid duplicate sessions in the child process.
10421041
self._topology._session_pool.reset()
@@ -1196,7 +1195,6 @@ def topology_description(self) -> TopologyDescription:
11961195
11971196
.. versionadded:: 4.0
11981197
"""
1199-
assert self._topology is not None
12001198
return self._topology.description
12011199

12021200
@property
@@ -1210,7 +1208,6 @@ def nodes(self) -> FrozenSet[_Address]:
12101208
to any servers, or a network partition causes it to lose connection
12111209
to all servers.
12121210
"""
1213-
assert self._topology is not None
12141211
description = self._topology.description
12151212
return frozenset(s.address for s in description.known_servers)
12161213

@@ -1384,7 +1381,6 @@ def _ensure_session(
13841381
def _send_cluster_time(
13851382
self, command: MutableMapping[str, Any], session: Optional[AsyncClientSession]
13861383
) -> None:
1387-
assert self._topology is not None
13881384
topology_time = self._topology.max_cluster_time()
13891385
session_time = session.cluster_time if session else None
13901386
if topology_time and session_time:
@@ -1570,7 +1566,6 @@ async def address(self) -> Optional[tuple[str, int]]:
15701566
15711567
.. versionadded:: 3.0
15721568
"""
1573-
assert self._topology is not None
15741569
topology_type = self._topology._description.topology_type
15751570
if (
15761571
topology_type == TOPOLOGY_TYPE.Sharded
@@ -1593,7 +1588,6 @@ async def primary(self) -> Optional[tuple[str, int]]:
15931588
.. versionadded:: 3.0
15941589
AsyncMongoClient gained this property in version 3.0.
15951590
"""
1596-
assert self._topology is not None
15971591
return await self._topology.get_primary() # type: ignore[return-value]
15981592

15991593
@property
@@ -1607,7 +1601,6 @@ async def secondaries(self) -> set[_Address]:
16071601
.. versionadded:: 3.0
16081602
AsyncMongoClient gained this property in version 3.0.
16091603
"""
1610-
assert self._topology is not None
16111604
return await self._topology.get_secondaries()
16121605

16131606
@property
@@ -1618,7 +1611,6 @@ async def arbiters(self) -> set[_Address]:
16181611
connected to a replica set, there are no arbiters, or this client was
16191612
created without the `replicaSet` option.
16201613
"""
1621-
assert self._topology is not None
16221614
return await self._topology.get_arbiters()
16231615

16241616
@property
@@ -1677,25 +1669,26 @@ async def close(self) -> None:
16771669
.. versionchanged:: 3.6
16781670
End all server sessions created by this client.
16791671
"""
1680-
if self._topology is not None:
1681-
session_ids = self._topology.pop_all_sessions()
1682-
if session_ids:
1683-
await self._end_sessions(session_ids)
1684-
# Stop the periodic task thread and then send pending killCursor
1685-
# requests before closing the topology.
1686-
self._kill_cursors_executor.close()
1687-
await self._process_kill_cursors()
1688-
await self._topology.close()
1689-
if self._encrypter:
1690-
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1691-
await self._encrypter.close()
1692-
self._closed = True
1693-
if not _IS_SYNC:
1694-
await asyncio.gather(
1695-
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1696-
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1697-
return_exceptions=True,
1698-
)
1672+
if self._topology is None:
1673+
return
1674+
session_ids = self._topology.pop_all_sessions()
1675+
if session_ids:
1676+
await self._end_sessions(session_ids)
1677+
# Stop the periodic task thread and then send pending killCursor
1678+
# requests before closing the topology.
1679+
self._kill_cursors_executor.close()
1680+
await self._process_kill_cursors()
1681+
await self._topology.close()
1682+
if self._encrypter:
1683+
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1684+
await self._encrypter.close()
1685+
self._closed = True
1686+
if not _IS_SYNC:
1687+
await asyncio.gather(
1688+
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1689+
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1690+
return_exceptions=True,
1691+
)
16991692

17001693
if not _IS_SYNC:
17011694
# Add support for contextlib.aclosing.
@@ -1711,12 +1704,10 @@ async def _get_topology(self) -> Topology:
17111704
if self._resolve_srv_info["is_srv"]:
17121705
await self._resolve_srv()
17131706
self._init_background()
1714-
assert self._topology is not None
17151707
await self._topology.open()
17161708
async with self._lock:
17171709
self._kill_cursors_executor.open()
17181710
self._opened = True
1719-
assert self._topology is not None
17201711
return self._topology
17211712

17221713
@contextlib.asynccontextmanager
@@ -1819,7 +1810,6 @@ async def _conn_from_server(
18191810
# Thread safe: if the type is single it cannot change.
18201811
# NOTE: We already opened the Topology when selecting a server so there's no need
18211812
# to call _get_topology() again.
1822-
assert self._topology is not None
18231813
single = self._topology.description.topology_type == TOPOLOGY_TYPE.Single
18241814
async with self._checkout(server, session) as conn:
18251815
if single:
@@ -2159,7 +2149,6 @@ async def _process_kill_cursors(self) -> None:
21592149
"""Process any pending kill cursors requests."""
21602150
address_to_cursor_ids = defaultdict(list)
21612151
pinned_cursors = []
2162-
assert self._topology is not None
21632152

21642153
# Other threads or the GC may append to the queue concurrently.
21652154
while True:
@@ -2201,7 +2190,6 @@ async def _process_periodic_tasks(self) -> None:
22012190
"""Process any pending kill cursors requests and
22022191
maintain connection pool parameters.
22032192
"""
2204-
assert self._topology is not None
22052193
try:
22062194
await self._process_kill_cursors()
22072195
await self._topology.update_pool()
@@ -2217,7 +2205,6 @@ def _return_server_session(
22172205
"""Internal: return a _ServerSession to the pool."""
22182206
if isinstance(server_session, _EmptyServerSession):
22192207
return None
2220-
assert self._topology is not None
22212208
return self._topology.return_server_session(server_session)
22222209

22232210
@contextlib.asynccontextmanager
@@ -2255,7 +2242,6 @@ async def _tmp_session(
22552242
async def _process_response(
22562243
self, reply: Mapping[str, Any], session: Optional[AsyncClientSession]
22572244
) -> None:
2258-
assert self._topology is not None
22592245
await self._topology.receive_cluster_time(reply.get("$clusterTime"))
22602246
if session is not None:
22612247
session._process_response(reply)

pymongo/asynchronous/pool.py

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

13171317
if handler:
1318-
assert handler.client._topology is not None
13191318
await handler.client._topology.receive_cluster_time(conn._cluster_time)
13201319

13211320
return conn

pymongo/synchronous/client_session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,6 @@ 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
995994
self._server_session = self._client._topology.get_server_session(
996995
logical_session_timeout_minutes
997996
)

pymongo/synchronous/mongo_client.py

Lines changed: 21 additions & 35 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: Optional[Topology] = None
758+
self._topology: Topology = None # type: ignore[assignment]
759759

760760
# _pool_class, _monitor_class, and _condition_class are for deep
761761
# customization of PyMongo, e.g. Motor.
@@ -1034,7 +1034,6 @@ 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
10381037
self._init_background(self._topology._pid)
10391038
# Reset the session pool to avoid duplicate sessions in the child process.
10401039
self._topology._session_pool.reset()
@@ -1194,7 +1193,6 @@ def topology_description(self) -> TopologyDescription:
11941193
11951194
.. versionadded:: 4.0
11961195
"""
1197-
assert self._topology is not None
11981196
return self._topology.description
11991197

12001198
@property
@@ -1208,7 +1206,6 @@ def nodes(self) -> FrozenSet[_Address]:
12081206
to any servers, or a network partition causes it to lose connection
12091207
to all servers.
12101208
"""
1211-
assert self._topology is not None
12121209
description = self._topology.description
12131210
return frozenset(s.address for s in description.known_servers)
12141211

@@ -1380,7 +1377,6 @@ def _ensure_session(self, session: Optional[ClientSession] = None) -> Optional[C
13801377
def _send_cluster_time(
13811378
self, command: MutableMapping[str, Any], session: Optional[ClientSession]
13821379
) -> None:
1383-
assert self._topology is not None
13841380
topology_time = self._topology.max_cluster_time()
13851381
session_time = session.cluster_time if session else None
13861382
if topology_time and session_time:
@@ -1564,7 +1560,6 @@ def address(self) -> Optional[tuple[str, int]]:
15641560
15651561
.. versionadded:: 3.0
15661562
"""
1567-
assert self._topology is not None
15681563
topology_type = self._topology._description.topology_type
15691564
if (
15701565
topology_type == TOPOLOGY_TYPE.Sharded
@@ -1587,7 +1582,6 @@ def primary(self) -> Optional[tuple[str, int]]:
15871582
.. versionadded:: 3.0
15881583
MongoClient gained this property in version 3.0.
15891584
"""
1590-
assert self._topology is not None
15911585
return self._topology.get_primary() # type: ignore[return-value]
15921586

15931587
@property
@@ -1601,7 +1595,6 @@ def secondaries(self) -> set[_Address]:
16011595
.. versionadded:: 3.0
16021596
MongoClient gained this property in version 3.0.
16031597
"""
1604-
assert self._topology is not None
16051598
return self._topology.get_secondaries()
16061599

16071600
@property
@@ -1612,7 +1605,6 @@ def arbiters(self) -> set[_Address]:
16121605
connected to a replica set, there are no arbiters, or this client was
16131606
created without the `replicaSet` option.
16141607
"""
1615-
assert self._topology is not None
16161608
return self._topology.get_arbiters()
16171609

16181610
@property
@@ -1671,25 +1663,26 @@ def close(self) -> None:
16711663
.. versionchanged:: 3.6
16721664
End all server sessions created by this client.
16731665
"""
1674-
if self._topology is not None:
1675-
session_ids = self._topology.pop_all_sessions()
1676-
if session_ids:
1677-
self._end_sessions(session_ids)
1678-
# Stop the periodic task thread and then send pending killCursor
1679-
# requests before closing the topology.
1680-
self._kill_cursors_executor.close()
1681-
self._process_kill_cursors()
1682-
self._topology.close()
1683-
if self._encrypter:
1684-
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1685-
self._encrypter.close()
1686-
self._closed = True
1687-
if not _IS_SYNC:
1688-
asyncio.gather(
1689-
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1690-
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1691-
return_exceptions=True,
1692-
)
1666+
if self._topology is None:
1667+
return
1668+
session_ids = self._topology.pop_all_sessions()
1669+
if session_ids:
1670+
self._end_sessions(session_ids)
1671+
# Stop the periodic task thread and then send pending killCursor
1672+
# requests before closing the topology.
1673+
self._kill_cursors_executor.close()
1674+
self._process_kill_cursors()
1675+
self._topology.close()
1676+
if self._encrypter:
1677+
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1678+
self._encrypter.close()
1679+
self._closed = True
1680+
if not _IS_SYNC:
1681+
asyncio.gather(
1682+
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1683+
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1684+
return_exceptions=True,
1685+
)
16931686

16941687
if not _IS_SYNC:
16951688
# Add support for contextlib.closing.
@@ -1705,12 +1698,10 @@ def _get_topology(self) -> Topology:
17051698
if self._resolve_srv_info["is_srv"]:
17061699
self._resolve_srv()
17071700
self._init_background()
1708-
assert self._topology is not None
17091701
self._topology.open()
17101702
with self._lock:
17111703
self._kill_cursors_executor.open()
17121704
self._opened = True
1713-
assert self._topology is not None
17141705
return self._topology
17151706

17161707
@contextlib.contextmanager
@@ -1813,7 +1804,6 @@ def _conn_from_server(
18131804
# Thread safe: if the type is single it cannot change.
18141805
# NOTE: We already opened the Topology when selecting a server so there's no need
18151806
# to call _get_topology() again.
1816-
assert self._topology is not None
18171807
single = self._topology.description.topology_type == TOPOLOGY_TYPE.Single
18181808
with self._checkout(server, session) as conn:
18191809
if single:
@@ -2153,7 +2143,6 @@ def _process_kill_cursors(self) -> None:
21532143
"""Process any pending kill cursors requests."""
21542144
address_to_cursor_ids = defaultdict(list)
21552145
pinned_cursors = []
2156-
assert self._topology is not None
21572146

21582147
# Other threads or the GC may append to the queue concurrently.
21592148
while True:
@@ -2195,7 +2184,6 @@ def _process_periodic_tasks(self) -> None:
21952184
"""Process any pending kill cursors requests and
21962185
maintain connection pool parameters.
21972186
"""
2198-
assert self._topology is not None
21992187
try:
22002188
self._process_kill_cursors()
22012189
self._topology.update_pool()
@@ -2211,7 +2199,6 @@ def _return_server_session(
22112199
"""Internal: return a _ServerSession to the pool."""
22122200
if isinstance(server_session, _EmptyServerSession):
22132201
return None
2214-
assert self._topology is not None
22152202
return self._topology.return_server_session(server_session)
22162203

22172204
@contextlib.contextmanager
@@ -2247,7 +2234,6 @@ def _tmp_session(
22472234
yield None
22482235

22492236
def _process_response(self, reply: Mapping[str, Any], session: Optional[ClientSession]) -> None:
2250-
assert self._topology is not None
22512237
self._topology.receive_cluster_time(reply.get("$clusterTime"))
22522238
if session is not None:
22532239
session._process_response(reply)

pymongo/synchronous/pool.py

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

13111311
if handler:
1312-
assert handler.client._topology is not None
13131312
handler.client._topology.receive_cluster_time(conn._cluster_time)
13141313

13151314
return conn

0 commit comments

Comments
 (0)