109109)
110110from pymongo .read_preferences import ReadPreference , _ServerMode
111111from pymongo .results import ClientBulkWriteResult
112+ from pymongo .server_description import ServerDescription
112113from pymongo .server_selectors import writable_server_selector
113114from pymongo .server_type import SERVER_TYPE
114115from pymongo .topology_description import TOPOLOGY_TYPE , TopologyDescription
@@ -779,7 +780,7 @@ def __init__(
779780 keyword_opts ["document_class" ] = doc_class
780781 self ._resolve_srv_info : dict [str , Any ] = {"keyword_opts" : keyword_opts }
781782
782- seeds = set ()
783+ self . _seeds = set ()
783784 is_srv = False
784785 username = None
785786 password = None
@@ -804,18 +805,18 @@ def __init__(
804805 srv_max_hosts = srv_max_hosts ,
805806 )
806807 is_srv = entity .startswith (SRV_SCHEME )
807- seeds .update (res ["nodelist" ])
808+ self . _seeds .update (res ["nodelist" ])
808809 username = res ["username" ] or username
809810 password = res ["password" ] or password
810811 dbase = res ["database" ] or dbase
811812 opts = res ["options" ]
812813 fqdn = res ["fqdn" ]
813814 else :
814- seeds .update (split_hosts (entity , self ._port ))
815- if not seeds :
815+ self . _seeds .update (split_hosts (entity , self ._port ))
816+ if not self . _seeds :
816817 raise ConfigurationError ("need to specify at least one host" )
817818
818- for hostname in [node [0 ] for node in seeds ]:
819+ for hostname in [node [0 ] for node in self . _seeds ]:
819820 if _detect_external_db (hostname ):
820821 break
821822
@@ -838,7 +839,7 @@ def __init__(
838839 srv_service_name = opts .get ("srvServiceName" , common .SRV_SERVICE_NAME )
839840
840841 srv_max_hosts = srv_max_hosts or opts .get ("srvmaxhosts" )
841- opts = self ._normalize_and_validate_options (opts , seeds )
842+ opts = self ._normalize_and_validate_options (opts , self . _seeds )
842843
843844 # Username and password passed as kwargs override user info in URI.
844845 username = opts .get ("username" , username )
@@ -857,7 +858,7 @@ def __init__(
857858 "username" : username ,
858859 "password" : password ,
859860 "dbase" : dbase ,
860- "seeds" : seeds ,
861+ "seeds" : self . _seeds ,
861862 "fqdn" : fqdn ,
862863 "srv_service_name" : srv_service_name ,
863864 "pool_class" : pool_class ,
@@ -873,8 +874,7 @@ def __init__(
873874 self ._options .read_concern ,
874875 )
875876
876- if not is_srv :
877- self ._init_based_on_options (seeds , srv_max_hosts , srv_service_name )
877+ self ._init_based_on_options (self ._seeds , srv_max_hosts , srv_service_name )
878878
879879 self ._opened = False
880880 self ._closed = False
@@ -975,6 +975,7 @@ def _init_based_on_options(
975975 srv_service_name = srv_service_name ,
976976 srv_max_hosts = srv_max_hosts ,
977977 server_monitoring_mode = self ._options .server_monitoring_mode ,
978+ topology_id = self ._topology_settings ._topology_id if self ._topology_settings else None ,
978979 )
979980 if self ._options .auto_encryption_opts :
980981 from pymongo .asynchronous .encryption import _Encrypter
@@ -1205,6 +1206,16 @@ def topology_description(self) -> TopologyDescription:
12051206
12061207 .. versionadded:: 4.0
12071208 """
1209+ if self ._topology is None :
1210+ servers = {(host , port ): ServerDescription ((host , port )) for host , port in self ._seeds }
1211+ return TopologyDescription (
1212+ TOPOLOGY_TYPE .Unknown ,
1213+ servers ,
1214+ None ,
1215+ None ,
1216+ None ,
1217+ self ._topology_settings ,
1218+ )
12081219 return self ._topology .description
12091220
12101221 @property
@@ -1218,6 +1229,8 @@ def nodes(self) -> FrozenSet[_Address]:
12181229 to any servers, or a network partition causes it to lose connection
12191230 to all servers.
12201231 """
1232+ if self ._topology is None :
1233+ return frozenset ()
12211234 description = self ._topology .description
12221235 return frozenset (s .address for s in description .known_servers )
12231236
@@ -1576,6 +1589,8 @@ async def address(self) -> Optional[tuple[str, int]]:
15761589
15771590 .. versionadded:: 3.0
15781591 """
1592+ if self ._topology is None :
1593+ await self ._get_topology ()
15791594 topology_type = self ._topology ._description .topology_type
15801595 if (
15811596 topology_type == TOPOLOGY_TYPE .Sharded
@@ -1598,6 +1613,8 @@ async def primary(self) -> Optional[tuple[str, int]]:
15981613 .. versionadded:: 3.0
15991614 AsyncMongoClient gained this property in version 3.0.
16001615 """
1616+ if self ._topology is None :
1617+ await self ._get_topology ()
16011618 return await self ._topology .get_primary () # type: ignore[return-value]
16021619
16031620 @property
@@ -1611,6 +1628,8 @@ async def secondaries(self) -> set[_Address]:
16111628 .. versionadded:: 3.0
16121629 AsyncMongoClient gained this property in version 3.0.
16131630 """
1631+ if self ._topology is None :
1632+ await self ._get_topology ()
16141633 return await self ._topology .get_secondaries ()
16151634
16161635 @property
@@ -1621,6 +1640,8 @@ async def arbiters(self) -> set[_Address]:
16211640 connected to a replica set, there are no arbiters, or this client was
16221641 created without the `replicaSet` option.
16231642 """
1643+ if self ._topology is None :
1644+ await self ._get_topology ()
16241645 return await self ._topology .get_arbiters ()
16251646
16261647 @property
0 commit comments