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 ncoghlan
Recipients Ramchandra Apte, cjw296, daniel.urban, docs@python, eric.araujo, ncoghlan, terry.reedy
Date 2013-02-16.05:05:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1360991112.28.0.120316277499.issue17179@psf.upfronthosting.co.za>
In-reply-to
Content
The types.new_class docs are quite clear that the supplied keyword arguments are equivalent to those provided in the type header (if you want to pre-populate the namespace, that's what exec_body is for). The problem here is that the dual signature of type (retrieving the type of an existing object, or creating a new one), and the fact that type.__prepare__ ignores all arguments, means the error message is thoroughly misleading when you pass an unknown keyword argument:

>>> type.__prepare__(foo=1)
{}
>>> type("Example", (), {}, foo=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type() takes 1 or 3 arguments

>>> class Example(foo=1): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type() takes 1 or 3 arguments

>>> import types
>>> types.new_class("Example", (), dict(foo=1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ncoghlan/devel/py3k/Lib/types.py", line 52, in new_class
    return meta(name, bases, ns, **kwds)
TypeError: type() takes 1 or 3 arguments

The reason type.__prepare__ ignores its arguments is to make it easy for people to use type to anchor a custom metaclass hierarchy and call super().__prepare__(name, bases, **kwds) without needing to worry much about filtering the keyword arguments. (The class machinery intercepts the metaclass hint and never passes it to __prepare__ or the metaclass constructor).

That means the real change needed here is to update type's error message for bad arguments to properly report unknown keyword errors when using the PEP 3115 metaclass API.
History
Date User Action Args
2013-02-16 05:05:12ncoghlansetrecipients: + ncoghlan, terry.reedy, cjw296, eric.araujo, daniel.urban, docs@python, Ramchandra Apte
2013-02-16 05:05:12ncoghlansetmessageid: <1360991112.28.0.120316277499.issue17179@psf.upfronthosting.co.za>
2013-02-16 05:05:12ncoghlanlinkissue17179 messages
2013-02-16 05:05:11ncoghlancreate