From 06ab0f625e76142637942362a9cf4f24dec432b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 6 Jul 2022 18:59:35 +0200 Subject: [PATCH] Fix parsing of values in ``pyproject.toml`` --- .../_configuration/toml_parsing.py | 28 +++++++++++-------- tests/data/config/valid_toml/pyproject.toml | 3 ++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pydocstringformatter/_configuration/toml_parsing.py b/pydocstringformatter/_configuration/toml_parsing.py index 981061e7..56121ee7 100644 --- a/pydocstringformatter/_configuration/toml_parsing.py +++ b/pydocstringformatter/_configuration/toml_parsing.py @@ -2,14 +2,12 @@ import argparse import os -from typing import Any, Final +from typing import Any import tomli from pydocstringformatter._utils.exceptions import TomlParsingError, UnrecognizedOption -OPTIONS_TYPES: Final = {"write": "store_true", "exclude": "store"} - def get_toml_file() -> dict[str, Any] | None: """See if there is a pyproject.toml and extract the correct section if it exists.""" @@ -26,19 +24,27 @@ def get_toml_file() -> dict[str, Any] | None: return None -def parse_toml_option(opt: str, value: Any) -> list[str]: +def parse_toml_option( + parser: argparse.ArgumentParser, opt: str, value: Any +) -> list[str]: """Parse an options value in the correct argument type for argparse.""" + # pylint: disable=protected-access try: - action = OPTIONS_TYPES[opt] + action = parser._option_string_actions[f"--{opt}"] except KeyError as exc: - raise UnrecognizedOption(f"Don't recognize option {opt}") from exc + try: + action = parser._option_string_actions[f"-{opt}"] + except KeyError: + raise UnrecognizedOption(f"Don't recognize option {opt}") from exc - if action == "store_true": + if isinstance(action, argparse._StoreTrueAction): if value is True: - return [f"--{opt}"] + return [action.option_strings[0]] return [] - if action == "store": - return [f"--{opt}", value] + if isinstance(action, argparse._StoreAction): + if isinstance(value, int): + value = str(value) + return [action.option_strings[0], value] return [] # pragma: no cover @@ -50,6 +56,6 @@ def parse_toml_file( arguments: list[str] = [] for key, value in toml_sect.items(): - arguments += parse_toml_option(key, value) + arguments += parse_toml_option(parser, key, value) parser.parse_args(arguments, namespace) diff --git a/tests/data/config/valid_toml/pyproject.toml b/tests/data/config/valid_toml/pyproject.toml index 6da9d857..ef6e0ac2 100644 --- a/tests/data/config/valid_toml/pyproject.toml +++ b/tests/data/config/valid_toml/pyproject.toml @@ -1,2 +1,5 @@ [tool.pydocstringformatter] write = true +max-summary-lines = 2 +# Also test short options +w = true