40
40
41
41
from contextlib import contextmanager
42
42
from functools import wraps
43
+ from typing import Dict , no_type_check
43
44
from unittest import SkipTest
44
45
45
46
import pymongo
48
49
from bson .son import SON
49
50
from pymongo import common , message
50
51
from pymongo .common import partition_node
52
+ from pymongo .database import Database
51
53
from pymongo .hello import HelloCompat
54
+ from pymongo .mongo_client import MongoClient
52
55
from pymongo .server_api import ServerApi
53
56
from pymongo .ssl_support import HAVE_SSL , _ssl
54
57
from pymongo .uri_parser import parse_uri
86
89
os .path .join (CERT_PATH , 'client.pem' ))
87
90
CA_PEM = os .environ .get ('CA_PEM' , os .path .join (CERT_PATH , 'ca.pem' ))
88
91
89
- TLS_OPTIONS = dict (tls = True )
92
+ TLS_OPTIONS : Dict = dict (tls = True )
90
93
if CLIENT_PEM :
91
94
TLS_OPTIONS ['tlsCertificateKeyFile' ] = CLIENT_PEM
92
95
if CA_PEM :
102
105
# Remove after PYTHON-2712
103
106
from pymongo import pool
104
107
pool ._MOCK_SERVICE_ID = True
105
- res = parse_uri (SINGLE_MONGOS_LB_URI )
108
+ res = parse_uri (SINGLE_MONGOS_LB_URI or "" )
106
109
host , port = res ['nodelist' ][0 ]
107
110
db_user = res ['username' ] or db_user
108
111
db_pwd = res ['password' ] or db_pwd
109
112
elif TEST_SERVERLESS :
110
113
TEST_LOADBALANCER = True
111
- res = parse_uri (SINGLE_MONGOS_LB_URI )
114
+ res = parse_uri (SINGLE_MONGOS_LB_URI or "" )
112
115
host , port = res ['nodelist' ][0 ]
113
116
db_user = res ['username' ] or db_user
114
117
db_pwd = res ['password' ] or db_pwd
@@ -184,6 +187,7 @@ def enable(self):
184
187
def __enter__ (self ):
185
188
self .enable ()
186
189
190
+ @no_type_check
187
191
def disable (self ):
188
192
common .HEARTBEAT_FREQUENCY = self .old_heartbeat_frequency
189
193
common .MIN_HEARTBEAT_INTERVAL = self .old_min_heartbeat_interval
@@ -224,6 +228,8 @@ def _all_users(db):
224
228
225
229
226
230
class ClientContext (object ):
231
+ client : MongoClient
232
+
227
233
MULTI_MONGOS_LB_URI = MULTI_MONGOS_LB_URI
228
234
229
235
def __init__ (self ):
@@ -247,9 +253,9 @@ def __init__(self):
247
253
self .tls = False
248
254
self .tlsCertificateKeyFile = False
249
255
self .server_is_resolvable = is_server_resolvable ()
250
- self .default_client_options = {}
256
+ self .default_client_options : Dict = {}
251
257
self .sessions_enabled = False
252
- self .client = None
258
+ self .client = None # type: ignore
253
259
self .conn_lock = threading .Lock ()
254
260
self .is_data_lake = False
255
261
self .load_balancer = TEST_LOADBALANCER
@@ -340,6 +346,7 @@ def _init_client(self):
340
346
try :
341
347
self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
342
348
except pymongo .errors .OperationFailure as e :
349
+ assert e .details is not None
343
350
msg = e .details .get ('errmsg' , '' )
344
351
if e .code == 13 or 'unauthorized' in msg or 'login' in msg :
345
352
# Unauthorized.
@@ -418,6 +425,7 @@ def _init_client(self):
418
425
else :
419
426
self .server_parameters = self .client .admin .command (
420
427
'getParameter' , '*' )
428
+ assert self .cmd_line is not None
421
429
if 'enableTestCommands=1' in self .cmd_line ['argv' ]:
422
430
self .test_commands_enabled = True
423
431
elif 'parsed' in self .cmd_line :
@@ -436,7 +444,8 @@ def _init_client(self):
436
444
self .mongoses .append (address )
437
445
if not self .serverless :
438
446
# Check for another mongos on the next port.
439
- next_address = address [0 ], address [1 ] + 1
447
+ assert address is not None
448
+ next_address = address [0 ], address [1 ] + 1
440
449
mongos_client = self ._connect (
441
450
* next_address , ** self .default_client_options )
442
451
if mongos_client :
@@ -496,6 +505,7 @@ def _check_user_provided(self):
496
505
try :
497
506
return db_user in _all_users (client .admin )
498
507
except pymongo .errors .OperationFailure as e :
508
+ assert e .details is not None
499
509
msg = e .details .get ('errmsg' , '' )
500
510
if e .code == 18 or 'auth fails' in msg :
501
511
# Auth failed.
@@ -505,6 +515,7 @@ def _check_user_provided(self):
505
515
506
516
def _server_started_with_auth (self ):
507
517
# MongoDB >= 2.0
518
+ assert self .cmd_line is not None
508
519
if 'parsed' in self .cmd_line :
509
520
parsed = self .cmd_line ['parsed' ]
510
521
# MongoDB >= 2.6
@@ -525,6 +536,7 @@ def _server_started_with_ipv6(self):
525
536
if not socket .has_ipv6 :
526
537
return False
527
538
539
+ assert self .cmd_line is not None
528
540
if 'parsed' in self .cmd_line :
529
541
if not self .cmd_line ['parsed' ].get ('net' , {}).get ('ipv6' ):
530
542
return False
@@ -932,6 +944,9 @@ def fail_point(self, command_args):
932
944
933
945
class IntegrationTest (PyMongoTestCase ):
934
946
"""Base class for TestCases that need a connection to MongoDB to pass."""
947
+ client : MongoClient
948
+ db : Database
949
+ credentials : Dict [str , str ]
935
950
936
951
@classmethod
937
952
@client_context .require_connection
@@ -1073,7 +1088,7 @@ def run(self, test):
1073
1088
1074
1089
1075
1090
if HAVE_XML :
1076
- class PymongoXMLTestRunner (XMLTestRunner ):
1091
+ class PymongoXMLTestRunner (XMLTestRunner ): # type: ignore[misc]
1077
1092
def run (self , test ):
1078
1093
setup ()
1079
1094
result = super (PymongoXMLTestRunner , self ).run (test )
0 commit comments