From 21fbf0c278148292a8752374e61c618b871e5aea Mon Sep 17 00:00:00 2001 From: Dr Alex Mitre Date: Thu, 5 Mar 2026 10:58:16 -0600 Subject: [PATCH 1/3] docs: guard pytestconfig.cache example when cacheprovider is disabled --- doc/en/how-to/cache.rst | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/doc/en/how-to/cache.rst b/doc/en/how-to/cache.rst index ca345916fc5..91cf04a7b68 100644 --- a/doc/en/how-to/cache.rst +++ b/doc/en/how-to/cache.rst @@ -200,7 +200,8 @@ The new config.cache object Plugins or conftest.py support code can get a cached value using the pytest ``config`` object. Here is a basic example plugin which implements a :ref:`fixture ` which reuses previously created state -across pytest invocations: +across pytest invocations. The example below also works when the +``cacheprovider`` plugin is disabled: .. code-block:: python @@ -214,11 +215,16 @@ across pytest invocations: @pytest.fixture def mydata(pytestconfig): - val = pytestconfig.cache.get("example/value", None) + cache = getattr(pytestconfig, "cache", None) + if cache is None: + 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 From 7b4851686380b5bb4f110e20f595265fbae7f869 Mon Sep 17 00:00:00 2001 From: Dr Alex Mitre Date: Thu, 5 Mar 2026 11:04:39 -0600 Subject: [PATCH 2/3] changelog: add doc fragment for cacheprovider example --- changelog/14148.doc.rst | 2 ++ 1 file changed, 2 insertions(+) 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. From c36e1fb05e3931ddba218e2475749f8d9908039e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 6 Mar 2026 09:52:34 -0300 Subject: [PATCH 3/3] Apply suggestions from code review --- doc/en/how-to/cache.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/en/how-to/cache.rst b/doc/en/how-to/cache.rst index 91cf04a7b68..c030e487563 100644 --- a/doc/en/how-to/cache.rst +++ b/doc/en/how-to/cache.rst @@ -200,8 +200,7 @@ The new config.cache object Plugins or conftest.py support code can get a cached value using the pytest ``config`` object. Here is a basic example plugin which implements a :ref:`fixture ` which reuses previously created state -across pytest invocations. The example below also works when the -``cacheprovider`` plugin is disabled: +across pytest invocations: .. code-block:: python @@ -217,6 +216,8 @@ across pytest invocations. The example below also works when the def mydata(pytestconfig): 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