Skip to content

Commit ed25867

Browse files
committed
fix typing
1 parent ed50141 commit ed25867

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -742,16 +742,16 @@ def __init__(
742742
**kwargs,
743743
}
744744

745+
if host is None:
746+
host = self.HOST
747+
if isinstance(host, str):
748+
host = [host]
749+
if port is None:
750+
port = self.PORT
751+
if not isinstance(port, int):
752+
raise TypeError(f"port must be an instance of int, not {type(port)}")
745753
self._host = host
746754
self._port = port
747-
if self._host is None:
748-
self._host = self.HOST
749-
if isinstance(self._host, str):
750-
self._host = [self._host]
751-
if self._port is None:
752-
self._port = self.PORT
753-
if not isinstance(self._port, int):
754-
raise TypeError(f"port must be an instance of int, not {type(port)}")
755755

756756
# _pool_class, _monitor_class, and _condition_class are for deep
757757
# customization of PyMongo, e.g. Motor.
@@ -762,7 +762,7 @@ def __init__(
762762
# Parse options passed as kwargs.
763763
keyword_opts = common._CaseInsensitiveDictionary(kwargs)
764764
keyword_opts["document_class"] = doc_class
765-
self._resolve_uri_info = {"keyword_opts": keyword_opts}
765+
self._resolve_uri_info: dict[str, Any] = {"keyword_opts": keyword_opts}
766766

767767
seeds = set()
768768
username = None
@@ -824,6 +824,8 @@ def __init__(
824824
opts = self._normalize_and_validate_options(opts, seeds)
825825

826826
# Username and password passed as kwargs override user info in URI.
827+
username = opts.get("username", username)
828+
password = opts.get("password", password)
827829
self._options = options = ClientOptions(username, password, dbase, opts, _IS_SYNC)
828830

829831
self._default_database_name = dbase
@@ -887,15 +889,7 @@ def __init__(
887889
# This will be used later if we fork.
888890
AsyncMongoClient._clients[self._topology._topology_id] = self
889891

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-
898-
def _resolve_uri(self):
892+
def _resolve_uri(self) -> None:
899893
keyword_opts = self._resolve_uri_info["keyword_opts"]
900894
seeds = set()
901895
opts = common._CaseInsensitiveDictionary()
@@ -953,7 +947,7 @@ def _resolve_uri(self):
953947
srv_service_name = opts.get("srvServiceName", common.SRV_SERVICE_NAME)
954948

955949
srv_max_hosts = srv_max_hosts or opts.get("srvmaxhosts")
956-
opts = self._normalize_and_validate_opts(opts, seeds)
950+
opts = self._normalize_and_validate_options(opts, seeds)
957951

958952
# Username and password passed as kwargs override user info in URI.
959953
username = opts.get("username", self._resolve_uri_info["username"])
@@ -991,15 +985,21 @@ def _resolve_uri(self):
991985

992986
self._topology = Topology(self._topology_settings)
993987

994-
def _normalize_and_validate_opts(self, opts, seeds):
988+
def _normalize_and_validate_options(
989+
self, opts: common._CaseInsensitiveDictionary, seeds: set[tuple[str, int | None]]
990+
) -> common._CaseInsensitiveDictionary:
995991
# Handle security-option conflicts in combined options.
996992
opts = _handle_security_options(opts)
997993
# Normalize combined options.
998994
opts = _normalize_options(opts)
999995
_check_options(seeds, opts)
1000996
return opts
1001997

1002-
def _validate_kwargs_and_update_opts(self, keyword_opts, opts):
998+
def _validate_kwargs_and_update_opts(
999+
self,
1000+
keyword_opts: common._CaseInsensitiveDictionary,
1001+
opts: common._CaseInsensitiveDictionary,
1002+
) -> common._CaseInsensitiveDictionary:
10031003
# Handle deprecated options in kwarg options.
10041004
keyword_opts = _handle_option_deprecations(keyword_opts)
10051005
# Validate kwarg options.

pymongo/synchronous/mongo_client.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,16 @@ def __init__(
740740
**kwargs,
741741
}
742742

743+
if host is None:
744+
host = self.HOST
745+
if isinstance(host, str):
746+
host = [host]
747+
if port is None:
748+
port = self.PORT
749+
if not isinstance(port, int):
750+
raise TypeError(f"port must be an instance of int, not {type(port)}")
743751
self._host = host
744752
self._port = port
745-
if self._host is None:
746-
self._host = self.HOST
747-
if isinstance(self._host, str):
748-
self._host = [self._host]
749-
if self._port is None:
750-
self._port = self.PORT
751-
if not isinstance(self._port, int):
752-
raise TypeError(f"port must be an instance of int, not {type(port)}")
753753

754754
# _pool_class, _monitor_class, and _condition_class are for deep
755755
# customization of PyMongo, e.g. Motor.
@@ -760,7 +760,7 @@ def __init__(
760760
# Parse options passed as kwargs.
761761
keyword_opts = common._CaseInsensitiveDictionary(kwargs)
762762
keyword_opts["document_class"] = doc_class
763-
self._resolve_uri_info = {"keyword_opts": keyword_opts}
763+
self._resolve_uri_info: dict[str, Any] = {"keyword_opts": keyword_opts}
764764

765765
seeds = set()
766766
username = None
@@ -822,6 +822,8 @@ def __init__(
822822
opts = self._normalize_and_validate_options(opts, seeds)
823823

824824
# Username and password passed as kwargs override user info in URI.
825+
username = opts.get("username", username)
826+
password = opts.get("password", password)
825827
self._options = options = ClientOptions(username, password, dbase, opts, _IS_SYNC)
826828

827829
self._default_database_name = dbase
@@ -885,15 +887,7 @@ def __init__(
885887
# This will be used later if we fork.
886888
MongoClient._clients[self._topology._topology_id] = self
887889

888-
def _normalize_and_validate_options(self, opts, seeds):
889-
# Handle security-option conflicts in combined options.
890-
opts = _handle_security_options(opts)
891-
# Normalize combined options.
892-
opts = _normalize_options(opts)
893-
_check_options(seeds, opts)
894-
return opts
895-
896-
def _resolve_uri(self):
890+
def _resolve_uri(self) -> None:
897891
keyword_opts = self._resolve_uri_info["keyword_opts"]
898892
seeds = set()
899893
opts = common._CaseInsensitiveDictionary()
@@ -951,7 +945,7 @@ def _resolve_uri(self):
951945
srv_service_name = opts.get("srvServiceName", common.SRV_SERVICE_NAME)
952946

953947
srv_max_hosts = srv_max_hosts or opts.get("srvmaxhosts")
954-
opts = self._normalize_and_validate_opts(opts, seeds)
948+
opts = self._normalize_and_validate_options(opts, seeds)
955949

956950
# Username and password passed as kwargs override user info in URI.
957951
username = opts.get("username", self._resolve_uri_info["username"])
@@ -989,15 +983,21 @@ def _resolve_uri(self):
989983

990984
self._topology = Topology(self._topology_settings)
991985

992-
def _normalize_and_validate_opts(self, opts, seeds):
986+
def _normalize_and_validate_options(
987+
self, opts: common._CaseInsensitiveDictionary, seeds: set[tuple[str, int | None]]
988+
) -> common._CaseInsensitiveDictionary:
993989
# Handle security-option conflicts in combined options.
994990
opts = _handle_security_options(opts)
995991
# Normalize combined options.
996992
opts = _normalize_options(opts)
997993
_check_options(seeds, opts)
998994
return opts
999995

1000-
def _validate_kwargs_and_update_opts(self, keyword_opts, opts):
996+
def _validate_kwargs_and_update_opts(
997+
self,
998+
keyword_opts: common._CaseInsensitiveDictionary,
999+
opts: common._CaseInsensitiveDictionary,
1000+
) -> common._CaseInsensitiveDictionary:
10011001
# Handle deprecated options in kwarg options.
10021002
keyword_opts = _handle_option_deprecations(keyword_opts)
10031003
# Validate kwarg options.

pymongo/uri_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def _validate_uri(
507507
warn: bool = False,
508508
normalize: bool = True,
509509
srv_max_hosts: Optional[int] = None,
510-
):
510+
) -> dict[str, Any]:
511511
if uri.startswith(SCHEME):
512512
is_srv = False
513513
scheme_free = uri[SCHEME_LEN:]

0 commit comments

Comments
 (0)