Skip to content

Commit 3aabdca

Browse files
committed
python: move _resolve_args_directness function
Needed for the next commit.
1 parent 3c22636 commit 3aabdca

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

src/_pytest/fixtures.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from typing import Final
2626
from typing import final
2727
from typing import Generic
28+
from typing import Literal
2829
from typing import NoReturn
2930
from typing import overload
3031
from typing import TYPE_CHECKING
@@ -1472,6 +1473,45 @@ def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
14721473
return None
14731474

14741475

1476+
def _resolve_args_directness(
1477+
argnames: Sequence[str],
1478+
indirect: bool | Sequence[str],
1479+
nodeid: str,
1480+
) -> dict[str, Literal["indirect", "direct"]]:
1481+
"""Resolve if each parametrized argument must be considered an indirect
1482+
parameter to a fixture of the same name, or a direct parameter to the
1483+
parametrized function, based on the ``indirect`` parameter of the
1484+
parametrize() call.
1485+
1486+
:param argnames:
1487+
List of argument names passed to ``parametrize()``.
1488+
:param indirect:
1489+
Same as the ``indirect`` parameter of ``parametrize()``.
1490+
:param nodeid:
1491+
Node ID to which the parametrization is applied.
1492+
:returns:
1493+
A dict mapping each arg name to either "indirect" or "direct".
1494+
"""
1495+
arg_directness: dict[str, Literal["indirect", "direct"]]
1496+
if isinstance(indirect, bool):
1497+
arg_directness = dict.fromkeys(argnames, "indirect" if indirect else "direct")
1498+
elif isinstance(indirect, Sequence):
1499+
arg_directness = dict.fromkeys(argnames, "direct")
1500+
for arg in indirect:
1501+
if arg not in argnames:
1502+
fail(
1503+
f"In {nodeid}: indirect fixture '{arg}' doesn't exist",
1504+
pytrace=False,
1505+
)
1506+
arg_directness[arg] = "indirect"
1507+
else:
1508+
fail(
1509+
f"In {nodeid}: expected Sequence or boolean for indirect, got {type(indirect).__name__}",
1510+
pytrace=False,
1511+
)
1512+
return arg_directness
1513+
1514+
14751515
def _get_direct_parametrize_args(node: nodes.Node) -> set[str]:
14761516
"""Return all direct parametrization arguments of a node, so we don't
14771517
mistake them for fixtures.

src/_pytest/python.py

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from _pytest.config import hookimpl
5454
from _pytest.config.argparsing import Parser
5555
from _pytest.deprecated import check_ispytest
56+
from _pytest.fixtures import _resolve_args_directness
5657
from _pytest.fixtures import FixtureDef
5758
from _pytest.fixtures import FixtureRequest
5859
from _pytest.fixtures import FuncFixtureInfo
@@ -1327,7 +1328,9 @@ def parametrize(
13271328
object.__setattr__(_param_mark._param_ids_from, "_param_ids_generated", ids)
13281329

13291330
# Calculate directness.
1330-
arg_directness = self._resolve_args_directness(argnames, indirect)
1331+
arg_directness = _resolve_args_directness(
1332+
argnames, indirect, self.definition.nodeid
1333+
)
13311334
self._params_directness.update(arg_directness)
13321335

13331336
# Add direct parametrizations as fixturedefs to arg2fixturedefs by
@@ -1464,45 +1467,6 @@ def _validate_ids(
14641467

14651468
return list(itertools.islice(ids, num_ids))
14661469

1467-
def _resolve_args_directness(
1468-
self,
1469-
argnames: Sequence[str],
1470-
indirect: bool | Sequence[str],
1471-
) -> dict[str, Literal["indirect", "direct"]]:
1472-
"""Resolve if each parametrized argument must be considered an indirect
1473-
parameter to a fixture of the same name, or a direct parameter to the
1474-
parametrized function, based on the ``indirect`` parameter of the
1475-
parametrized() call.
1476-
1477-
:param argnames:
1478-
List of argument names passed to ``parametrize()``.
1479-
:param indirect:
1480-
Same as the ``indirect`` parameter of ``parametrize()``.
1481-
:returns
1482-
A dict mapping each arg name to either "indirect" or "direct".
1483-
"""
1484-
nodeid = self.definition.nodeid
1485-
arg_directness: dict[str, Literal["indirect", "direct"]]
1486-
if isinstance(indirect, bool):
1487-
arg_directness = dict.fromkeys(
1488-
argnames, "indirect" if indirect else "direct"
1489-
)
1490-
elif isinstance(indirect, Sequence):
1491-
arg_directness = dict.fromkeys(argnames, "direct")
1492-
for arg in indirect:
1493-
if arg not in argnames:
1494-
fail(
1495-
f"In {nodeid}: indirect fixture '{arg}' doesn't exist",
1496-
pytrace=False,
1497-
)
1498-
arg_directness[arg] = "indirect"
1499-
else:
1500-
fail(
1501-
f"In {nodeid}: expected Sequence or boolean for indirect, got {type(indirect).__name__}",
1502-
pytrace=False,
1503-
)
1504-
return arg_directness
1505-
15061470
def _validate_if_using_arg_names(
15071471
self,
15081472
argnames: Sequence[str],

0 commit comments

Comments
 (0)