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 eric.snow
Recipients eric.snow, ncoghlan
Date 2013-03-14.20:20:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1363292427.77.0.787480674647.issue17421@psf.upfronthosting.co.za>
In-reply-to
Content
Currently type_new() in Objects/typeobject.c enforces a restriction that the namespace be a dict or dict subclass.  It does this via the PyArg_ParseTupleAndKeywords() call there.

This means that valid mappings may not be used even though they should work just fine.  A demonstration of the problem is below.

I've attached a patch that relaxes this restriction.  Should we also add a note in the docs that type() will take anything for namespace that dict() will take?

Demonstration
-------------

class Meta(type):
    @classmethod
    def __prepare__(cls, name, bases, **kwargs):
        return ClassMapping()

from collections import MutableMapping
class ClassMapping(MutableMapping):
    def __init__(self, *args, **kwargs):
        self._dict = dict(*args, **kwargs)
    def __len__(self):
      return len(self._dict)
    def __iter__(self):
        return iter(self._dict)
    def __getitem__(self, key):
        return self._dict[key]
    def __setitem__(self, key, value):
        self._dict[key] = value
    def __delitem__(self, key):
        del self._dict[key]

>>> class X(metaclass=Meta):
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type() argument 3 must be dict, not ClassMapping
History
Date User Action Args
2013-03-14 20:20:27eric.snowsetrecipients: + eric.snow, ncoghlan
2013-03-14 20:20:27eric.snowsetmessageid: <1363292427.77.0.787480674647.issue17421@psf.upfronthosting.co.za>
2013-03-14 20:20:27eric.snowlinkissue17421 messages
2013-03-14 20:20:27eric.snowcreate