annotationlib.ForwardRef defines both __eq__ and __hash__, but __hash__
raises TypeError when the forward reference holds an unhashable value, even
though two such forward references compare equal. This violates the data model
(objects that compare equal must be hashable and hash equal) and breaks storing
forward references in sets or as dict keys, which typing tools do.
Reproducer
from annotationlib import Format, get_annotations
class Unhashable:
__hash__ = None
obj = Unhashable()
def f(a: undefined | obj): ...
fr1 = get_annotations(f, format=Format.FORWARDREF)["a"]
fr2 = get_annotations(f, format=Format.FORWARDREF)["a"]
assert fr1 == fr2
hash(fr1) # TypeError: unhashable type: 'Unhashable'
obj resolves as a global, so the FORWARDREF format stores it in the forward
reference's __extra_names__. The same happens when the forward reference's
__owner__ is unhashable, for example a class whose metaclass sets
__hash__ = None.
Root cause
ForwardRef.__hash__ already hashes the unhashable __globals__ and __cell__
fields by identity (id()), but __owner__ and __extra_names__ are hashed
directly. __extra_names__ holds arbitrary values embedded in an annotation
that the FORWARDREF format could not render as a plain name, so those values
may be unhashable.
Regression
This is a sibling of #143831, which fixed the same class of bug for __cell__.
The traceback in that report already pointed at the __extra_names__ line.
Affected versions
3.14+ (where annotationlib was added).
Linked PRs
annotationlib.ForwardRefdefines both__eq__and__hash__, but__hash__raises
TypeErrorwhen the forward reference holds an unhashable value, eventhough two such forward references compare equal. This violates the data model
(objects that compare equal must be hashable and hash equal) and breaks storing
forward references in sets or as dict keys, which typing tools do.
Reproducer
objresolves as a global, so theFORWARDREFformat stores it in the forwardreference's
__extra_names__. The same happens when the forward reference's__owner__is unhashable, for example a class whose metaclass sets__hash__ = None.Root cause
ForwardRef.__hash__already hashes the unhashable__globals__and__cell__fields by identity (
id()), but__owner__and__extra_names__are hasheddirectly.
__extra_names__holds arbitrary values embedded in an annotationthat the
FORWARDREFformat could not render as a plain name, so those valuesmay be unhashable.
Regression
This is a sibling of #143831, which fixed the same class of bug for
__cell__.The traceback in that report already pointed at the
__extra_names__line.Affected versions
3.14+ (where
annotationlibwas added).Linked PRs