diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 8882486b0..7f3d479e7 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -15,7 +15,6 @@ from typing import Final from typing import final from typing import Generator from typing import Generic -from typing import Hashable from typing import Iterable from typing import Iterator from typing import List @@ -241,24 +240,13 @@ def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]: @dataclasses.dataclass(frozen=True) -class FixtureArgKeyByIndex: +class FixtureArgKey: argname: str param_index: int scoped_item_path: Optional[Path] item_cls: Optional[type] -@dataclasses.dataclass(frozen=True) -class FixtureArgKeyByValue: - argname: str - param_value: Hashable - scoped_item_path: Optional[Path] - item_cls: Optional[type] - - -FixtureArgKey = Union[FixtureArgKeyByIndex, FixtureArgKeyByValue] - - def get_parametrized_fixture_keys( item: nodes.Item, scope: Scope ) -> Iterator[FixtureArgKey]: @@ -292,15 +280,7 @@ def get_parametrized_fixture_keys( assert_never(scope) param_index = cs.indices[argname] - param_value = cs.params[argname] - if isinstance(param_value, Hashable): - yield FixtureArgKeyByValue( - argname, param_value, scoped_item_path, item_cls - ) - else: - yield FixtureArgKeyByIndex( # type: ignore[unreachable] - argname, param_index, scoped_item_path, item_cls - ) + yield FixtureArgKey(argname, param_index, scoped_item_path, item_cls) # Algorithm for sorting on a per-parametrized resource setup basis. diff --git a/testing/example_scripts/issue_519.py b/testing/example_scripts/issue_519.py index 73437ef7b..e44367fca 100644 --- a/testing/example_scripts/issue_519.py +++ b/testing/example_scripts/issue_519.py @@ -22,13 +22,13 @@ def checked_order(): assert order == [ ("issue_519.py", "fix1", "arg1v1"), ("test_one[arg1v1-arg2v1]", "fix2", "arg2v1"), - ("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"), ("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"), + ("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"), ("test_two[arg1v1-arg2v2]", "fix2", "arg2v2"), ("issue_519.py", "fix1", "arg1v2"), ("test_one[arg1v2-arg2v1]", "fix2", "arg2v1"), - ("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"), ("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"), + ("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"), ("test_two[arg1v2-arg2v2]", "fix2", "arg2v2"), ] diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 1e05bf4c4..5f855e6b4 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -12,7 +12,6 @@ from _pytest.monkeypatch import MonkeyPatch from _pytest.pytester import get_public_names from _pytest.pytester import Pytester from _pytest.python import Function -from _pytest.scope import Scope def test_getfuncargnames_functions(): @@ -4539,115 +4538,6 @@ def test_yield_fixture_with_no_value(pytester: Pytester) -> None: assert result.ret == ExitCode.TESTS_FAILED -@pytest.mark.parametrize("scope", ["module", "package"]) -def test_basing_fixture_argkeys_on_param_values_rather_than_on_param_indices( - scope, - pytester: Pytester, -): - package = pytester.mkdir("package") - package.joinpath("__init__.py").write_text("", encoding="utf-8") - package.joinpath("test_a.py").write_text( - textwrap.dedent( - f"""\ - import pytest - - @pytest.fixture(scope='{scope}') - def fixture1(request): - pass - - @pytest.mark.parametrize("fixture1", [1, 0], indirect=True) - def test_0(fixture1): - pass - - @pytest.mark.parametrize("fixture1", [2, 1], indirect=True) - def test_1(fixture1): - pass - - def test_2(): - pass - - @pytest.mark.parametrize("param", [0, 1, 2], scope='{scope}') - def test_3(param): - pass - - @pytest.mark.parametrize("param", [2, 1, 0], scope='{scope}') - def test_4(param): - pass - """ - ), - encoding="utf-8", - ) - result = pytester.runpytest("--collect-only") - result.stdout.re_match_lines( - [ - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - ] - ) - - -def test_basing_fixture_argkeys_on_param_values_rather_than_on_param_indices_2( - pytester: Pytester, -): - pytester.makepyfile( - """ - import pytest - - @pytest.fixture(scope='module') - def fixture1(request): - pass - - @pytest.fixture(scope='module') - def fixture2(request): - pass - - @pytest.mark.parametrize("fixture1, fixture2", [("a", 0), ("b", 1), ("a", 2)], indirect=True) - def test_1(fixture1, fixture2): - pass - - @pytest.mark.parametrize("fixture1, fixture2", [("c", 4), ("a", 3)], indirect=True) - def test_2(fixture1, fixture2): - pass - - def test_3(): - pass - - @pytest.mark.parametrize("param1, param2", [("a", 0), ("b", 1), ("a", 2)], scope='module') - def test_4(param1, param2): - pass - - @pytest.mark.parametrize("param1, param2", [("c", 4), ("a", 3)], scope='module') - def test_5(param1, param2): - pass - """ - ) - result = pytester.runpytest("--collect-only") - result.stdout.re_match_lines( - [ - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - r" ", - ] - ) - - @pytest.mark.xfail( reason="It isn't differentiated between direct `fixture` param and fixture `fixture`. Will be" "solved by adding `baseid` to `FixtureArgKey`." @@ -4684,54 +4574,3 @@ def test_reorder_with_high_scoped_direct_and_fixture_parametrization( r" ", ] ) - - -def test_get_parametrized_fixture_keys_with_unhashable_params( - pytester: Pytester, -) -> None: - module = pytester.makepyfile( - """ - import pytest - - @pytest.mark.parametrize("arg", [[1], [2]], scope='module') - def test(arg): - pass - """ - ) - test_0, test_1 = pytester.genitems((pytester.getmodulecol(module),)) - test_0_keys = list(fixtures.get_parametrized_fixture_keys(test_0, Scope.Module)) - test_1_keys = list(fixtures.get_parametrized_fixture_keys(test_1, Scope.Module)) - assert len(test_0_keys) == len(test_1_keys) == 1 - assert isinstance(test_0_keys[0], fixtures.FixtureArgKeyByIndex) - assert test_0_keys[0].param_index == 0 - assert isinstance(test_1_keys[0], fixtures.FixtureArgKeyByIndex) - assert test_1_keys[0].param_index == 1 - - -def test_reordering_with_unhashable_parametrize_args(pytester: Pytester) -> None: - pytester.makepyfile( - """ - import pytest - - @pytest.mark.parametrize("arg", [[1], [2]], scope='module') - def test_1(arg): - print(arg) - - def test_2(): - print("test_2") - - @pytest.mark.parametrize("arg", [[3], [4]], scope='module') - def test_3(arg): - print(arg) - """ - ) - result = pytester.runpytest("-s") - result.stdout.fnmatch_lines( - [ - r"*1*", - r"*3*", - r"*2*", - r"*4*", - r"*test_2*", - ] - )