Skip to content

Commit 241a898

Browse files
committed
PYTHON-1059 - Make kwarg only options unknown to the URI parser
1 parent b227f07 commit 241a898

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

pymongo/common.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def validate_ok_for_update(update):
407407

408408
# journal is an alias for j,
409409
# wtimeoutms is an alias for wtimeout,
410-
VALIDATORS = {
410+
URI_VALIDATORS = {
411411
'replicaset': validate_string_or_none,
412412
'w': validate_int_or_basestring,
413413
'wtimeout': validate_integer,
@@ -428,21 +428,27 @@ def validate_ok_for_update(update):
428428
'ssl_ca_certs': validate_readable,
429429
'ssl_match_hostname': validate_boolean_or_string,
430430
'readconcernlevel': validate_string_or_none,
431-
'read_preference': validate_read_preference,
432431
'readpreference': validate_read_preference_mode,
433432
'readpreferencetags': validate_read_preference_tags,
434433
'localthresholdms': validate_positive_float_or_zero,
435434
'serverselectiontimeoutms': validate_timeout_or_zero,
436435
'authmechanism': validate_auth_mechanism,
437436
'authsource': validate_string,
438437
'authmechanismproperties': validate_auth_mechanism_properties,
439-
'document_class': validate_document_class,
440438
'tz_aware': validate_boolean_or_string,
441439
'uuidrepresentation': validate_uuid_representation,
442440
'connect': validate_boolean_or_string,
441+
}
442+
443+
KW_VALIDATORS = {
444+
'document_class': validate_document_class,
445+
'read_preference': validate_read_preference,
443446
'event_listeners': _validate_event_listeners
444447
}
445448

449+
VALIDATORS = URI_VALIDATORS.copy()
450+
VALIDATORS.update(KW_VALIDATORS)
451+
446452

447453
_AUTH_OPTIONS = frozenset(['authmechanismproperties'])
448454

@@ -466,18 +472,21 @@ def validate(option, value):
466472
return lower, value
467473

468474

469-
def get_validated_options(options):
475+
def get_validated_options(options, warn=True):
470476
"""Validate each entry in options and raise a warning if it is not valid.
471477
Returns a copy of options with invalid entries removed
472478
"""
473479
validated_options = {}
474480
for opt, value in iteritems(options):
475481
lower = opt.lower()
476482
try:
477-
validator = VALIDATORS.get(lower, raise_config_error)
483+
validator = URI_VALIDATORS.get(lower, raise_config_error)
478484
value = validator(opt, value)
479485
except (ValueError, ConfigurationError) as exc:
480-
warnings.warn(str(exc))
486+
if warn:
487+
warnings.warn(str(exc))
488+
else:
489+
raise
481490
else:
482491
validated_options[lower] = value
483492
return validated_options

pymongo/uri_parser.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
else:
2424
from urllib import unquote_plus
2525

26-
from pymongo.common import (validate as _validate, get_validated_options)
26+
from pymongo.common import get_validated_options
2727
from pymongo.errors import ConfigurationError, InvalidURI
2828

2929

@@ -165,10 +165,7 @@ def validate_options(opts, warn=False):
165165
invalid options will be ignored. Otherwise invalid options will
166166
cause errors.
167167
"""
168-
if warn:
169-
return get_validated_options(opts)
170-
else:
171-
return dict([_validate(opt, val) for opt, val in iteritems(opts)])
168+
return get_validated_options(opts, warn)
172169

173170

174171
def _parse_options(opts, delim):

0 commit comments

Comments
 (0)