Skip to content

gh-152358: Fix hash() of annotationlib.ForwardRef with unhashable value#152360

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-gh-152358-forwardref-hash-unhashable
Open

gh-152358: Fix hash() of annotationlib.ForwardRef with unhashable value#152360
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-gh-152358-forwardref-hash-unhashable

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

ForwardRef.__eq__ and ForwardRef.__hash__ already compare and hash the
unhashable __globals__ and __cell__ fields by identity, but __owner__ and
__extra_names__ were used directly. Because a forward reference can hold an
arbitrary, possibly unhashable, value -- a value embedded in an annotation, or
an unhashable owner -- hash() raised TypeError while __eq__ succeeded, a
data model invariant violation that also broke storing forward references in
sets and as dict keys.

This applies the same id()-based treatment to __owner__ and __extra_names__
in both __eq__ and __hash__, mirroring the __cell__ fix in gh-143831 and
making __hash__ total. Comparing these fields by identity is consistent with
the existing __globals__/__cell__ handling, and two forward references
derived from the same annotation share the same objects, so they still compare
equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant