From 4c8e386b3a887cdd35c046d426b1247c9018aa0c Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Sat, 15 Jan 2022 10:39:48 -0600 Subject: [PATCH] Switch to a different method of resolving ForwardRefs during deserialization for python 3.10.2 compatibility --- HISTORY.md | 3 ++- requests_cache/serializers/cattrs.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 8425e0c9..f13459bc 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,7 +1,8 @@ # History ## 0.9.1 (Unreleased) -* Add support for key-only request parameters +* Add support for python 3.10.2 (regarding resolving `ForwardRef` types during deserialization) +* Add support for key-only request parameters (regarding hashing request data for cache key creation) * Reduce verbosity of log messages when encountering an invalid JSON request body ## 0.9.0 (2022-01-01) diff --git a/requests_cache/serializers/cattrs.py b/requests_cache/serializers/cattrs.py index 7b483d79..7652cadc 100644 --- a/requests_cache/serializers/cattrs.py +++ b/requests_cache/serializers/cattrs.py @@ -65,10 +65,10 @@ def init_converter(factory: Callable[..., GenConverter] = None): converter.register_unstructure_hook(HTTPHeaderDict, dict) converter.register_structure_hook(HTTPHeaderDict, lambda obj, cls: HTTPHeaderDict(obj)) - # Tell cattrs that a 'CachedResponse' forward ref is equivalent to the CachedResponse class - converter.register_structure_hook( - ForwardRef('CachedResponse'), - lambda obj, cls: converter.structure(obj, CachedResponse), + # Tell cattrs to resolve forward references (required for CachedResponse.history) + converter.register_structure_hook_func( + lambda cls: cls.__class__ is ForwardRef, + lambda obj, cls: converter.structure(obj, cls.__forward_value__), ) return converter