From 5185872f2047ee5d63cd4becf96503ab9032607a Mon Sep 17 00:00:00 2001 From: Dr Alex Mitre Date: Fri, 1 May 2026 05:25:07 -0600 Subject: [PATCH] docs: guard pytestconfig.cache example when cacheprovider is disabled (#14254) Update the `config.cache` example in `doc/en/how-to/cache.rst` to safely handle cases where the `cacheprovider` plugin is disabled. Closes #14148 (cherry picked from commit 2aa07343b6ebcb426f4cc595e3bf1912b0a442de) --- changelog/14148.doc.rst | 2 ++ doc/en/how-to/cache.rst | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelog/14148.doc.rst diff --git a/changelog/14148.doc.rst b/changelog/14148.doc.rst new file mode 100644 index 00000000000..b54ae2cd8e3 --- /dev/null +++ b/changelog/14148.doc.rst @@ -0,0 +1,2 @@ +Documented a safe ``pytestconfig.cache`` access pattern when the +``cacheprovider`` plugin is disabled. diff --git a/doc/en/how-to/cache.rst b/doc/en/how-to/cache.rst index ca345916fc5..c030e487563 100644 --- a/doc/en/how-to/cache.rst +++ b/doc/en/how-to/cache.rst @@ -214,11 +214,18 @@ across pytest invocations: @pytest.fixture def mydata(pytestconfig): - val = pytestconfig.cache.get("example/value", None) + cache = getattr(pytestconfig, "cache", None) + if cache is None: + # pytestconfig not having the cache attribute means the + # cache plugin is disabled. + expensive_computation() + return 42 + + val = cache.get("example/value", None) if val is None: expensive_computation() val = 42 - pytestconfig.cache.set("example/value", val) + cache.set("example/value", val) return val