4040
4141from contextlib import contextmanager
4242from functools import wraps
43+ from typing import Dict , no_type_check
4344from unittest import SkipTest
4445
4546import pymongo
4849from bson .son import SON
4950from pymongo import common , message
5051from pymongo .common import partition_node
52+ from pymongo .database import Database
5153from pymongo .hello import HelloCompat
54+ from pymongo .mongo_client import MongoClient
5255from pymongo .server_api import ServerApi
5356from pymongo .ssl_support import HAVE_SSL , _ssl
5457from pymongo .uri_parser import parse_uri
8689 os .path .join (CERT_PATH , 'client.pem' ))
8790CA_PEM = os .environ .get ('CA_PEM' , os .path .join (CERT_PATH , 'ca.pem' ))
8891
89- TLS_OPTIONS = dict (tls = True )
92+ TLS_OPTIONS : Dict = dict (tls = True )
9093if CLIENT_PEM :
9194 TLS_OPTIONS ['tlsCertificateKeyFile' ] = CLIENT_PEM
9295if CA_PEM :
102105 # Remove after PYTHON-2712
103106 from pymongo import pool
104107 pool ._MOCK_SERVICE_ID = True
105- res = parse_uri (SINGLE_MONGOS_LB_URI )
108+ res = parse_uri (SINGLE_MONGOS_LB_URI or "" )
106109 host , port = res ['nodelist' ][0 ]
107110 db_user = res ['username' ] or db_user
108111 db_pwd = res ['password' ] or db_pwd
109112elif TEST_SERVERLESS :
110113 TEST_LOADBALANCER = True
111- res = parse_uri (SINGLE_MONGOS_LB_URI )
114+ res = parse_uri (SINGLE_MONGOS_LB_URI or "" )
112115 host , port = res ['nodelist' ][0 ]
113116 db_user = res ['username' ] or db_user
114117 db_pwd = res ['password' ] or db_pwd
@@ -184,6 +187,7 @@ def enable(self):
184187 def __enter__ (self ):
185188 self .enable ()
186189
190+ @no_type_check
187191 def disable (self ):
188192 common .HEARTBEAT_FREQUENCY = self .old_heartbeat_frequency
189193 common .MIN_HEARTBEAT_INTERVAL = self .old_min_heartbeat_interval
@@ -224,6 +228,8 @@ def _all_users(db):
224228
225229
226230class ClientContext (object ):
231+ client : MongoClient
232+
227233 MULTI_MONGOS_LB_URI = MULTI_MONGOS_LB_URI
228234
229235 def __init__ (self ):
@@ -247,9 +253,9 @@ def __init__(self):
247253 self .tls = False
248254 self .tlsCertificateKeyFile = False
249255 self .server_is_resolvable = is_server_resolvable ()
250- self .default_client_options = {}
256+ self .default_client_options : Dict = {}
251257 self .sessions_enabled = False
252- self .client = None
258+ self .client = None # type: ignore
253259 self .conn_lock = threading .Lock ()
254260 self .is_data_lake = False
255261 self .load_balancer = TEST_LOADBALANCER
@@ -340,6 +346,7 @@ def _init_client(self):
340346 try :
341347 self .cmd_line = self .client .admin .command ('getCmdLineOpts' )
342348 except pymongo .errors .OperationFailure as e :
349+ assert e .details is not None
343350 msg = e .details .get ('errmsg' , '' )
344351 if e .code == 13 or 'unauthorized' in msg or 'login' in msg :
345352 # Unauthorized.
@@ -418,6 +425,7 @@ def _init_client(self):
418425 else :
419426 self .server_parameters = self .client .admin .command (
420427 'getParameter' , '*' )
428+ assert self .cmd_line is not None
421429 if 'enableTestCommands=1' in self .cmd_line ['argv' ]:
422430 self .test_commands_enabled = True
423431 elif 'parsed' in self .cmd_line :
@@ -436,7 +444,8 @@ def _init_client(self):
436444 self .mongoses .append (address )
437445 if not self .serverless :
438446 # 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
440449 mongos_client = self ._connect (
441450 * next_address , ** self .default_client_options )
442451 if mongos_client :
@@ -496,6 +505,7 @@ def _check_user_provided(self):
496505 try :
497506 return db_user in _all_users (client .admin )
498507 except pymongo .errors .OperationFailure as e :
508+ assert e .details is not None
499509 msg = e .details .get ('errmsg' , '' )
500510 if e .code == 18 or 'auth fails' in msg :
501511 # Auth failed.
@@ -505,6 +515,7 @@ def _check_user_provided(self):
505515
506516 def _server_started_with_auth (self ):
507517 # MongoDB >= 2.0
518+ assert self .cmd_line is not None
508519 if 'parsed' in self .cmd_line :
509520 parsed = self .cmd_line ['parsed' ]
510521 # MongoDB >= 2.6
@@ -525,6 +536,7 @@ def _server_started_with_ipv6(self):
525536 if not socket .has_ipv6 :
526537 return False
527538
539+ assert self .cmd_line is not None
528540 if 'parsed' in self .cmd_line :
529541 if not self .cmd_line ['parsed' ].get ('net' , {}).get ('ipv6' ):
530542 return False
@@ -932,6 +944,9 @@ def fail_point(self, command_args):
932944
933945class IntegrationTest (PyMongoTestCase ):
934946 """Base class for TestCases that need a connection to MongoDB to pass."""
947+ client : MongoClient
948+ db : Database
949+ credentials : Dict [str , str ]
935950
936951 @classmethod
937952 @client_context .require_connection
@@ -1073,7 +1088,7 @@ def run(self, test):
10731088
10741089
10751090if HAVE_XML :
1076- class PymongoXMLTestRunner (XMLTestRunner ):
1091+ class PymongoXMLTestRunner (XMLTestRunner ): # type: ignore[misc]
10771092 def run (self , test ):
10781093 setup ()
10791094 result = super (PymongoXMLTestRunner , self ).run (test )
0 commit comments