Skip to content

Commit 22e3da4

Browse files
committed
Use explicit type checks
1 parent e5dce8c commit 22e3da4

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/_pytest/config/__init__.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from typing import IO
3434
from typing import TextIO
3535
from typing import TYPE_CHECKING
36-
from typing import Union
3736
import warnings
3837

3938
import pluggy
@@ -1646,7 +1645,10 @@ def _getini(self, name: str):
16461645
if self.inipath is not None
16471646
else self.invocation_params.dir
16481647
)
1649-
value = cast(Union[str, list[str]], value)
1648+
if not isinstance(value, (str, Sequence)):
1649+
raise TypeError(
1650+
f"Expected str or sequence for option {name} of type str/list, but got: {value!r}"
1651+
) from None
16501652
input_values = shlex.split(value) if isinstance(value, str) else value
16511653
return [dp / x for x in input_values]
16521654
elif type == "args":
@@ -1661,25 +1663,24 @@ def _getini(self, name: str):
16611663
elif type == "string":
16621664
return value
16631665
elif type == "int":
1664-
value = cast(str, value)
1665-
try:
1666-
return int(value)
1667-
except ValueError:
1668-
raise ValueError(
1669-
f"invalid integer value for option {name}: {value!r}"
1666+
if not isinstance(value, (str, int)):
1667+
raise TypeError(
1668+
f"Expected str or int for option {name} of type integer, but got: {value!r}"
16701669
) from None
1670+
return int(value)
16711671
elif type == "float":
1672-
value = cast(str, value)
1673-
try:
1674-
return float(value)
1675-
except ValueError:
1676-
raise ValueError(
1677-
f"invalid float value for option {name}: {value!r}"
1672+
if not isinstance(value, (str, float)):
1673+
raise TypeError(
1674+
f"Expected str or flot for option {name} of type float, but got: {value!r}"
16781675
) from None
1676+
return float(value)
16791677
elif type is None:
16801678
return value
16811679
else:
1682-
value = cast(Union[str, list[str]], value)
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
16831684
return self._getini_unknown_type(name, type, value)
16841685

16851686
def _getconftest_pathlist(

0 commit comments

Comments
 (0)