-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
TypeError when registering ForwardRefs in python 3.10.2 #206
Comments
Note that one of the other solutions posted in #201 still works in python 3.10.2: #201 (comment) So the above example would become: 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()
converter .register_structure_hook_func(
lambda t: t.__class__ is typing.ForwardRef,
lambda v, t: converter.structure(v, t.__forward_value__),
) So this may be a non-issue, but probably still useful for someone else out there googling for the same error. |
I think the error is due to this commit (a change in functools.py), which was a fix due to bpo-46032. It seems that the newly added check It is a bit hard to follow the logic, but ForwardRef("MyClass") technically is an instance and not a type (which may explain the behavior). However, I do not understand is why the solution in your last comment works. Also what the change to singledispatch means for cattrs. I have the feeling that this will bite us again. |
Good to know, thanks for tracking that down. It looks like this may be an unintended side effect of that bugfix rather than an intentional change, then.
True, but an instance of >>> from typing import ForwardRef
>>> type(ForwardRef("MyClass"))
<class 'typing.ForwardRef'>
Tinche would be able to give a more thorough explanation, but I believe it works because Lines 95 to 131 in 22b24c2
We're using the function we provide (in this case testing |
Yeah, as you folks found out, So the predicate approach would be preferred, yeah. |
Same bug occurs with Python 3.9.10 (on Debian/Ubuntu). |
This works around a Python bug introduced in Python 3.10.2 and 3.9.10 (python-attrs/cattrs#206). This change should be reverted once that bug is fixed and new Python packages available in Debian/Ubuntu. Fixes #6119.
This works around a Python bug introduced in Python 3.10.2 and 3.9.10 (python-attrs/cattrs#206). This change should be reverted once that bug is fixed and new Python packages are available in Debian/Ubuntu. Fixes #6119.
This works around a Python bug introduced in Python 3.10.2 and 3.9.10 (python-attrs/cattrs#206). This change should be reverted once that bug is fixed and new Python packages are available in Debian/Ubuntu. Fixes #6119. (cherry picked from commit 412b6c6)
This works around a Python bug introduced in Python 3.10.2 and 3.9.10 (python-attrs/cattrs#206). This change should be reverted once that bug is fixed and new Python packages are available in Debian/Ubuntu. Fixes #6119.
This works around a Python bug introduced in Python 3.10.2 and 3.9.10 (python-attrs/cattrs#206). This change should be reverted once that bug is fixed and new Python packages are available in Debian/Ubuntu. Fixes #6119. (cherry picked from commit 10e1697)
See also the bug in requests-cache: requests-cache/requests-cache#501 |
Hi, any timeline to fix this issue for python 3.9.10? Thanks |
This is fixed in requests-cache 0.9.1 |
Thanks, but I'm curious more about
|
@alekseiloginov cattrs has no "official" support for ForwardRefs yet (or has ever had it). The We might build ForwardRef support into cattrs in a special way (there's some interest) in the future, but until then simply switch from using |
Due to some changes very high up the stack in CPython 3.9.10 [1], requests-cache does no longer work properly [2] and will fail due to its dependency on cattrs [3] which uses functools.singledispatch which is what changed in the aforementioned CPython update. The requests-cache devs have implemented a workaround in version 0.9.1 which is the only thing that they can do since cattrs does not explicitly support their usage (and are not willing to implement a workaround at their level). This commit upgrades to said version. [1] https://bugs.python.org/issue46032 [2] requests-cache/requests-cache#504 [3] python-attrs/cattrs#206
I wasn't able to get my slightly different situation to work with from attr import define, field
@define()
class MyClass:
history: "Optional[List[MyClass]]" = field(default=None) |
* feat: Add generate command for generating a scaler CSV file * feat: Read scaler configuration from a CSV * feat: Add configurations for ssp119, ssp126 and ssp245 * fix: Update register hook See python-attrs/cattrs#206 (comment)
* feat: Add generate command for generating a scaler CSV file * feat: Read scaler configuration from a CSV * feat: Add configurations for ssp119, ssp126 and ssp245 * fix: Update register hook See python-attrs/cattrs#206 (comment)
Description
This is related to #201, but probably worth making a separate issue for, since it affects one of the (previously working) workarounds mentioned in that issue. Feel free to close this if you feel this is redundant with #201 or #94.
I have a converter that registers a
ForwardRef
. In python 3.6.x through 3.10.1, this has worked as intended. As of python 3.10.2, this is now throwing aTypeError
:Strangely, I don't see anything in the python 3.10.2 changelog that would indicate changed behavior with
ForwardRef
orregister()
.What I Did
Minimal example:
Traceback:
The text was updated successfully, but these errors were encountered: