From 52fbf3dbaae4139ae5f82d757b62992e1b4f0b19 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 25 Jan 2022 22:44:04 +0200 Subject: [PATCH] fixtures: make code flow clearer Make the two cases (direct/indirect fixture) clearer. The try-catch forces the reader to jump around. --- src/_pytest/fixtures.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index be03fb2a8..612fb60fd 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -597,8 +597,17 @@ class FixtureRequest: funcitem = self._pyfuncitem scope = fixturedef._scope try: - param = funcitem.callspec.getparam(argname) - except (AttributeError, ValueError): + callspec = funcitem.callspec + except AttributeError: + callspec = None + if callspec is not None and argname in callspec.params: + param = callspec.params[argname] + param_index = callspec.indices[argname] + # If a parametrize invocation set a scope it will override + # the static scope defined with the fixture function. + with suppress(KeyError): + scope = callspec._arg2scope[argname] + else: param = NOTSET param_index = 0 has_params = fixturedef.params is not None @@ -638,12 +647,6 @@ class FixtureRequest: ) ) fail(msg, pytrace=False) - else: - param_index = funcitem.callspec.indices[argname] - # If a parametrize invocation set a scope it will override - # the static scope defined with the fixture function. - with suppress(KeyError): - scope = funcitem.callspec._arg2scope[argname] subrequest = SubRequest( self, scope, param, param_index, fixturedef, _ispytest=True