@@ -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 :
0 commit comments