Repository navigation
Closed
Description
The problem
It seems that requests-cache is not compatible with Python 3.10.2 (it works with 3.10.1).
$ python -c "from requests_cache import CachedSession"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/__init__.py", line 11, in <module>
from .backends import *
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/backends/__init__.py", line 7, in <module>
from .base import BaseCache, BaseStorage, DictStorage
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/backends/base.py", line 18, in <module>
from ..serializers import init_serializer
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/serializers/__init__.py", line 6, in <module>
from .preconf import (
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/serializers/preconf.py", line 27, in <module>
CattrStage()
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/serializers/cattrs.py", line 32, in __init__
self.converter = init_converter(factory)
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/requests_cache/serializers/cattrs.py", line 69, in init_converter
converter.register_structure_hook(
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/cattr/converters.py", line 269, in register_structure_hook
self._structure_func.register_cls_list([(cl, func)])
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/site-packages/cattr/dispatch.py", line 57, in register_cls_list
self._single_dispatch.register(cls, handler)
File "/Users/tchegini/.local/apps/mambaforge/envs/ts/lib/python3.10/functools.py", line 856, in register
raise TypeError(
TypeError: Invalid first argument to `register()`. ForwardRef('CachedResponse') is not a class.Expected behavior
Since Python 3.10.2 is a patch release requests-cache should work.
Steps to reproduce the behavior
Create an environment with python 3.10.2 and then just try importing CachedResponse:
python -c "from requests_cache import CachedSession"Workarounds
Not that I know of.
Environment
- requests-cache version: Tried with both the stable release,
0.9.0andmasterfrom Github - Python version: 3.10.2
- Platform: MacOSX 12.1
Activity
JWCook commentedon Jan 15, 2022
Thanks for reporting this. This might be a difficult one to figure out.
I don't see anything in the python 3.10.2 changelog that indicates different behavior with
ForwardRef. This may be something that would require changes in thecattrslibrary, but I'm not 100% sure yet.I should have some time to look into this next week. If I can't figure out the root cause, I can at least make an ugly temporary workaround.
cheginit commentedon Jan 15, 2022
Right, I was kinda surprised too.
Thanks, that would be great.
JWCook commentedon Jan 15, 2022
While writing up python-attrs/cattrs#206, I found a fix for this in python-attrs/cattrs#201 (comment).
Brief explanation for anyone else who encounters a similar problem: In python 3.10.2, some behavior unexpectedly changed regarding ForwardRef and register() with
functoolssingle-dispatch generic functions.This caused the following method of structuring
ForwardRef(usingcattrs) to fail:A solution for this is to register a function that tests whether a type is a
ForwardRef, rather than registering aForwardRefinstance:This is also an all-around better solution since it generically resolves any forward references instead of a specific one, and avoids potential naming conflicts in forward references across different modules.
[-]Issue with Python 3.10.2[/-][+]Error when running on python 3.10.2 - TypeError: ForwardRef('...') is not a class [/+]dargor commentedon Jan 15, 2022
I encountered the same problem with python
3.9.10, too. I patched with #503, now it works fine. Thanks !JWCook commentedon Jan 15, 2022
This fix is now in the latest stable version (requests-cache 0.9.1).
cheginit commentedon Jan 15, 2022
@JWCook Will you be releasing 0.9.1 in
conda-forgeas well? Theconda-forgebuild of my package (conda-forge/pygeoogc-feedstock#25) is failing due torequests-cache.JWCook commentedon Jan 15, 2022
That is automated, it just takes some time to pick up a new release from PyPI sometimes. Looks like it just finished a few minutes ago:
conda-forge/requests-cache-feedstock#31
https://anaconda.org/conda-forge/requests-cache
cheginit commentedon Jan 15, 2022
Great! Thanks.
[-]Error when running on python 3.10.2 - TypeError: ForwardRef('...') is not a class [/-][+]Error when running on python 3.10.2 & 3.9.10 - TypeError: ForwardRef('...') is not a class [/+]Requests cache breaks in Python 3.9
Tinche commentedon Jan 26, 2022
FWIW I think they broke it with changes to
singledispatch. (https://bugs.python.org/issue46032)