Skip to content

Commit b28195f

Browse files
committed
Code review
1 parent 22e3da4 commit b28195f

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/_pytest/config/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,8 +1607,10 @@ def getini(self, name: str):
16071607

16081608
# Meant for easy monkeypatching by legacypath plugin.
16091609
# Can be inlined back (with no cover removed) once legacypath is gone.
1610-
def _getini_unknown_type(self, name: str, type: str, value: str | list[str]):
1611-
msg = f"unknown configuration type: {type}"
1610+
def _getini_unknown_type(self, name: str, type: str, value: object):
1611+
msg = (
1612+
f"Option {name} has unknown configuration type {type} with value {value!r}"
1613+
)
16121614
raise ValueError(msg, value) # pragma: no cover
16131615

16141616
def _getini(self, name: str):
@@ -1671,16 +1673,12 @@ def _getini(self, name: str):
16711673
elif type == "float":
16721674
if not isinstance(value, (str, float)):
16731675
raise TypeError(
1674-
f"Expected str or flot for option {name} of type float, but got: {value!r}"
1676+
f"Expected str or float for option {name} of type float, but got: {value!r}"
16751677
) from None
16761678
return float(value)
16771679
elif type is None:
16781680
return value
16791681
else:
1680-
if not isinstance(value, (str, Sequence)):
1681-
raise TypeError(
1682-
f"Expected str or sequence for option {name} of type str/list, but got: {value!r}"
1683-
) from None
16841682
return self._getini_unknown_type(name, type, value)
16851683

16861684
def _getconftest_pathlist(

src/_pytest/config/findpaths.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
from typing_extensions import TypeAlias
2323

24-
ConfigDict: TypeAlias = dict[str, Union[str, int, float, list[str]]]
24+
# Even though TOML supports richer data types, all values are converted to str/list[str] during
25+
# parsing to maintain compatibility with the rest of the configuration system.
26+
ConfigDict: TypeAlias = dict[str, Union[str, list[str]]]
2527

2628

2729
def _parse_ini_config(path: Path) -> iniconfig.IniConfig:

testing/test_config.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,23 @@ def pytest_addoption(parser):
866866
config = pytester.parseconfig()
867867
assert config.getini("ini_param") == int_val
868868

869+
def test_addini_int_invalid(self, pytester: Pytester) -> None:
870+
pytester.makeconftest(
871+
"""
872+
def pytest_addoption(parser):
873+
parser.addini("ini_param", "", type="int", default=2)
874+
"""
875+
)
876+
pytester.makepyprojecttoml(
877+
"""
878+
[tool.pytest.ini_options]
879+
ini_param=["foo"]
880+
"""
881+
)
882+
config = pytester.parseconfig()
883+
with pytest.raises(TypeError, match="Expected str or int for option ini_param"):
884+
_ = config.getini("ini_param")
885+
869886
@pytest.mark.parametrize("str_val, float_val", [("10.5", 10.5), ("no-ini", 2.2)])
870887
def test_addini_float(
871888
self, pytester: Pytester, str_val: str, float_val: bool
@@ -886,6 +903,25 @@ def pytest_addoption(parser):
886903
config = pytester.parseconfig()
887904
assert config.getini("ini_param") == float_val
888905

906+
def test_addini_float_invalid(self, pytester: Pytester) -> None:
907+
pytester.makeconftest(
908+
"""
909+
def pytest_addoption(parser):
910+
parser.addini("ini_param", "", type="float", default=2.2)
911+
"""
912+
)
913+
pytester.makepyprojecttoml(
914+
"""
915+
[tool.pytest.ini_options]
916+
ini_param=["foo"]
917+
"""
918+
)
919+
config = pytester.parseconfig()
920+
with pytest.raises(
921+
TypeError, match="Expected str or float for option ini_param"
922+
):
923+
_ = config.getini("ini_param")
924+
889925
def test_addinivalue_line_existing(self, pytester: Pytester) -> None:
890926
pytester.makeconftest(
891927
"""

0 commit comments

Comments
 (0)