-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when running on python 3.10.2 & 3.9.10 - TypeError: ForwardRef('...') is not a class #501
Comments
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 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. |
Right, I was kinda surprised too.
Thanks, that would be great. |
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 This caused the following method of structuring from attr import define, field
from cattr import GenConverter
from typing import ForwardRef, List
@define()
class MyClass:
history: List['MyClass'] = field(factory=list)
converter = GenConverter()
# Resolve MyClass forward reference during deserialization
converter.register_structure_hook(
ForwardRef('MyClass'),
lambda obj, _: converter.structure(obj, MyClass)
) A solution for this is to register a function that tests whether a type is a # Resolve any forward references during deserialization
converter.register_structure_hook_func(
lambda cls: cls.__class__ is typing.ForwardRef,
lambda obj, cls: converter .structure(obj, cls.__forward_value__),
) 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. |
I encountered the same problem with python |
This fix is now in the latest stable version (requests-cache 0.9.1). |
@JWCook Will you be releasing 0.9.1 in |
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: |
Great! Thanks. |
FWIW I think they broke it with changes to |
The problem
It seems that
requests-cache
is not compatible with Python 3.10.2 (it works with 3.10.1).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
0.9.0
andmaster
from GithubThe text was updated successfully, but these errors were encountered: