- 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
Merged
ShaneHarvey merged 14 commits into mongodb:master from caseyclements:feature/PYTHON-3036 Nov 30, 2023
Merged
Changes from 13 commits
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
125831e
PYTHON-3036 Add difflib.get_close_matches to common.validate
caseyclements 9f21925
PYTHON-3036 Add tests of suggestions for kwargs in MongoClient.__init…
caseyclements 8344de8
PYTHON-3036 Removed redundant quotes
caseyclements b0a436f
Added caseyclements to contributors.rst
caseyclements b6038e2
PYTHON-3036 Formatting
caseyclements d784da6
PYTHON-3036 Add camelCase comment to suggestions
caseyclements d2c355c
PYTHON-3036 move the validator() call to after the try/except so that…
caseyclements 4e6a044
PYTHON-3036 Add test that highlights that error message returns kwarg…
caseyclements 7c35554
PYTHON-3036 Turn off typecheck in one new test_validate_suggestion
caseyclements eff93e0
PYTHON-3036 arg-type to typecheck ignore statement
caseyclements 045902c
PYTHON-3036 Removed code duplication by having get_validated_options …
caseyclements 6de01e7
PYTHON-3036 Updated expected error message in a test
caseyclements 62a0725
PYTHON-3036 Added _get_validators
caseyclements f287e76
PYTHON-3036 Added return type to _get_validators
caseyclements File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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,24 @@ 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 _get_validator( | ||
key: str, validators: dict[str, Callable[[Any, Any], Any]], normed_key: Optional[str] = None | ||
): | ||
normed_key = normed_key or key | ||
try: | ||
return validators[normed_key] | ||
except KeyError: | ||
suggestions = get_close_matches(normed_key, validators, cutoff=0.2) | ||
raise_config_error(key, suggestions) | ||
| ||
| ||
def validate(option: str, value: Any) -> tuple[str, Any]: | ||
"""Generic validation function.""" | ||
lower = option.lower() | ||
validator = VALIDATORS.get(lower, raise_config_error) | ||
validator = _get_validator(option, VALIDATORS, normed_key=option.lower()) | ||
value = validator(option, value) | ||
return option, value | ||
| ||
| @@ -856,15 +870,15 @@ 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 | ||
validator = _get_validator(opt, URI_OPTIONS_VALIDATOR_MAP, normed_key=normed_key) | ||
validated = validator(opt, 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 | ||
| ||
| ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typecheck is failing due to the missing return type here.