third_party.pylibs.pylint.src/pylint/config/utils.py

73 lines
2.5 KiB
Python
Raw Normal View History

# 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
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Utils for arguments/options parsing and handling."""
2022-04-02 13:33:19 +00:00
from typing import Any, Dict, Union
2022-04-02 17:30:16 +00:00
from pylint.config.argument import _Argument, _CallableArgument, _StoreTrueArgument
from pylint.config.callback_actions import _CallbackAction
IMPLEMENTED_OPTDICT_KEYS = {"action", "default", "type", "choices", "help", "metavar"}
"""This is used to track our progress on accepting all optdict keys."""
2022-04-02 13:33:19 +00:00
def _convert_option_to_argument(
opt: str, optdict: Dict[str, Any]
2022-04-02 17:30:16 +00:00
) -> Union[_Argument, _StoreTrueArgument, _CallableArgument]:
"""Convert an optdict to an Argument class instance."""
# See if the optdict contains any keys we don't yet implement
# pylint: disable-next=fixme
# TODO: This should be removed once the migration to argparse is finished
for key, value in optdict.items():
if key not in IMPLEMENTED_OPTDICT_KEYS:
print("Unhandled key found in Argument creation:", key) # pragma: no cover
print("It's value is:", value) # pragma: no cover
# Get the long and short flags
flags = [f"--{opt}"]
if "short" in optdict:
flags += [f"-{optdict['short']}"]
# Get the action type
2022-04-02 13:33:19 +00:00
action = optdict.get("action", "store")
# pylint: disable-next=fixme
# TODO: Remove this handling after we have deprecated multiple-choice arguments
choices = optdict.get("choices", None)
if opt == "confidence":
choices = None
2022-04-02 13:33:19 +00:00
if action == "store_true":
return _StoreTrueArgument(
flags=flags,
2022-04-02 13:33:19 +00:00
action=action,
default=optdict["default"],
arg_help=optdict["help"],
)
2022-04-02 17:30:16 +00:00
if not isinstance(action, str) and issubclass(action, _CallbackAction):
return _CallableArgument(
flags=flags,
action=action,
arg_help=optdict["help"],
kwargs=optdict["kwargs"],
)
return _Argument(
flags=flags,
2022-04-02 13:33:19 +00:00
action=action,
default=optdict["default"],
arg_type=optdict["type"],
choices=choices,
arg_help=optdict["help"],
metavar=optdict["metavar"],
)
def _parse_rich_type_value(value: Any) -> str:
"""Parse rich (toml) types into strings."""
if isinstance(value, (list, tuple)):
return ",".join(value)
return str(value)