From ecfab4dc8b6ea33fef7a8f4880cf8e745679bc15 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 10 Jul 2023 23:55:00 +0300 Subject: [PATCH] fixtures: fix a typing ignore TODO From understanding the code better I see this is the correct fix. The fixturedefs can be None if `request.getfixturevalue("doesnotexist")` is used. In practice there is no change in behavior because this mapping is used as `self._arg2fixturedefs.get(argname, None)` which ends up the same. --- src/_pytest/fixtures.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 04c4c85d3..41a6504cc 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -464,12 +464,13 @@ class FixtureRequest: assert self._pyfuncitem.parent is not None parentid = self._pyfuncitem.parent.nodeid fixturedefs = self._fixturemanager.getfixturedefs(argname, parentid) - # TODO: Fix this type ignore. Either add assert or adjust types. - # Can this be None here? - self._arg2fixturedefs[argname] = fixturedefs # type: ignore[assignment] + if fixturedefs is not None: + self._arg2fixturedefs[argname] = fixturedefs + if fixturedefs is None: + raise FixtureLookupError(argname, self) # fixturedefs list is immutable so we maintain a decreasing index. index = self._arg2index.get(argname, 0) - 1 - if fixturedefs is None or (-index > len(fixturedefs)): + if -index > len(fixturedefs): raise FixtureLookupError(argname, self) self._arg2index[argname] = index return fixturedefs[index]