109
109
)
110
110
from pymongo .read_preferences import ReadPreference , _ServerMode
111
111
from pymongo .results import ClientBulkWriteResult
112
+ from pymongo .server_description import ServerDescription
112
113
from pymongo .server_selectors import writable_server_selector
113
114
from pymongo .server_type import SERVER_TYPE
114
115
from pymongo .topology_description import TOPOLOGY_TYPE , TopologyDescription
@@ -779,7 +780,7 @@ def __init__(
779
780
keyword_opts ["document_class" ] = doc_class
780
781
self ._resolve_srv_info : dict [str , Any ] = {"keyword_opts" : keyword_opts }
781
782
782
- seeds = set ()
783
+ self . _seeds = set ()
783
784
is_srv = False
784
785
username = None
785
786
password = None
@@ -804,18 +805,18 @@ def __init__(
804
805
srv_max_hosts = srv_max_hosts ,
805
806
)
806
807
is_srv = entity .startswith (SRV_SCHEME )
807
- seeds .update (res ["nodelist" ])
808
+ self . _seeds .update (res ["nodelist" ])
808
809
username = res ["username" ] or username
809
810
password = res ["password" ] or password
810
811
dbase = res ["database" ] or dbase
811
812
opts = res ["options" ]
812
813
fqdn = res ["fqdn" ]
813
814
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 :
816
817
raise ConfigurationError ("need to specify at least one host" )
817
818
818
- for hostname in [node [0 ] for node in seeds ]:
819
+ for hostname in [node [0 ] for node in self . _seeds ]:
819
820
if _detect_external_db (hostname ):
820
821
break
821
822
@@ -838,7 +839,7 @@ def __init__(
838
839
srv_service_name = opts .get ("srvServiceName" , common .SRV_SERVICE_NAME )
839
840
840
841
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 )
842
843
843
844
# Username and password passed as kwargs override user info in URI.
844
845
username = opts .get ("username" , username )
@@ -857,7 +858,7 @@ def __init__(
857
858
"username" : username ,
858
859
"password" : password ,
859
860
"dbase" : dbase ,
860
- "seeds" : seeds ,
861
+ "seeds" : self . _seeds ,
861
862
"fqdn" : fqdn ,
862
863
"srv_service_name" : srv_service_name ,
863
864
"pool_class" : pool_class ,
@@ -873,8 +874,7 @@ def __init__(
873
874
self ._options .read_concern ,
874
875
)
875
876
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 )
878
878
879
879
self ._opened = False
880
880
self ._closed = False
@@ -975,6 +975,7 @@ def _init_based_on_options(
975
975
srv_service_name = srv_service_name ,
976
976
srv_max_hosts = srv_max_hosts ,
977
977
server_monitoring_mode = self ._options .server_monitoring_mode ,
978
+ topology_id = self ._topology_settings ._topology_id if self ._topology_settings else None ,
978
979
)
979
980
if self ._options .auto_encryption_opts :
980
981
from pymongo .asynchronous .encryption import _Encrypter
@@ -1205,6 +1206,16 @@ def topology_description(self) -> TopologyDescription:
1205
1206
1206
1207
.. versionadded:: 4.0
1207
1208
"""
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
+ )
1208
1219
return self ._topology .description
1209
1220
1210
1221
@property
@@ -1218,6 +1229,8 @@ def nodes(self) -> FrozenSet[_Address]:
1218
1229
to any servers, or a network partition causes it to lose connection
1219
1230
to all servers.
1220
1231
"""
1232
+ if self ._topology is None :
1233
+ return frozenset ()
1221
1234
description = self ._topology .description
1222
1235
return frozenset (s .address for s in description .known_servers )
1223
1236
@@ -1576,6 +1589,8 @@ async def address(self) -> Optional[tuple[str, int]]:
1576
1589
1577
1590
.. versionadded:: 3.0
1578
1591
"""
1592
+ if self ._topology is None :
1593
+ await self ._get_topology ()
1579
1594
topology_type = self ._topology ._description .topology_type
1580
1595
if (
1581
1596
topology_type == TOPOLOGY_TYPE .Sharded
@@ -1598,6 +1613,8 @@ async def primary(self) -> Optional[tuple[str, int]]:
1598
1613
.. versionadded:: 3.0
1599
1614
AsyncMongoClient gained this property in version 3.0.
1600
1615
"""
1616
+ if self ._topology is None :
1617
+ await self ._get_topology ()
1601
1618
return await self ._topology .get_primary () # type: ignore[return-value]
1602
1619
1603
1620
@property
@@ -1611,6 +1628,8 @@ async def secondaries(self) -> set[_Address]:
1611
1628
.. versionadded:: 3.0
1612
1629
AsyncMongoClient gained this property in version 3.0.
1613
1630
"""
1631
+ if self ._topology is None :
1632
+ await self ._get_topology ()
1614
1633
return await self ._topology .get_secondaries ()
1615
1634
1616
1635
@property
@@ -1621,6 +1640,8 @@ async def arbiters(self) -> set[_Address]:
1621
1640
connected to a replica set, there are no arbiters, or this client was
1622
1641
created without the `replicaSet` option.
1623
1642
"""
1643
+ if self ._topology is None :
1644
+ await self ._get_topology ()
1624
1645
return await self ._topology .get_arbiters ()
1625
1646
1626
1647
@property
0 commit comments