From af3759a50372602be748b126b3390ed38c072713 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 7 Oct 2020 17:21:55 -0400 Subject: [PATCH 1/3] Parser.addini() can take and defaults to 'string' --- src/_pytest/config/argparsing.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index 86e9c990a..9a4819655 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -160,20 +160,23 @@ class Parser: self, name: str, help: str, - type: Optional["Literal['pathlist', 'args', 'linelist', 'bool']"] = None, + type: Optional[ + "Literal['string', 'pathlist', 'args', 'linelist', 'bool']" + ] = None, default=None, ) -> None: """Register an ini-file option. :name: Name of the ini-variable. - :type: Type of the variable, can be ``pathlist``, ``args``, ``linelist`` - or ``bool``. + :type: Type of the variable, can be ``string``, ``pathlist``, ``args``, + ``linelist`` or ``bool``. Defaults to ``string`` if ``None`` or + not passed. :default: Default value if no ini-file option exists but is queried. The value of ini-variables can be retrieved via a call to :py:func:`config.getini(name) <_pytest.config.Config.getini>`. """ - assert type in (None, "pathlist", "args", "linelist", "bool") + assert type in (None, "string", "pathlist", "args", "linelist", "bool") self._inidict[name] = (help, type, default) self._ininames.append(name) From 76acb4433098c069b21903dec18ad1df5696b4fc Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 7 Oct 2020 17:56:54 -0400 Subject: [PATCH 2/3] Update tests to cover explicit None and "string" as addini() types --- src/_pytest/config/__init__.py | 2 +- testing/test_config.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 39fbe01a0..7e486e99e 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1402,7 +1402,7 @@ class Config: elif type == "bool": return _strtobool(str(value).strip()) else: - assert type is None + assert type in [None, "string"] return value def _getconftest_pathlist( diff --git a/testing/test_config.py b/testing/test_config.py index 582d241a6..f3fa64372 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -570,11 +570,17 @@ class TestConfigAPI: assert pl[0] == tmpdir assert pl[1] == somepath - def test_addini(self, testdir): + @pytest.mark.parametrize("maybe_type", ["not passed", "None", '"string"']) + def test_addini(self, testdir, maybe_type): + if maybe_type == "not passed": + type_string = "" + else: + type_string = f", {maybe_type}" + testdir.makeconftest( - """ + f""" def pytest_addoption(parser): - parser.addini("myname", "my new ini value") + parser.addini("myname", "my new ini value"{type_string}) """ ) testdir.makeini( From 1630c3726618b7654a8134ecec6247c18c8bb784 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 7 Oct 2020 18:06:13 -0400 Subject: [PATCH 3/3] Added changelog/7872.doc.rst --- changelog/7872.doc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/7872.doc.rst diff --git a/changelog/7872.doc.rst b/changelog/7872.doc.rst new file mode 100644 index 000000000..46236acbf --- /dev/null +++ b/changelog/7872.doc.rst @@ -0,0 +1 @@ +``_pytest.config.argparsing.Parser.addini()`` accepts explicit ``None`` and ``"string"``.