Skip to content

Commit

Permalink
Merge pull request #11301 from bluetech/fixtures-resolve-directness
Browse files Browse the repository at this point in the history
python: use clearer terminology for `_resolve_arg_value_types`
  • Loading branch information
bluetech committed Aug 10, 2023
2 parents 09b7873 + 3ad3fc6 commit 556e075
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,9 @@ def parametrize(
name2pseudofixturedef = node.stash.setdefault(
name2pseudofixturedef_key, default
)
arg_values_types = self._resolve_arg_value_types(argnames, indirect)
arg_directness = self._resolve_args_directness(argnames, indirect)
for argname in argnames:
if arg_values_types[argname] == "params":
if arg_directness[argname] == "indirect":
continue
if name2pseudofixturedef is not None and argname in name2pseudofixturedef:
fixturedef = name2pseudofixturedef[argname]
Expand Down Expand Up @@ -1470,28 +1470,30 @@ def _validate_ids(

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

def _resolve_arg_value_types(
def _resolve_args_directness(
self,
argnames: Sequence[str],
indirect: Union[bool, Sequence[str]],
) -> Dict[str, "Literal['params', 'funcargs']"]:
"""Resolve if each parametrized argument must be considered a
parameter to a fixture or a "funcarg" to the function, based on the
``indirect`` parameter of the parametrized() call.
) -> Dict[str, Literal["indirect", "direct"]]:
"""Resolve if each parametrized argument must be considered an indirect
parameter to a fixture of the same name, or a direct parameter to the
parametrized function, based on the ``indirect`` parameter of the
parametrized() call.
:param List[str] argnames: List of argument names passed to ``parametrize()``.
:param indirect: Same as the ``indirect`` parameter of ``parametrize()``.
:rtype: Dict[str, str]
A dict mapping each arg name to either:
* "params" if the argname should be the parameter of a fixture of the same name.
* "funcargs" if the argname should be a parameter to the parametrized test function.
:param argnames:
List of argument names passed to ``parametrize()``.
:param indirect:
Same as the ``indirect`` parameter of ``parametrize()``.
:returns
A dict mapping each arg name to either "indirect" or "direct".
"""
arg_directness: Dict[str, Literal["indirect", "direct"]]
if isinstance(indirect, bool):
valtypes: Dict[str, Literal["params", "funcargs"]] = dict.fromkeys(
argnames, "params" if indirect else "funcargs"
arg_directness = dict.fromkeys(
argnames, "indirect" if indirect else "direct"
)
elif isinstance(indirect, Sequence):
valtypes = dict.fromkeys(argnames, "funcargs")
arg_directness = dict.fromkeys(argnames, "direct")
for arg in indirect:
if arg not in argnames:
fail(
Expand All @@ -1500,15 +1502,15 @@ def _resolve_arg_value_types(
),
pytrace=False,
)
valtypes[arg] = "params"
arg_directness[arg] = "indirect"
else:
fail(
"In {func}: expected Sequence or boolean for indirect, got {type}".format(
type=type(indirect).__name__, func=self.function.__name__
),
pytrace=False,
)
return valtypes
return arg_directness

def _validate_if_using_arg_names(
self,
Expand Down

0 comments on commit 556e075

Please sign in to comment.