Skip to content

Commit

Permalink
Merge pull request #7872 from altendky/addini_takes_string
Browse files Browse the repository at this point in the history
Document that Parser.addini() can take, and defaults to, 'string'
  • Loading branch information
altendky committed Oct 7, 2020
2 parents 95917f8 + 1630c37 commit 5acc55e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/7872.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``_pytest.config.argparsing.Parser.addini()`` accepts explicit ``None`` and ``"string"``.
2 changes: 1 addition & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ def _getini(self, name: str):
elif type == "bool":
return _strtobool(str(value).strip())
else:
assert type is None
assert type in [None, "string"]
return value

def _getconftest_pathlist(
Expand Down
11 changes: 7 additions & 4 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,23 @@ def addini(
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)

Expand Down
12 changes: 9 additions & 3 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,17 @@ def test_getconftest_pathlist(self, testdir, tmpdir):
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(
Expand Down

0 comments on commit 5acc55e

Please sign in to comment.