Skip to content

Commit 1413bb0

Browse files
committed
PYTHON-1087 - Fix __repr__ handling of *TimeoutMS=None
1 parent c6907bd commit 1413bb0

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

pymongo/common.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,8 @@ def validate_ok_for_update(update):
415415
'fsync': validate_boolean_or_string,
416416
'j': validate_boolean_or_string,
417417
'journal': validate_boolean_or_string,
418-
'connecttimeoutms': validate_timeout_or_none,
419418
'maxpoolsize': validate_positive_integer_or_none,
420419
'socketkeepalive': validate_boolean_or_string,
421-
'sockettimeoutms': validate_timeout_or_none,
422-
'waitqueuetimeoutms': validate_timeout_or_none,
423420
'waitqueuemultiple': validate_non_negative_integer_or_none,
424421
'ssl': validate_boolean_or_string,
425422
'ssl_keyfile': validate_readable,
@@ -431,7 +428,6 @@ def validate_ok_for_update(update):
431428
'readpreference': validate_read_preference_mode,
432429
'readpreferencetags': validate_read_preference_tags,
433430
'localthresholdms': validate_positive_float_or_zero,
434-
'serverselectiontimeoutms': validate_timeout_or_zero,
435431
'authmechanism': validate_auth_mechanism,
436432
'authsource': validate_string,
437433
'authmechanismproperties': validate_auth_mechanism_properties,
@@ -440,12 +436,20 @@ def validate_ok_for_update(update):
440436
'connect': validate_boolean_or_string,
441437
}
442438

439+
TIMEOUT_VALIDATORS = {
440+
'connecttimeoutms': validate_timeout_or_none,
441+
'sockettimeoutms': validate_timeout_or_none,
442+
'waitqueuetimeoutms': validate_timeout_or_none,
443+
'serverselectiontimeoutms': validate_timeout_or_zero,
444+
}
445+
443446
KW_VALIDATORS = {
444447
'document_class': validate_document_class,
445448
'read_preference': validate_read_preference,
446449
'event_listeners': _validate_event_listeners
447450
}
448451

452+
URI_VALIDATORS.update(TIMEOUT_VALIDATORS)
449453
VALIDATORS = URI_VALIDATORS.copy()
450454
VALIDATORS.update(KW_VALIDATORS)
451455

pymongo/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ def option_repr(option, value):
839839
else:
840840
return 'document_class=%s.%s' % (value.__module__,
841841
value.__name__)
842-
if "ms" in option:
843-
return "%s='%s'" % (option, int(value * 1000))
842+
if option in common.TIMEOUT_VALIDATORS and value is not None:
843+
return "%s=%s" % (option, int(value * 1000))
844844

845845
return '%s=%r' % (option, value)
846846

test/test_client.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def test_repr(self):
259259

260260
client = MongoClient(
261261
'mongodb://localhost:27017,localhost:27018/?replicaSet=replset'
262-
'&connectTimeoutMS=12345',
262+
'&connectTimeoutMS=12345&w=1&wtimeoutms=100',
263263
connect=False, document_class=SON)
264264

265265
the_repr = repr(client)
@@ -269,8 +269,32 @@ def test_repr(self):
269269
"tz_aware=False, "
270270
"connect=False, ",
271271
the_repr)
272-
self.assertIn("connecttimeoutms='12345'", the_repr)
273-
self.assertIn("replicaset=", the_repr)
272+
self.assertIn("connecttimeoutms=12345", the_repr)
273+
self.assertIn("replicaset='replset'", the_repr)
274+
self.assertIn("w=1", the_repr)
275+
self.assertIn("wtimeoutms=100", the_repr)
276+
277+
self.assertEqual(eval(the_repr), client)
278+
279+
client = MongoClient("localhost:27017,localhost:27018",
280+
replicaSet='replset',
281+
connectTimeoutMS=12345,
282+
socketTimeoutMS=None,
283+
w=1,
284+
wtimeoutms=100,
285+
connect=False)
286+
the_repr = repr(client)
287+
self.assertIn('MongoClient(host=', the_repr)
288+
self.assertIn(
289+
"document_class=dict, "
290+
"tz_aware=False, "
291+
"connect=False, ",
292+
the_repr)
293+
self.assertIn("connecttimeoutms=12345", the_repr)
294+
self.assertIn("replicaset='replset'", the_repr)
295+
self.assertIn("sockettimeoutms=None", the_repr)
296+
self.assertIn("w=1", the_repr)
297+
self.assertIn("wtimeoutms=100", the_repr)
274298

275299
self.assertEqual(eval(the_repr), client)
276300

0 commit comments

Comments
 (0)