Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config.getini default none value #11282 #11496

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions changelog/11282.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return "None" as the default value if "None" or no default value if provided by the developer.In this approach, existing calls to the getini function would need to check for "None" values.
2 changes: 2 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,8 @@
return default
if type is None:
return ""
if type == "string":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im very torn on this change in fear of the butterfly effec
at the same time and more propper solution will need a new api to avoid hell for users
@nicoddemus whats your impression on this

in particular the detail about the type is such, that for both None and string, it also makes sense to return a empty string

return None

Check warning on line 1529 in src/_pytest/config/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/config/__init__.py#L1529

Added line #L1529 was not covered by tests
return []
else:
value = override_value
Expand Down
27 changes: 27 additions & 0 deletions testing/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,33 @@ def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None:
assert len(set(pm.get_plugins()) - {pm}) == 0


def test_my_option(pytester: Pytester):
pytester.makeconftest(
"""\
import pytest
def pytest_addoption(parser):
parser.addini(
"my_option",
type="string",
default=None,
help="My option",
)

@pytest.fixture(scope='session')
def my_option(request):
return request.config.getini("my_option")
"""
)
pytester.makepyfile(
"""\
def test_x(my_option):
assert my_option is None
"""
)
res = pytester.runpytest()
assert res.ret == 0


def test_conftest_confcutdir(pytester: Pytester) -> None:
pytester.makeconftest("assert 0")
x = pytester.mkdir("x")
Expand Down