Skip to content

Commit

Permalink
Add utils to config
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Dec 22, 2021
1 parent a1b5e5c commit 95db076
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pylint/config/utils.py
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)

0 comments on commit 95db076

Please sign in to comment.