Title: dict init/update accesses internal items of dict derivative
Type: enhancement Stage: test needed
Components: Interpreter Core Versions: Python 3.1, Python 2.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, blakeross, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2007-06-04 03:59 by blakeross, last changed 2010-08-11 20:24 by rhettinger. This issue is now closed.

Messages (4)
msg32207 - (view) Author: Blake Ross (blakeross) Date: 2007-06-04 03:59
>>> class MyDict(dict):
...     def keys(self): print "keys"
...     def __getitem__(self, n): print "__getitem__"
>>> myDict = MyDict(a=1, b=2)
>>> dict(myDict)
{'a': 1, 'b': 2}

PyDict_Merge accesses the items of the dict to be merged directly rather than going through the interface for any dict instance--even a dict derivative--by virtue of using PyDict_Check rather than PyDict_CheckExact. I believe the logic needs to be:

if type(d).__getitem__ is dict.__getitem__ and type(d).keys is dict.keys:
    ...okay to access items directly...
    ...go through the methods...
msg85623 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-04-06 09:41
IIRC, the same behavior is present for subclasses of other builtin types.
msg113372 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-09 04:02
It is documented that access to special methods is more direct than normal path. This is not going to change. A similar example was discussed on python-list earlier this year with the above conclusion.
msg113627 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-08-11 20:24
I concur with Terry.
This is just a fact of life when subclassing builtins.
The objects are "open-for-extension, closed-for-modification".
If you want more direct control use UserDict.
Date User Action Args
2010-08-11 20:24:29rhettingersetnosy: + rhettinger
messages: + msg113627
2010-08-09 04:02:30terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg113372

resolution: rejected
2009-04-06 09:41:55ajaksu2setversions: + Python 3.1, Python 2.7
nosy: + ajaksu2

messages: + msg85623

type: enhancement
stage: test needed
2007-06-04 03:59:53blakerosscreate