This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author conchylicultor
Recipients conchylicultor
Date 2021-05-15.08:42:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1621068171.99.0.542983196926.issue44140@roundup.psfhosted.org>
In-reply-to
Content
WeakKeyDictionary are great to "associate additional data with an object owned by other parts of an application", as quoted from the doc: https://docs.python.org/3/library/weakref.html#weakref.WeakKeyDictionary

However, this currently only works for hashable types. Non-hashables are not supported:

```
@dataclass
class A:
  pass

a = A()

d = weakref.WeakKeyDictionary()
d[a] = 3  # TypeError: unhashable type: 'A'
```

With regular dict, this could be easilly solved by using `d[id(a)] = 3`, but WeakKeyDictionary don't accept `int` of course. I cannot wrap the object either as the weakref would not be attached to the original object, but the wrapper.

It would be great to be able to force WeakKeyDictionary to perform lookup on `id` internally. Like `d = WeakKeyDictionary(use_id_lookup=True)`
History
Date User Action Args
2021-05-15 08:42:52conchylicultorsetrecipients: + conchylicultor
2021-05-15 08:42:51conchylicultorsetmessageid: <1621068171.99.0.542983196926.issue44140@roundup.psfhosted.org>
2021-05-15 08:42:51conchylicultorlinkissue44140 messages
2021-05-15 08:42:51conchylicultorcreate