Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43328
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: name lookup failure using metaclasses with unittests |
| Date | 2013-04-11 08:43 +0200 |
| Message-ID | <em2i3a-hj2.ln1@satorlaser.homedns.org> (permalink) |
| References | <sqjf3a-03s.ln1@satorlaser.homedns.org> <mailman.404.1365587492.3114.python-list@python.org> |
Am 10.04.2013 11:52, schrieb Peter Otten: > Ulrich Eckhardt wrote: [...] > It looks like this particular invocation relies on class attribute and > function __name__ being identical. > > Please file a bug report. Thanks for confirming this and reducing the test case even more. >> Now, concerning Python 3, it fails to detect any test case at all! My >> guess is that the unittest library was changed to use metaclasses itself >> in order to detect classes derived from unittest.TestCase. Therefore, >> overriding the metaclass breaks test case discovery. My question in that >> context is how do I extend metaclasses instead of overriding it? In >> other words, what is the equivalent to super() for class creation? > > Python 3 does not recognize the __metaclass__ attribute as the metaclass. > You need to provide it like so: > > def __metaclass__(name, bases, dict): > ... > > class X(unittest.TestCase, metaclass=__metaclass__): > pass :| Doing some research[0, 1] on metaclasses in 2 and 3, I have a few more questions... The first thing I was wondering was why Python doesn't complain about a class property that is marked as special (leading and trailing double underscores) but that it knows nothing about. Worse, Python 3 should be aware of its legacy and recognize the Python 2 metaclass syntax, even if only to reject it loudly. I'm pretty sure there is a reason for that, The second question that came up was if there is a way to keep a metaclass defined inside the class or if the only way is to provide it externally. The reason I like this in-class definition is that for my case of autogenerated test functions, everything is in one place which used to be in a loop that modified the class after its creation. Maybe I'm just too brainwashed by static languages though. To get the Python2 feeling back, I have a hack in mind that involves creating a baseclass which in turn provides a metaclass that invokes a specific function to post-initialize the class, similar to the way Python 2 does it automatically, but I'm wondering if there isn't anything better. Also PEP 3115 "Metaclasses in Python 3000"[2] seems to consider postprocessing of a class definition as better handled by a class decorator, which is something I haven't looked at yet. Greetings from Hamburg! Uli [0] http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/ [1] http://www.artima.com/weblogs/viewpost.jsp?thread=236234 [2] http://www.python.org/dev/peps/pep-3115/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-10 10:18 +0200
Re: name lookup failure using metaclasses with unittests Peter Otten <__peter__@web.de> - 2013-04-10 11:52 +0200
Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-11 09:09 +0200
Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-11 08:43 +0200
Re: name lookup failure using metaclasses with unittests Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-11 08:19 +0000
Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-12 09:17 +0200
Re: name lookup failure using metaclasses with unittests Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-12 10:58 -0400
Re: name lookup failure using metaclasses with unittests Arnaud Delobelle <arnodel@gmail.com> - 2013-04-11 09:19 +0100
csiph-web