-
-
Notifications
You must be signed in to change notification settings - Fork 32k
ForwardRef: remove __hash__
and __eq__
#129463
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
Comments
so this broke things for us in SQLAlchemy, however I'm still trying to see if this is only local to our test suite. The previous behavior of ForwardRef made perfect sense: import typing
def make_fw_ref(anno: str) -> typing.ForwardRef:
return typing.Union[anno]
assert make_fw_ref("str") == typing.ForwardRef("str") why should that assertion now fail? it looks to be strictly the |
The change definitely makes it more annoying to do equality comparisons with ForwardRef objects. In CPython we added the On reflection though I do think it makes sense to remove |
I'm pretty sure we're hitting this only because we are looking in tests at the output of functions we use that modify types and comparing them to what's expected. we rely a lot on a function the failures we have are in our test suite for de_optionalize_union_types. So yes I can make a comparison routine as well, however this gets a tedious when i need to compare something like |
for those tests what I can do is make my own mock ForwardRef that has its own |
#132283 removes the |
OK , thanks. at the same time I vendored EqualToForwardRef and am running that in CI now |
Feature or enhancement
Currently,
annotationlib.ForwardRef
has a__hash__
and__eq__
method, which look at a few attributes:cpython/Lib/typing.py
Line 1096 in 8b4a0d6
__forward_module__
). This gets worse on 3.14, where ForwardRef has gained a few more fields (cpython/Lib/annotationlib.py
Line 232 in a472244
I think it's better to remove the
__eq__
and__hash__
methods fromForwardRef
objects, and make it so two ForwardRefs are equal only if they're identical. I don't see a good use case for comparing two ForwardRefs for equality; if you want to know that they refer to the same thing, you should evaluate them and compare the type object that comes out.This came up in agronholm/typeguard#492 where the current implementation of equality caused some grief. cc people involved with some runtime type checking tools: @agronholm @Viicos @leycec.
Linked PRs
The text was updated successfully, but these errors were encountered: