Skip to content

Commit 3f38901

Browse files
Improve tests and fix Mypy issues
1 parent f4bef42 commit 3f38901

File tree

4 files changed

+78
-40
lines changed

4 files changed

+78
-40
lines changed

src/_pytest/config/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ def _get_legacy_hook_marks(
353353
if TYPE_CHECKING:
354354
# abuse typeguard from importlib to avoid massive method type union thats lacking a alias
355355
assert inspect.isroutine(method)
356-
known_marks: set[str] = {m.name for m in getattr(method, "pytestmark", [])}
357-
must_warn: list[str] = []
358-
opts: dict[str, bool] = {}
356+
known_marks: Set[str] = {m.name for m in getattr(method, "pytestmark", [])}
357+
must_warn: List[str] = []
358+
opts: Dict[str, bool] = {}
359359
for opt_name in opt_names:
360360
opt_attr = getattr(method, opt_name, AttributeError)
361361
if opt_attr is not AttributeError:

src/_pytest/python.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pathlib import Path
1515
from typing import Any
1616
from typing import Callable
17+
from typing import cast
1718
from typing import Dict
1819
from typing import final
1920
from typing import Generator
@@ -1221,7 +1222,7 @@ def resolve_values_indices_in_parametersets(
12211222
argname_indices.append(argvalues_count[argname])
12221223
argvalues_count[argname] += 1
12231224
indices.append(argname_indices)
1224-
return list(zip(*indices))
1225+
return list(cast(Iterable[Tuple[int]], zip(*indices)))
12251226

12261227

12271228
# Used for storing artificial fixturedefs for direct parametrization.
@@ -1378,7 +1379,9 @@ def parametrize(
13781379
ids = self._resolve_parameter_set_ids(
13791380
argnames, ids, parametersets, nodeid=self.definition.nodeid
13801381
)
1381-
param_indices_list = resolve_values_indices_in_parametersets(argnames, parametersets)
1382+
param_indices_list = resolve_values_indices_in_parametersets(
1383+
argnames, parametersets
1384+
)
13821385
# Store used (possibly generated) ids with parametrize Marks.
13831386
if _param_mark and _param_mark._param_ids_from and generated_ids is None:
13841387
object.__setattr__(_param_mark._param_ids_from, "_param_ids_generated", ids)
@@ -1389,7 +1392,7 @@ def parametrize(
13891392
arg2fixturedefs = self._arg2fixturedefs
13901393
node = None
13911394
if scope_ is not Scope.Function:
1392-
node = get_scope_node(self.definition.parent, scope_)
1395+
node = get_scope_node(cast(nodes.Node, self.definition.parent), scope_)
13931396
if node is None:
13941397
assert scope_ is Scope.Class and isinstance(
13951398
self.definition.parent, _pytest.python.Module

testing/python/fixtures.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4511,33 +4511,3 @@ def test_fixt(custom):
45114511
result.assert_outcomes(errors=1)
45124512
result.stdout.fnmatch_lines([expected])
45134513
assert result.ret == ExitCode.TESTS_FAILED
4514-
4515-
4516-
def test_reorder_parametrized_tests_with_parametersets_with_duplicate_values(
4517-
pytester: Pytester,
4518-
):
4519-
pytester.makepyfile(
4520-
"""
4521-
import pytest
4522-
4523-
@pytest.fixture(scope='module')
4524-
def fixture1(request):
4525-
pass
4526-
4527-
@pytest.fixture(scope='module')
4528-
def fixture2(request):
4529-
pass
4530-
4531-
@pytest.mark.parametrize("fixture1, fixture2", [("a", 0), ("b", 1), ("a", 2)], indirect=True)
4532-
def test(fixture1, fixture2):
4533-
pass
4534-
"""
4535-
)
4536-
result = pytester.runpytest("--collect-only")
4537-
result.stdout.re_match_lines(
4538-
[
4539-
r" <Function test\[a-0\]>",
4540-
r" <Function test\[a-2\]>",
4541-
r" <Function test\[b-1\]>",
4542-
]
4543-
)

testing/python/metafunc.py

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def Metafunc(self, func, config=None) -> python.Metafunc:
3333
# on the funcarg level, so we don't need a full blown
3434
# initialization.
3535
class FuncFixtureInfoMock:
36-
name2fixturedefs = {}
36+
name2fixturedefs: Dict[str, List[fixtures.FixtureDef[object]]] = {}
3737

3838
def __init__(self, names):
3939
self.names_closure = names
@@ -983,6 +983,69 @@ def test_parametrize_with_duplicate_values(self) -> None:
983983
assert metafunc._calls[2].indices == dict(x=0, y=2)
984984
assert metafunc._calls[3].indices == dict(x=2, y=0)
985985

986+
def test_high_scoped_parametrize_reordering(self, pytester: Pytester) -> None:
987+
pytester.makepyfile(
988+
"""
989+
import pytest
990+
991+
@pytest.mark.parametrize("arg2", [3, 4])
992+
@pytest.mark.parametrize("arg1", [0, 1, 2], scope='module')
993+
def test1(arg1, arg2):
994+
pass
995+
996+
def test2():
997+
pass
998+
999+
@pytest.mark.parametrize("arg1", [0, 1, 2], scope='module')
1000+
def test3(arg1):
1001+
pass
1002+
"""
1003+
)
1004+
result = pytester.runpytest("--collect-only")
1005+
result.stdout.re_match_lines(
1006+
[
1007+
r" <Function test1\[0-3\]>",
1008+
r" <Function test1\[0-4\]>",
1009+
r" <Function test3\[0\]>",
1010+
r" <Function test1\[1-3\]>",
1011+
r" <Function test1\[1-4\]>",
1012+
r" <Function test3\[1\]>",
1013+
r" <Function test1\[2-3\]>",
1014+
r" <Function test1\[2-4\]>",
1015+
r" <Function test3\[2\]>",
1016+
r" <Function test2>",
1017+
]
1018+
)
1019+
1020+
def test_high_scoped_parametrize_with_duplicate_values_reordering(
1021+
self, pytester: Pytester
1022+
) -> None:
1023+
pytester.makepyfile(
1024+
"""
1025+
import pytest
1026+
1027+
@pytest.fixture(scope='module')
1028+
def fixture1(request):
1029+
pass
1030+
1031+
@pytest.fixture(scope='module')
1032+
def fixture2(request):
1033+
pass
1034+
1035+
@pytest.mark.parametrize("fixture1, fixture2", [("a", 0), ("b", 1), ("a", 2)], indirect=True)
1036+
def test(fixture1, fixture2):
1037+
pass
1038+
"""
1039+
)
1040+
result = pytester.runpytest("--collect-only")
1041+
result.stdout.re_match_lines(
1042+
[
1043+
r" <Function test\[a-0\]>",
1044+
r" <Function test\[a-2\]>",
1045+
r" <Function test\[b-1\]>",
1046+
]
1047+
)
1048+
9861049
def test_parametrize_multiple_times(self, pytester: Pytester) -> None:
9871050
pytester.makepyfile(
9881051
"""
@@ -1504,8 +1567,10 @@ def test_foo(x):
15041567
"*= no tests collected, 1 error in *",
15051568
]
15061569
)
1507-
1508-
def test_parametrize_module_level_test_with_class_scope(self, pytester: Pytester) -> None:
1570+
1571+
def test_parametrize_module_level_test_with_class_scope(
1572+
self, pytester: Pytester
1573+
) -> None:
15091574
pytester.makepyfile(
15101575
"""
15111576
import pytest
@@ -1520,7 +1585,7 @@ def item(request):
15201585
def test_1(item, x):
15211586
global fixturedef
15221587
fixturedef = item._fixtureinfo.name2fixturedefs['x'][-1]
1523-
1588+
15241589
@pytest.mark.parametrize("x", [1, 2], scope="module")
15251590
def test_2(item, x):
15261591
global fixturedef

0 commit comments

Comments
 (0)