Skip to content

Commit 255ef77

Browse files
authored
PYTHON-3869 add types to uri_parser.py (#1338)
1 parent 02a3652 commit 255ef77

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

pymongo/uri_parser.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@
1414

1515

1616
"""Tools to parse and validate a MongoDB URI."""
17+
from __future__ import annotations
18+
1719
import re
1820
import sys
1921
import warnings
20-
from typing import Any, Dict, List, Mapping, MutableMapping, Optional, Tuple, Union
22+
from typing import (
23+
TYPE_CHECKING,
24+
Any,
25+
Dict,
26+
List,
27+
Mapping,
28+
MutableMapping,
29+
Optional,
30+
Sized,
31+
Tuple,
32+
Union,
33+
cast,
34+
)
2135
from urllib.parse import unquote_plus
2236

2337
from pymongo.client_options import _parse_ssl_options
@@ -32,14 +46,17 @@
3246
from pymongo.srv_resolver import _HAVE_DNSPYTHON, _SrvResolver
3347
from pymongo.typings import _Address
3448

49+
if TYPE_CHECKING:
50+
from pymongo.pyopenssl_context import SSLContext
51+
3552
SCHEME = "mongodb://"
3653
SCHEME_LEN = len(SCHEME)
3754
SRV_SCHEME = "mongodb+srv://"
3855
SRV_SCHEME_LEN = len(SRV_SCHEME)
3956
DEFAULT_PORT = 27017
4057

4158

42-
def _unquoted_percent(s):
59+
def _unquoted_percent(s: str) -> bool:
4360
"""Check for unescaped percent signs.
4461
4562
:Parameters:
@@ -152,7 +169,7 @@ def parse_host(entity: str, default_port: Optional[int] = DEFAULT_PORT) -> _Addr
152169
}
153170

154171

155-
def _parse_options(opts, delim):
172+
def _parse_options(opts: str, delim: Optional[str]) -> _CaseInsensitiveDictionary:
156173
"""Helper method for split_options which creates the options dict.
157174
Also handles the creation of a list for the URI tag_sets/
158175
readpreferencetags portion, and the use of a unicode options string.
@@ -174,7 +191,7 @@ def _parse_options(opts, delim):
174191
return options
175192

176193

177-
def _handle_security_options(options):
194+
def _handle_security_options(options: _CaseInsensitiveDictionary) -> _CaseInsensitiveDictionary:
178195
"""Raise appropriate errors when conflicting TLS options are present in
179196
the options dictionary.
180197
@@ -214,7 +231,7 @@ def _handle_security_options(options):
214231

215232
if "ssl" in options and "tls" in options:
216233

217-
def truth_value(val):
234+
def truth_value(val: Any) -> Any:
218235
if val in ("true", "false"):
219236
return val == "true"
220237
if isinstance(val, bool):
@@ -228,7 +245,7 @@ def truth_value(val):
228245
return options
229246

230247

231-
def _handle_option_deprecations(options):
248+
def _handle_option_deprecations(options: _CaseInsensitiveDictionary) -> _CaseInsensitiveDictionary:
232249
"""Issue appropriate warnings when deprecated options are present in the
233250
options dictionary. Removes deprecated option key, value pairs if the
234251
options dictionary is found to also have the renamed option.
@@ -268,7 +285,7 @@ def _handle_option_deprecations(options):
268285
return options
269286

270287

271-
def _normalize_options(options):
288+
def _normalize_options(options: _CaseInsensitiveDictionary) -> _CaseInsensitiveDictionary:
272289
"""Normalizes option names in the options dictionary by converting them to
273290
their internally-used names.
274291
@@ -346,7 +363,7 @@ def split_options(
346363
options = _normalize_options(options)
347364

348365
if validate:
349-
options = validate_options(options, warn)
366+
options = cast(_CaseInsensitiveDictionary, validate_options(options, warn))
350367
if options.get("authsource") == "":
351368
raise InvalidURI("the authSource database cannot be an empty string")
352369

@@ -387,7 +404,7 @@ def split_hosts(hosts: str, default_port: Optional[int] = DEFAULT_PORT) -> List[
387404
)
388405

389406

390-
def _check_options(nodes, options):
407+
def _check_options(nodes: Sized, options: Mapping[str, Any]) -> None:
391408
# Ensure directConnection was not True if there are multiple seeds.
392409
if len(nodes) > 1 and options.get("directconnection"):
393410
raise ConfigurationError("Cannot specify multiple hosts with directConnection=true")
@@ -577,7 +594,7 @@ def parse_uri(
577594
}
578595

579596

580-
def _parse_kms_tls_options(kms_tls_options):
597+
def _parse_kms_tls_options(kms_tls_options: Optional[Mapping[str, Any]]) -> Dict[str, SSLContext]:
581598
"""Parse KMS TLS connection options."""
582599
if not kms_tls_options:
583600
return {}

0 commit comments

Comments
 (0)