14
14
15
15
16
16
"""Tools to parse and validate a MongoDB URI."""
17
+ from __future__ import annotations
18
+
17
19
import re
18
20
import sys
19
21
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
+ )
21
35
from urllib .parse import unquote_plus
22
36
23
37
from pymongo .client_options import _parse_ssl_options
32
46
from pymongo .srv_resolver import _HAVE_DNSPYTHON , _SrvResolver
33
47
from pymongo .typings import _Address
34
48
49
+ if TYPE_CHECKING :
50
+ from pymongo .pyopenssl_context import SSLContext
51
+
35
52
SCHEME = "mongodb://"
36
53
SCHEME_LEN = len (SCHEME )
37
54
SRV_SCHEME = "mongodb+srv://"
38
55
SRV_SCHEME_LEN = len (SRV_SCHEME )
39
56
DEFAULT_PORT = 27017
40
57
41
58
42
- def _unquoted_percent (s ) :
59
+ def _unquoted_percent (s : str ) -> bool :
43
60
"""Check for unescaped percent signs.
44
61
45
62
:Parameters:
@@ -152,7 +169,7 @@ def parse_host(entity: str, default_port: Optional[int] = DEFAULT_PORT) -> _Addr
152
169
}
153
170
154
171
155
- def _parse_options (opts , delim ) :
172
+ def _parse_options (opts : str , delim : Optional [ str ]) -> _CaseInsensitiveDictionary :
156
173
"""Helper method for split_options which creates the options dict.
157
174
Also handles the creation of a list for the URI tag_sets/
158
175
readpreferencetags portion, and the use of a unicode options string.
@@ -174,7 +191,7 @@ def _parse_options(opts, delim):
174
191
return options
175
192
176
193
177
- def _handle_security_options (options ) :
194
+ def _handle_security_options (options : _CaseInsensitiveDictionary ) -> _CaseInsensitiveDictionary :
178
195
"""Raise appropriate errors when conflicting TLS options are present in
179
196
the options dictionary.
180
197
@@ -214,7 +231,7 @@ def _handle_security_options(options):
214
231
215
232
if "ssl" in options and "tls" in options :
216
233
217
- def truth_value (val ) :
234
+ def truth_value (val : Any ) -> Any :
218
235
if val in ("true" , "false" ):
219
236
return val == "true"
220
237
if isinstance (val , bool ):
@@ -228,7 +245,7 @@ def truth_value(val):
228
245
return options
229
246
230
247
231
- def _handle_option_deprecations (options ) :
248
+ def _handle_option_deprecations (options : _CaseInsensitiveDictionary ) -> _CaseInsensitiveDictionary :
232
249
"""Issue appropriate warnings when deprecated options are present in the
233
250
options dictionary. Removes deprecated option key, value pairs if the
234
251
options dictionary is found to also have the renamed option.
@@ -268,7 +285,7 @@ def _handle_option_deprecations(options):
268
285
return options
269
286
270
287
271
- def _normalize_options (options ) :
288
+ def _normalize_options (options : _CaseInsensitiveDictionary ) -> _CaseInsensitiveDictionary :
272
289
"""Normalizes option names in the options dictionary by converting them to
273
290
their internally-used names.
274
291
@@ -346,7 +363,7 @@ def split_options(
346
363
options = _normalize_options (options )
347
364
348
365
if validate :
349
- options = validate_options (options , warn )
366
+ options = cast ( _CaseInsensitiveDictionary , validate_options (options , warn ) )
350
367
if options .get ("authsource" ) == "" :
351
368
raise InvalidURI ("the authSource database cannot be an empty string" )
352
369
@@ -387,7 +404,7 @@ def split_hosts(hosts: str, default_port: Optional[int] = DEFAULT_PORT) -> List[
387
404
)
388
405
389
406
390
- def _check_options (nodes , options ) :
407
+ def _check_options (nodes : Sized , options : Mapping [ str , Any ]) -> None :
391
408
# Ensure directConnection was not True if there are multiple seeds.
392
409
if len (nodes ) > 1 and options .get ("directconnection" ):
393
410
raise ConfigurationError ("Cannot specify multiple hosts with directConnection=true" )
@@ -577,7 +594,7 @@ def parse_uri(
577
594
}
578
595
579
596
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 ] :
581
598
"""Parse KMS TLS connection options."""
582
599
if not kms_tls_options :
583
600
return {}
0 commit comments