From 09e6ee20e784f7e7864bfe72480426a074c9fd6e Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 14 Nov 2024 15:28:42 +0100
Subject: [PATCH] add assertion to FixtureLookupError.formatrepr
---
src/_pytest/fixtures.py | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py
index 6b882fa351..8f275e4d62 100644
--- a/src/_pytest/fixtures.py
+++ b/src/_pytest/fixtures.py
@@ -805,6 +805,15 @@ def formatrepr(self) -> FixtureLookupErrorRepr:
stack = [self.request._pyfuncitem.obj]
stack.extend(map(lambda x: x.func, self.fixturestack))
msg = self.msg
+ # This function currently makes an assumption that a non-None msg means we
+ # have a non-empty `self.fixturestack`. This is currently true, but if
+ # somebody at some point want to extend the use of FixtureLookupError to
+ # new cases it might break.
+ # Add the assert to make it clearer to developer that this will fail, otherwise
+ # it crashes because `fspath` does not get set due to `stack` being empty.
+ assert (
+ self.msg is None or self.fixturestack
+ ), "formatrepr assumptions broken, rewrite it to handle it"
if msg is not None:
# The last fixture raise an error, let's present
# it at the requesting side.