Skip to content

Commit 2802d79

Browse files
committed
Fix tests and typing
1 parent 042f608 commit 2802d79

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/_pytest/config/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from typing import IO
3434
from typing import TextIO
3535
from typing import TYPE_CHECKING
36+
from typing import Union
3637
import warnings
3738

3839
import pluggy
@@ -1645,6 +1646,7 @@ def _getini(self, name: str):
16451646
if self.inipath is not None
16461647
else self.invocation_params.dir
16471648
)
1649+
value = cast(Union[str, list[str]], value)
16481650
input_values = shlex.split(value) if isinstance(value, str) else value
16491651
return [dp / x for x in input_values]
16501652
elif type == "args":
@@ -1659,13 +1661,15 @@ def _getini(self, name: str):
16591661
elif type == "string":
16601662
return value
16611663
elif type == "int":
1664+
value = cast(str, value)
16621665
try:
16631666
return int(value)
16641667
except ValueError:
16651668
raise ValueError(
16661669
f"invalid integer value for option {name}: {value!r}"
16671670
) from None
16681671
elif type == "float":
1672+
value = cast(str, value)
16691673
try:
16701674
return float(value)
16711675
except ValueError:
@@ -1675,6 +1679,7 @@ def _getini(self, name: str):
16751679
elif type is None:
16761680
return value
16771681
else:
1682+
value = cast(Union[str, list[str]], value)
16781683
return self._getini_unknown_type(name, type, value)
16791684

16801685
def _getconftest_pathlist(

src/_pytest/config/findpaths.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from pathlib import Path
77
import sys
8+
from typing import TYPE_CHECKING
89

910
import iniconfig
1011

@@ -15,6 +16,14 @@
1516
from _pytest.pathlib import safe_exists
1617

1718

19+
if TYPE_CHECKING:
20+
from typing import Union
21+
22+
from typing_extensions import TypeAlias
23+
24+
ConfigDict: TypeAlias = dict[str, Union[str, int, float, list[str]]]
25+
26+
1827
def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
1928
"""Parse the given generic '.ini' file using legacy IniConfig parser, returning
2029
the parsed object.
@@ -29,7 +38,7 @@ def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
2938

3039
def load_config_dict_from_file(
3140
filepath: Path,
32-
) -> dict[str, str | list[str]] | None:
41+
) -> ConfigDict | None:
3342
"""Load pytest configuration from the given file path, if supported.
3443
3544
Return None if the file does not contain valid pytest configuration.
@@ -85,7 +94,7 @@ def make_scalar(v: object) -> str | list[str]:
8594
def locate_config(
8695
invocation_dir: Path,
8796
args: Iterable[Path],
88-
) -> tuple[Path | None, Path | None, dict[str, str | list[str]]]:
97+
) -> tuple[Path | None, Path | None, ConfigDict]:
8998
"""Search in the list of arguments for a valid ini-file for pytest,
9099
and return a tuple of (rootdir, inifile, cfg-dict)."""
91100
config_names = [
@@ -172,7 +181,7 @@ def determine_setup(
172181
args: Sequence[str],
173182
rootdir_cmd_arg: str | None,
174183
invocation_dir: Path,
175-
) -> tuple[Path, Path | None, dict[str, int | float | str | list[str]]]:
184+
) -> tuple[Path, Path | None, ConfigDict]:
176185
"""Determine the rootdir, inifile and ini configuration values from the
177186
command line arguments.
178187

testing/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ def pytest_addoption(parser):
848848
config = pytester.parseconfig()
849849
assert config.getini("strip") is bool_val
850850

851-
@pytest.mark.parametrize("str_val, int_val", [("10", 10), ("no-ini", 0)])
851+
@pytest.mark.parametrize("str_val, int_val", [("10", 10), ("no-ini", 2)])
852852
def test_addini_int(self, pytester: Pytester, str_val: str, int_val: bool) -> None:
853853
pytester.makeconftest(
854854
"""
@@ -866,7 +866,7 @@ def pytest_addoption(parser):
866866
config = pytester.parseconfig()
867867
assert config.getini("ini_param") == int_val
868868

869-
@pytest.mark.parametrize("str_val, float_val", [("10.5", 10.5), ("no-ini", 0.0)])
869+
@pytest.mark.parametrize("str_val, float_val", [("10.5", 10.5), ("no-ini", 2.2)])
870870
def test_addini_float(
871871
self, pytester: Pytester, str_val: str, float_val: bool
872872
) -> None:

0 commit comments

Comments
 (0)