From 12054a49722eee2b12e3b9106f3cf878bcb13648 Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Thu, 27 Jul 2023 09:32:23 +0300 Subject: [PATCH 1/2] config: avoid list[], set[], dict[] Should wait with this until Python 3.8 is dropped. --- src/_pytest/config/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index be62fe999..2b6f250f3 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -353,9 +353,9 @@ def _get_legacy_hook_marks( if TYPE_CHECKING: # abuse typeguard from importlib to avoid massive method type union thats lacking a alias assert inspect.isroutine(method) - known_marks: set[str] = {m.name for m in getattr(method, "pytestmark", [])} - must_warn: list[str] = [] - opts: dict[str, bool] = {} + known_marks: Set[str] = {m.name for m in getattr(method, "pytestmark", [])} + must_warn: List[str] = [] + opts: Dict[str, bool] = {} for opt_name in opt_names: opt_attr = getattr(method, opt_name, AttributeError) if opt_attr is not AttributeError: From e8aa906e06f696f28d2ae4ce205e099aab18a7a8 Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Thu, 27 Jul 2023 09:36:30 +0300 Subject: [PATCH 2/2] fixtures: move _get_direct_parametrize_args to a standalone function So it can be used independently of the FixtureManager. --- AUTHORS | 1 + src/_pytest/fixtures.py | 41 +++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/AUTHORS b/AUTHORS index 4c42de21f..313e507f2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -327,6 +327,7 @@ Ross Lawley Ruaridh Williamson Russel Winder Ryan Wooden +Sadra Barikbin Saiprasad Kale Samuel Colvin Samuel Dion-Girardeau diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 650e934b3..516a595ef 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1404,6 +1404,26 @@ def pytest_addoption(parser: Parser) -> None: ) +def _get_direct_parametrize_args(node: nodes.Node) -> List[str]: + """Return all direct parametrization arguments of a node, so we don't + mistake them for fixtures. + + Check https://github.com/pytest-dev/pytest/issues/5036. + + These things are done later as well when dealing with parametrization + so this could be improved. + """ + parametrize_argnames: List[str] = [] + for marker in node.iter_markers(name="parametrize"): + if not marker.kwargs.get("indirect", False): + p_argnames, _ = ParameterSet._parse_parametrize_args( + *marker.args, **marker.kwargs + ) + parametrize_argnames.extend(p_argnames) + + return parametrize_argnames + + class FixtureManager: """pytest fixture definitions and information is stored and managed from this class. @@ -1453,25 +1473,6 @@ class FixtureManager: } session.config.pluginmanager.register(self, "funcmanage") - def _get_direct_parametrize_args(self, node: nodes.Node) -> List[str]: - """Return all direct parametrization arguments of a node, so we don't - mistake them for fixtures. - - Check https://github.com/pytest-dev/pytest/issues/5036. - - These things are done later as well when dealing with parametrization - so this could be improved. - """ - parametrize_argnames: List[str] = [] - for marker in node.iter_markers(name="parametrize"): - if not marker.kwargs.get("indirect", False): - p_argnames, _ = ParameterSet._parse_parametrize_args( - *marker.args, **marker.kwargs - ) - parametrize_argnames.extend(p_argnames) - - return parametrize_argnames - def getfixtureinfo( self, node: nodes.Item, @@ -1503,7 +1504,7 @@ class FixtureManager: ) initialnames = usefixtures + argnames initialnames, names_closure, arg2fixturedefs = self.getfixtureclosure( - initialnames, node, ignore_args=self._get_direct_parametrize_args(node) + initialnames, node, ignore_args=_get_direct_parametrize_args(node) ) return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)