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 Antony.Lee
Recipients Antony.Lee
Date 2017-08-22.07:31:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1503387072.25.0.0553627508053.issue31254@psf.upfronthosting.co.za>
In-reply-to
Content
The following example, which raises a KeyError, shows that in a WeakKeyDictionary subclass that defines __missing__, that method doesn't get called.

    from weakref import WeakKeyDictionary

    class WeakKeyDictionaryWithMissing(WeakKeyDictionary):
        __missing__ = lambda: print("hello")

    class C: pass

    d = WeakKeyDictionaryWithMissing()
    d[C()]

This behavior is technically OK, as object.__missing__ is only documented in the datamodel to be called for dict subclasses, and WeakKeyDictionary is actually not a subclass of dict, but of MutableMapping.

Still, it would seem preferable if either WeakKeyDictionary did use __missing__, or perhaps, more reasonably, Mapping.__getitem__ did so.  (Or, at least, if the WeakKeyDictionary class clearly stated that it does not inherit from dict.  Note that the docs start with "Mapping class that references keys weakly. Entries in the *dictionary* etc." (emphasis mine))
History
Date User Action Args
2017-08-22 07:31:12Antony.Leesetrecipients: + Antony.Lee
2017-08-22 07:31:12Antony.Leesetmessageid: <1503387072.25.0.0553627508053.issue31254@psf.upfronthosting.co.za>
2017-08-22 07:31:12Antony.Leelinkissue31254 messages
2017-08-22 07:31:11Antony.Leecreate