Skip to content
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

ForwardRef: remove __hash__ and __eq__ #129463

Open
JelleZijlstra opened this issue Jan 30, 2025 · 0 comments · May be fixed by #129465
Open

ForwardRef: remove __hash__ and __eq__ #129463

JelleZijlstra opened this issue Jan 30, 2025 · 0 comments · May be fixed by #129465
Labels
topic-typing type-feature A feature request or enhancement

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Jan 30, 2025

Feature or enhancement

Currently, annotationlib.ForwardRef has a __hash__ and __eq__ method, which look at a few attributes:

def __eq__(self, other):
(3.13). These look unsound already; it's possible for two ForwardRefs to compare equal but hash differently (if they have the same evaluated value but not the same __forward_module__). This gets worse on 3.14, where ForwardRef has gained a few more fields (
def __eq__(self, other):
).

I think it's better to remove the __eq__ and __hash__ methods from ForwardRef 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant