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

Improvement for Parser.addini to facilite parsing of 'int' and 'float' values #13193

Merged
merged 25 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
259987e
#11381
harmin-parra Feb 4, 2025
db57e8a
#11381
harmin-parra Feb 4, 2025
181716d
Create 11381.improvement.rst
harmin-parra Feb 4, 2025
344a723
Update 11381.improvement.rst
harmin-parra Feb 4, 2025
ba27f9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
3b4fb75
tests for #11381
harmin-parra Feb 4, 2025
f2cbed2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
04224b8
Update __init__.py
harmin-parra Feb 4, 2025
311e5c0
Update findpaths.py
harmin-parra Feb 4, 2025
68cd042
Update src/_pytest/config/argparsing.py
harmin-parra Feb 4, 2025
60acd06
Update src/_pytest/config/__init__.py
harmin-parra Feb 4, 2025
a87f4a7
Update src/_pytest/config/__init__.py
harmin-parra Feb 4, 2025
4d219b6
Update changelog/11381.improvement.rst
harmin-parra Feb 4, 2025
58f9582
Update changelog/11381.improvement.rst
harmin-parra Feb 4, 2025
7270cd3
Update changelog/11381.improvement.rst
harmin-parra Feb 4, 2025
65cf408
Update changelog/11381.improvement.rst
harmin-parra Feb 4, 2025
042f608
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
2802d79
Fix tests and typing
nicoddemus Feb 5, 2025
522a41a
Update test_config.py
harmin-parra Feb 5, 2025
e5dce8c
Update test_config.py
harmin-parra Feb 5, 2025
22e3da4
Use explicit type checks
nicoddemus Feb 8, 2025
b28195f
Code review
nicoddemus Feb 9, 2025
b302d31
Fix explicit checks
nicoddemus Feb 9, 2025
8eac202
Update src/_pytest/config/__init__.py
nicoddemus Feb 15, 2025
946b963
Merge branch 'main' into main
harmin-parra Feb 15, 2025
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
27 changes: 27 additions & 0 deletions changelog/11381.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
The ``type`` parameter of the ``parser.addini`` method now accepts the **int** and **float** literals, facilitating the parsing of configuration values in the **ini** file.
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved

Example:

.. code-block:: pytest
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved

def pytest_addoption(parser):
parser.addini(
"int_value",
type="int",
default=2,
help="my int value"
)
parser.addini(
"float_value",
type="float",
default=4.2,
help="my float value"
)

The **pytest.ini** file:
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: pytest
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved

[pytest]
int_value = 3
float_value = 5.4
12 changes: 12 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,8 @@ def getini(self, name: str):
``paths``, ``pathlist``, ``args`` and ``linelist`` : empty list ``[]``
``bool`` : ``False``
``string`` : empty string ``""``
``int`` : ``0``
``float`` : ``0.0``

If neither the ``default`` nor the ``type`` parameter is passed
while registering the configuration through
Expand Down Expand Up @@ -1656,6 +1658,16 @@ def _getini(self, name: str):
return _strtobool(str(value).strip())
elif type == "string":
return value
elif type == "int":
try:
return int(value)
except ValueError:
raise ValueError(f"invalid integer value {value!r}") from None
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved
elif type == "float":
try:
return float(value)
except ValueError:
raise ValueError(f"invalid float value {value!r}") from None
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved
elif type is None:
return value
else:
Expand Down
23 changes: 21 additions & 2 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ def addini(
* ``linelist``: a list of strings, separated by line breaks
* ``paths``: a list of :class:`pathlib.Path`, separated as in a shell
* ``pathlist``: a list of ``py.path``, separated as in a shell
* ``int``: an integer
* ``float``: a floating-point number
harmin-parra marked this conversation as resolved.
Show resolved Hide resolved

For ``paths`` and ``pathlist`` types, they are considered relative to the ini-file.
In case the execution is happening without an ini-file defined,
Expand All @@ -209,7 +211,17 @@ def addini(
The value of ini-variables can be retrieved via a call to
:py:func:`config.getini(name) <pytest.Config.getini>`.
"""
assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool")
assert type in (
None,
"string",
"paths",
"pathlist",
"args",
"linelist",
"bool",
"int",
"float",
)
if default is NOT_SET:
default = get_ini_default_for_type(type)

Expand All @@ -218,7 +230,10 @@ def addini(


def get_ini_default_for_type(
type: Literal["string", "paths", "pathlist", "args", "linelist", "bool"] | None,
type: Literal[
"string", "paths", "pathlist", "args", "linelist", "bool", "int", "float"
]
| None,
) -> Any:
"""
Used by addini to get the default value for a given ini-option type, when
Expand All @@ -230,6 +245,10 @@ def get_ini_default_for_type(
return []
elif type == "bool":
return False
elif type == "int":
return 0
elif type == "float":
return 0.0
else:
return ""

Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def determine_setup(
args: Sequence[str],
rootdir_cmd_arg: str | None,
invocation_dir: Path,
) -> tuple[Path, Path | None, dict[str, str | list[str]]]:
) -> tuple[Path, Path | None, dict[str, int | float | str | list[str]]]:
"""Determine the rootdir, inifile and ini configuration values from the
command line arguments.

Expand Down
38 changes: 38 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,44 @@ def pytest_addoption(parser):
config = pytester.parseconfig()
assert config.getini("strip") is bool_val

@pytest.mark.parametrize("str_val, int_val", [("10", 10), ("no-ini", 0)])
def test_addini_int(self, pytester: Pytester, str_val: str, int_val: bool) -> None:
pytester.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("ini_param", "", type="int", default=2)
"""
)
if str_val != "no-ini":
pytester.makeini(
f"""
[pytest]
ini_param={str_val}
"""
)
config = pytester.parseconfig()
assert config.getini("ini_param") == int_val

@pytest.mark.parametrize("str_val, float_val", [("10.5", 10.5), ("no-ini", 0.0)])
def test_addini_float(
self, pytester: Pytester, str_val: str, float_val: bool
) -> None:
pytester.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("ini_param", "", type="float", default=2.2)
"""
)
if str_val != "no-ini":
pytester.makeini(
f"""
[pytest]
ini_param={str_val}
"""
)
config = pytester.parseconfig()
assert config.getini("ini_param") == float_val

def test_addinivalue_line_existing(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
Expand Down
Loading