-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1b5e5c
commit 95db076
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE | ||
|
||
"""Utils for arguments/options parsing and handling.""" | ||
|
||
from typing import Any, Dict | ||
|
||
from pylint.config.argument import _Argument | ||
|
||
ArgumentsDict = Dict[str, _Argument] | ||
|
||
IMPLEMENTED_OPTDICT_KEYS = {"default", "type", "choices", "help", "metavar"} | ||
"""This is used to track our progress on accepting all optdict keys""" | ||
|
||
|
||
def _convert_option_to_argument(opt: str, optdict: Dict[str, Any]) -> _Argument: | ||
"""Convert an optdict to an Argument class instance""" | ||
# See if the optdict contains any keys we don't yet implement | ||
for key, value in optdict.items(): | ||
if key not in IMPLEMENTED_OPTDICT_KEYS: | ||
print("Unhandled key found in Argument creation:", key) | ||
print("It's value is:", value) | ||
return _Argument( | ||
name=opt, | ||
default=optdict["default"], | ||
arg_type=optdict["type"], | ||
choices=optdict.get("choices", []), | ||
arg_help=optdict["help"], | ||
metavar=optdict["metavar"], | ||
) | ||
|
||
|
||
def _validate_argument(argument: _Argument) -> None: | ||
"""Method to check if all instance attributes have the correct typed.""" | ||
# Check default value | ||
assert argument.default | ||
assert argument.default == argument.type(argument.default) | ||
|
||
# Check type value | ||
assert argument.type | ||
assert callable(argument.type) | ||
|
||
# Check choices value | ||
assert isinstance(argument.choices, list) | ||
assert all(isinstance(i, str) for i in argument.choices) | ||
|
||
# Check help value | ||
assert argument.help | ||
assert isinstance(argument.help, str) | ||
|
||
# Check metavar value | ||
assert argument.metavar | ||
assert isinstance(argument.metavar, str) |