diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index 9fcabd6c8..461b8afdf 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -212,24 +212,27 @@ class Parser: """ assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool") if default is NOT_SET: - default = self._get_ini_default_for_type(type) + default = get_ini_default_for_type(type) self._inidict[name] = (help, type, default) self._ininames.append(name) - def _get_ini_default_for_type(self, type) -> Any: - default = Any - if type is None: - default = "" - else: - if type in ["paths", "pathlist", "args", "linelist"]: - default = [] - elif type == "bool": - default = False - else: - # for string type - default = "" - return default + +def get_ini_default_for_type( + type: Optional[Literal["string", "paths", "pathlist", "args", "linelist", "bool"]] +) -> Any: + """ + Used by addini to get the default value for a given ini-option type, when + default is not supplied. + """ + if type is None: + return "" + elif type in ("paths", "pathlist", "args", "linelist"): + return [] + elif type == "bool": + return False + else: + return "" class ArgumentError(Exception): diff --git a/testing/test_config.py b/testing/test_config.py index 52fb1e0ae..b7c61feea 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -5,6 +5,7 @@ import re import sys import textwrap from pathlib import Path +from typing import Any from typing import Dict from typing import List from typing import Sequence @@ -21,6 +22,7 @@ from _pytest.config import Config from _pytest.config import ConftestImportFailure from _pytest.config import ExitCode from _pytest.config import parse_warning_filter +from _pytest.config.argparsing import get_ini_default_for_type from _pytest.config.exceptions import UsageError from _pytest.config.findpaths import determine_setup from _pytest.config.findpaths import get_common_ancestor @@ -904,6 +906,21 @@ class TestConfigAPI: value = config.getini("no_type") assert value == "" + @pytest.mark.parametrize( + "type, expected", + [ + pytest.param(None, "", id="None"), + pytest.param("string", "", id="string"), + pytest.param("paths", [], id="paths"), + pytest.param("pathlist", [], id="pathlist"), + pytest.param("args", [], id="args"), + pytest.param("linelist", [], id="linelist"), + pytest.param("bool", False, id="bool"), + ], + ) + def test_get_ini_default_for_type(self, type: Any, expected: Any) -> None: + assert get_ini_default_for_type(type) == expected + def test_confcutdir_check_isdir(self, pytester: Pytester) -> None: """Give an error if --confcutdir is not a valid directory (#2078)""" exp_match = r"^--confcutdir must be a directory, given: "