From 95db0760f2aa075dee657061b11c631b1cebc848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 22 Dec 2021 09:57:39 +0100 Subject: [PATCH] Add ``utils`` to config --- pylint/config/utils.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pylint/config/utils.py diff --git a/pylint/config/utils.py b/pylint/config/utils.py new file mode 100644 index 00000000000..f013c84352a --- /dev/null +++ b/pylint/config/utils.py @@ -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)