[issue44523] weakref.proxy documentation might be outdated

Pablo Galindo Salgado report at bugs.python.org
Mon Jun 28 12:17:27 EDT 2021


Pablo Galindo Salgado <pablogsal at gmail.com> added the comment:

In my view, proxies should behave almost exactly as the underliying object, so using them is as ergonomic as possible. For instance, when using them as a way to avoid cycles, is quite annoying if you have a hashable object in a dictionary and they fail to tell you that is there:

>>> class A:
...   ...
...
>>> a = A()
>>> d = {a: None}
>>> weakref.proxy(a) in d
True

but doing this in 3.8 is impossible:

>>> import weakref
>>> class A:
...    ...
...
>>> a = A()
>>> d = {a: None}
>>> weakref.proxy(a) in d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'weakproxy'

Which defies a lot of use cases of this. Notice that if the object is not hashable because the author of the class wants to prevent against that it will work as expected:

>>> class A:
...    __hash__ = None
...
>>> a = A()
>>> hash(weakref.proxy(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'A'

This also gives a better error message because it explains that A is not hashable.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44523>
_______________________________________


More information about the Python-bugs-list mailing list