- Notifications
You must be signed in to change notification settings - Fork 1.1k
[PYTHON-3036] Improve error message for unknown MongoClient options #1440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
125831e
9f21925
8344de8
b0a436f
b6038e2
d784da6
d2c355c
4e6a044
7c35554
eff93e0
045902c
6de01e7
62a0725
f287e76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -20,6 +20,7 @@ | |
import inspect | ||
import warnings | ||
from collections import OrderedDict, abc | ||
from difflib import get_close_matches | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
| @@ -162,9 +163,12 @@ def clean_node(node: str) -> tuple[str, int]: | |
return host.lower(), port | ||
| ||
| ||
def raise_config_error(key: str, dummy: Any) -> NoReturn: | ||
def raise_config_error(key: str, suggestions: Optional[list] = None) -> NoReturn: | ||
"""Raise ConfigurationError with the given key name.""" | ||
raise ConfigurationError(f"Unknown option {key}") | ||
msg = f"Unknown option: {key}." | ||
if suggestions: | ||
msg += f" Did you mean one of ({', '.join(suggestions)}) or maybe a camelCase version of one? Refer to docstring." | ||
raise ConfigurationError(msg) | ||
| ||
| ||
# Mapping of URI uuid representation options to valid subtypes. | ||
| @@ -810,14 +814,18 @@ def validate_auth_option(option: str, value: Any) -> tuple[str, Any]: | |
"""Validate optional authentication parameters.""" | ||
lower, value = validate(option, value) | ||
if lower not in _AUTH_OPTIONS: | ||
raise ConfigurationError(f"Unknown authentication option: {option}") | ||
raise ConfigurationError(f"Unknown option: {option}. Must be in {_AUTH_OPTIONS}") | ||
return option, value | ||
| ||
| ||
def validate(option: str, value: Any) -> tuple[str, Any]: | ||
"""Generic validation function.""" | ||
lower = option.lower() | ||
validator = VALIDATORS.get(lower, raise_config_error) | ||
try: | ||
validator = VALIDATORS[lower] | ||
except KeyError: | ||
suggestions = get_close_matches(lower, VALIDATORS, cutoff=0.2) | ||
raise_config_error(option, suggestions) | ||
| ||
value = validator(option, value) | ||
return option, value | ||
| ||
| @@ -856,15 +864,14 @@ def get_setter_key(x: str) -> str: | |
for opt, value in options.items(): | ||
normed_key = get_normed_key(opt) | ||
try: | ||
validator = URI_OPTIONS_VALIDATOR_MAP.get(normed_key, raise_config_error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code should still be using URI_OPTIONS_VALIDATOR_MAP but by calling validate() it now uses VALIDATORS. This is why I suggested the | ||
value = validator(opt, value) # noqa: PLW2901 | ||
_, validated = validate(normed_key, value) | ||
except (ValueError, TypeError, ConfigurationError) as exc: | ||
if warn: | ||
warnings.warn(str(exc), stacklevel=2) | ||
else: | ||
raise | ||
else: | ||
validated_options[get_setter_key(normed_key)] = value | ||
validated_options[get_setter_key(normed_key)] = validated | ||
return validated_options | ||
| ||
| ||
|
Uh oh!
There was an error while loading. Please reload this page.