Skip to content

Commit fdc1b10

Browse files
Apply comments
1 parent e653fa9 commit fdc1b10

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

src/_pytest/fixtures.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,24 @@ def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]:
241241

242242

243243
@dataclasses.dataclass(frozen=True)
244-
class FixtureArgKey:
244+
class FixtureArgKeyByIndex:
245245
argname: str
246-
param_index: Optional[int]
247-
param_value: Optional[Hashable]
246+
param_index: int
248247
scoped_item_path: Optional[Path]
249248
item_cls: Optional[type]
250249

251250

251+
@dataclasses.dataclass(frozen=True)
252+
class FixtureArgKeyByValue:
253+
argname: str
254+
param_value: Hashable
255+
scoped_item_path: Optional[Path]
256+
item_cls: Optional[type]
257+
258+
259+
FixtureArgKey = Union[FixtureArgKeyByIndex, FixtureArgKeyByValue]
260+
261+
252262
def get_parametrized_fixture_keys(
253263
item: nodes.Item, scope: Scope
254264
) -> Iterator[FixtureArgKey]:
@@ -268,13 +278,6 @@ def get_parametrized_fixture_keys(
268278
if cs._arg2scope[argname] != scope:
269279
continue
270280

271-
param_index: Optional[int] = cs.indices[argname]
272-
param_value = cs.params[argname]
273-
if isinstance(cs.params[argname], Hashable):
274-
param_index = None
275-
else:
276-
param_value = None
277-
278281
item_cls = None
279282
if scope is Scope.Session:
280283
scoped_item_path = None
@@ -288,9 +291,16 @@ def get_parametrized_fixture_keys(
288291
else:
289292
assert_never(scope)
290293

291-
yield FixtureArgKey(
292-
argname, param_index, param_value, scoped_item_path, item_cls
293-
)
294+
param_index = cs.indices[argname]
295+
param_value = cs.params[argname]
296+
if isinstance(param_value, Hashable):
297+
yield FixtureArgKeyByValue(
298+
argname, param_value, scoped_item_path, item_cls
299+
)
300+
else:
301+
yield FixtureArgKeyByIndex( # type: ignore[unreachable]
302+
argname, param_index, scoped_item_path, item_cls
303+
)
294304

295305

296306
# Algorithm for sorting on a per-parametrized resource setup basis.

testing/python/fixtures.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from _pytest.pytester import get_public_names
1313
from _pytest.pytester import Pytester
1414
from _pytest.python import Function
15+
from _pytest.scope import Scope
1516

1617

1718
def test_getfuncargnames_functions():
@@ -4683,3 +4684,54 @@ def test_3(fixture):
46834684
r" <Function test_3\[2\]>",
46844685
]
46854686
)
4687+
4688+
4689+
def test_get_parametrized_fixture_keys_with_unhashable_params(
4690+
pytester: Pytester,
4691+
) -> None:
4692+
module = pytester.makepyfile(
4693+
"""
4694+
import pytest
4695+
4696+
@pytest.mark.parametrize("arg", [[1], [2]], scope='module')
4697+
def test(arg):
4698+
pass
4699+
"""
4700+
)
4701+
test_0, test_1 = pytester.genitems((pytester.getmodulecol(module),))
4702+
test_0_keys = list(fixtures.get_parametrized_fixture_keys(test_0, Scope.Module))
4703+
test_1_keys = list(fixtures.get_parametrized_fixture_keys(test_1, Scope.Module))
4704+
assert len(test_0_keys) == len(test_1_keys) == 1
4705+
assert isinstance(test_0_keys[0], fixtures.FixtureArgKeyByIndex)
4706+
assert test_0_keys[0].param_index == 0
4707+
assert isinstance(test_1_keys[0], fixtures.FixtureArgKeyByIndex)
4708+
assert test_1_keys[0].param_index == 1
4709+
4710+
4711+
def test_reordering_with_unhashable_parametrize_args(pytester: Pytester) -> None:
4712+
pytester.makepyfile(
4713+
"""
4714+
import pytest
4715+
4716+
@pytest.mark.parametrize("arg", [[1], [2]], scope='module')
4717+
def test_1(arg):
4718+
print(arg)
4719+
4720+
def test_2():
4721+
print("test_2")
4722+
4723+
@pytest.mark.parametrize("arg", [[3], [4]], scope='module')
4724+
def test_3(arg):
4725+
print(arg)
4726+
"""
4727+
)
4728+
result = pytester.runpytest("-s")
4729+
result.stdout.fnmatch_lines(
4730+
[
4731+
r"*1*",
4732+
r"*3*",
4733+
r"*2*",
4734+
r"*4*",
4735+
r"*test_2*",
4736+
]
4737+
)

0 commit comments

Comments
 (0)