@@ -762,6 +762,7 @@ def __init__(
762
762
# Parse options passed as kwargs.
763
763
keyword_opts = common ._CaseInsensitiveDictionary (kwargs )
764
764
keyword_opts ["document_class" ] = doc_class
765
+ self ._resolve_uri_info = {"keyword_opts" : keyword_opts }
765
766
766
767
seeds = set ()
767
768
username = None
@@ -814,25 +815,13 @@ def __init__(
814
815
keyword_opts ["tz_aware" ] = tz_aware
815
816
keyword_opts ["connect" ] = connect
816
817
817
- # Handle deprecated options in kwarg options.
818
- keyword_opts = _handle_option_deprecations (keyword_opts )
819
- # Validate kwarg options.
820
- keyword_opts = common ._CaseInsensitiveDictionary (
821
- dict (common .validate (keyword_opts .cased_key (k ), v ) for k , v in keyword_opts .items ())
822
- )
823
-
824
- # Override connection string options with kwarg options.
825
- opts .update (keyword_opts )
818
+ opts = self ._validate_kwargs_and_update_opts (keyword_opts , opts )
826
819
827
820
if srv_service_name is None :
828
821
srv_service_name = opts .get ("srvServiceName" , common .SRV_SERVICE_NAME )
829
822
830
823
srv_max_hosts = srv_max_hosts or opts .get ("srvmaxhosts" )
831
- # Handle security-option conflicts in combined options.
832
- opts = _handle_security_options (opts )
833
- # Normalize combined options.
834
- opts = _normalize_options (opts )
835
- _check_options (seeds , opts )
824
+ opts = self ._normalize_and_validate_options (opts , seeds )
836
825
837
826
# Username and password passed as kwargs override user info in URI.
838
827
self ._options = options = ClientOptions (username , password , dbase , opts , _IS_SYNC )
@@ -872,15 +861,17 @@ def __init__(
872
861
self ._closed = False
873
862
self ._init_background ()
874
863
875
- self ._for_resolve_uri = {
876
- "username" : username ,
877
- "password" : password ,
878
- "dbase" : dbase ,
879
- "fqdn" : fqdn ,
880
- "pool_class" : pool_class ,
881
- "monitor_class" : monitor_class ,
882
- "condition_class" : condition_class ,
883
- }
864
+ self ._resolve_uri_info .update (
865
+ {
866
+ "username" : username ,
867
+ "password" : password ,
868
+ "dbase" : dbase ,
869
+ "fqdn" : fqdn ,
870
+ "pool_class" : pool_class ,
871
+ "monitor_class" : monitor_class ,
872
+ "condition_class" : condition_class ,
873
+ }
874
+ )
884
875
if _IS_SYNC and connect :
885
876
self ._get_topology () # type: ignore[unused-coroutine]
886
877
@@ -896,17 +887,16 @@ def __init__(
896
887
# This will be used later if we fork.
897
888
AsyncMongoClient ._clients [self ._topology ._topology_id ] = self
898
889
890
+ def _normalize_and_validate_options (self , opts , seeds ):
891
+ # Handle security-option conflicts in combined options.
892
+ opts = _handle_security_options (opts )
893
+ # Normalize combined options.
894
+ opts = _normalize_options (opts )
895
+ _check_options (seeds , opts )
896
+ return opts
897
+
899
898
def _resolve_uri (self ):
900
- keyword_opts = common ._CaseInsensitiveDictionary (self ._init_kwargs )
901
- for i in [
902
- "_pool_class" ,
903
- "_monitor_class" ,
904
- "_condition_class" ,
905
- "host" ,
906
- "port" ,
907
- "type_registry" ,
908
- ]:
909
- keyword_opts .pop (i , None )
899
+ keyword_opts = self ._resolve_uri_info ["keyword_opts" ]
910
900
seeds = set ()
911
901
opts = common ._CaseInsensitiveDictionary ()
912
902
srv_service_name = keyword_opts .get ("srvservicename" )
@@ -957,31 +947,19 @@ def _resolve_uri(self):
957
947
keyword_opts ["tz_aware" ] = tz_aware
958
948
keyword_opts ["connect" ] = connect
959
949
960
- # Handle deprecated options in kwarg options.
961
- keyword_opts = _handle_option_deprecations (keyword_opts )
962
- # Validate kwarg options.
963
- keyword_opts = common ._CaseInsensitiveDictionary (
964
- dict (common .validate (keyword_opts .cased_key (k ), v ) for k , v in keyword_opts .items ())
965
- )
966
-
967
- # Override connection string options with kwarg options.
968
- opts .update (keyword_opts )
950
+ opts = self ._validate_kwargs_and_update_opts (keyword_opts , opts )
969
951
970
952
if srv_service_name is None :
971
953
srv_service_name = opts .get ("srvServiceName" , common .SRV_SERVICE_NAME )
972
954
973
955
srv_max_hosts = srv_max_hosts or opts .get ("srvmaxhosts" )
974
- # Handle security-option conflicts in combined options.
975
- opts = _handle_security_options (opts )
976
- # Normalize combined options.
977
- opts = _normalize_options (opts )
978
- _check_options (seeds , opts )
956
+ opts = self ._normalize_and_validate_opts (opts , seeds )
979
957
980
958
# Username and password passed as kwargs override user info in URI.
981
- username = opts .get ("username" , self ._for_resolve_uri ["username" ])
982
- password = opts .get ("password" , self ._for_resolve_uri ["password" ])
959
+ username = opts .get ("username" , self ._resolve_uri_info ["username" ])
960
+ password = opts .get ("password" , self ._resolve_uri_info ["password" ])
983
961
self ._options = ClientOptions (
984
- username , password , self ._for_resolve_uri ["dbase" ], opts , _IS_SYNC
962
+ username , password , self ._resolve_uri_info ["dbase" ], opts , _IS_SYNC
985
963
)
986
964
987
965
self ._event_listeners = self ._options .pool_options ._event_listeners
@@ -995,15 +973,15 @@ def _resolve_uri(self):
995
973
self ._topology_settings = TopologySettings (
996
974
seeds = seeds ,
997
975
replica_set_name = self ._options .replica_set_name ,
998
- pool_class = self ._for_resolve_uri ["pool_class" ],
976
+ pool_class = self ._resolve_uri_info ["pool_class" ],
999
977
pool_options = self ._options .pool_options ,
1000
- monitor_class = self ._for_resolve_uri ["monitor_class" ],
1001
- condition_class = self ._for_resolve_uri ["condition_class" ],
978
+ monitor_class = self ._resolve_uri_info ["monitor_class" ],
979
+ condition_class = self ._resolve_uri_info ["condition_class" ],
1002
980
local_threshold_ms = self ._options .local_threshold_ms ,
1003
981
server_selection_timeout = self ._options .server_selection_timeout ,
1004
982
server_selector = self ._options .server_selector ,
1005
983
heartbeat_frequency = self ._options .heartbeat_frequency ,
1006
- fqdn = self ._for_resolve_uri ["fqdn" ],
984
+ fqdn = self ._resolve_uri_info ["fqdn" ],
1007
985
direct_connection = self ._options .direct_connection ,
1008
986
load_balanced = self ._options .load_balanced ,
1009
987
srv_service_name = srv_service_name ,
@@ -1013,6 +991,25 @@ def _resolve_uri(self):
1013
991
1014
992
self ._topology = Topology (self ._topology_settings )
1015
993
994
+ def _normalize_and_validate_opts (self , opts , seeds ):
995
+ # Handle security-option conflicts in combined options.
996
+ opts = _handle_security_options (opts )
997
+ # Normalize combined options.
998
+ opts = _normalize_options (opts )
999
+ _check_options (seeds , opts )
1000
+ return opts
1001
+
1002
+ def _validate_kwargs_and_update_opts (self , keyword_opts , opts ):
1003
+ # Handle deprecated options in kwarg options.
1004
+ keyword_opts = _handle_option_deprecations (keyword_opts )
1005
+ # Validate kwarg options.
1006
+ keyword_opts = common ._CaseInsensitiveDictionary (
1007
+ dict (common .validate (keyword_opts .cased_key (k ), v ) for k , v in keyword_opts .items ())
1008
+ )
1009
+ # Override connection string options with kwarg options.
1010
+ opts .update (keyword_opts )
1011
+ return opts
1012
+
1016
1013
async def aconnect (self ) -> None :
1017
1014
"""Explicitly connect to MongoDB asynchronously instead of on the first operation."""
1018
1015
await self ._get_topology ()
0 commit comments