From fdb5d04a586eced68ced842d9660f224ce548b67 Mon Sep 17 00:00:00 2001 From: James Ward Date: Thu, 30 Oct 2025 01:15:38 -0400 Subject: [PATCH 1/5] test: validate context vars are supported --- tests/test_cached.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_cached.py b/tests/test_cached.py index dbebc83..5d1367c 100644 --- a/tests/test_cached.py +++ b/tests/test_cached.py @@ -68,6 +68,14 @@ async def test_params_are_passed_through(self): assert await decorated_fn("foo") == ("foo",) assert await decorated_fn("foo", bar="baz") == ("foo", ("bar", "baz")) + async def test_context_var_passed(self): + var = contextvars.ContextVar('var') + var.set("test") + async def read_contextvar(): + return var.get() + decorated_fn = cachetools_async.cached({})(read_contextvar) + assert decorated_fn() == "test" + async def test_multiple_calls(self): mock = AsyncMock() From 52ef1e1ecf1c51a916fb565b55f17926ee41b8eb Mon Sep 17 00:00:00 2001 From: James Ward Date: Thu, 30 Oct 2025 01:16:44 -0400 Subject: [PATCH 2/5] test: import contextvars --- tests/test_cached.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_cached.py b/tests/test_cached.py index 5d1367c..e0b55d4 100644 --- a/tests/test_cached.py +++ b/tests/test_cached.py @@ -1,5 +1,6 @@ import asyncio import contextlib +import contextvars from unittest.mock import AsyncMock, MagicMock, call import pytest From 1ff5b497313addc503d74adb65b351f05cdd4232 Mon Sep 17 00:00:00 2001 From: James Ward Date: Thu, 30 Oct 2025 01:17:47 -0400 Subject: [PATCH 3/5] test: await --- tests/test_cached.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cached.py b/tests/test_cached.py index e0b55d4..169b43c 100644 --- a/tests/test_cached.py +++ b/tests/test_cached.py @@ -75,7 +75,7 @@ async def test_context_var_passed(self): async def read_contextvar(): return var.get() decorated_fn = cachetools_async.cached({})(read_contextvar) - assert decorated_fn() == "test" + assert await decorated_fn() == "test" async def test_multiple_calls(self): mock = AsyncMock() From cc5d4c9ed8ff344c9b20d3a5d877b2011fc06b60 Mon Sep 17 00:00:00 2001 From: James Ward Date: Thu, 30 Oct 2025 01:25:54 -0400 Subject: [PATCH 4/5] test: try another approach --- tests/test_cached.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/test_cached.py b/tests/test_cached.py index 169b43c..3c3ffbe 100644 --- a/tests/test_cached.py +++ b/tests/test_cached.py @@ -70,12 +70,21 @@ async def test_params_are_passed_through(self): assert await decorated_fn("foo", bar="baz") == ("foo", ("bar", "baz")) async def test_context_var_passed(self): - var = contextvars.ContextVar('var') + var = contextvars.ContextVar("var") var.set("test") - async def read_contextvar(): + + def read_contextvar(): return var.get() - decorated_fn = cachetools_async.cached({})(read_contextvar) - assert await decorated_fn() == "test" + + async def example(): + var.set("example") + return read_contextvar() + + decorated_fn = cachetools_async.cached({})(example) + + actual = await decorated_fn(var) + assert actual == "example" + assert var.get() == "test" async def test_multiple_calls(self): mock = AsyncMock() From 8d0bc05cb89536474515de63f6818753a350f775 Mon Sep 17 00:00:00 2001 From: James Ward Date: Thu, 30 Oct 2025 01:27:05 -0400 Subject: [PATCH 5/5] test: call example correctly --- tests/test_cached.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cached.py b/tests/test_cached.py index 3c3ffbe..911ae40 100644 --- a/tests/test_cached.py +++ b/tests/test_cached.py @@ -82,7 +82,7 @@ async def example(): decorated_fn = cachetools_async.cached({})(example) - actual = await decorated_fn(var) + actual = await decorated_fn() assert actual == "example" assert var.get() == "test"