Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/6117.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Ini options (typically added via ``parser.addini`` in ``pytest_addoption``)
allow for using ``default=None``, where previously the default would be ``[]``
then.
2 changes: 1 addition & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ def _getini(self, name: str) -> Any:
try:
value = self.inicfg[name]
except KeyError:
if default is not None:
if default is not notset:
return default
if type is None:
return ""
Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import py

from _pytest.config import notset
from _pytest.config.exceptions import UsageError

FILE_OR_DIR = "file_or_dir"
Expand Down Expand Up @@ -127,7 +128,7 @@ def parse_known_and_unknown_args(
args = [str(x) if isinstance(x, py.path.local) else x for x in args]
return optparser.parse_known_args(args, namespace=namespace)

def addini(self, name, help, type=None, default=None):
def addini(self, name, help, type=None, default=notset):
""" register an ini-file option.

:name: name of the ini-variable
Expand Down
2 changes: 2 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def test_addini(self, testdir):
"""
def pytest_addoption(parser):
parser.addini("myname", "my new ini value")
parser.addini("defaultunset", "allow None", default=None)
"""
)
testdir.makeini(
Expand All @@ -279,6 +280,7 @@ def pytest_addoption(parser):
config = testdir.parseconfig()
val = config.getini("myname")
assert val == "hello"
assert config.getini("defaultunset") is None
pytest.raises(ValueError, config.getini, "other")

def test_addini_pathlist(self, testdir):
Expand Down