Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #14987
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <fabiofz@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'oct': 0.02; 'attribute': 0.07; 'unittest': 0.07; 'python': 0.08; '>>>>': 0.09; '__name__': 0.09; 'descriptors': 0.09; 'subclass': 0.09; 'subclasses': 0.09; '25,': 0.12; 'am,': 0.12; 'classes,': 0.13; 'def': 0.15; '#the': 0.16; "'__main__':": 0.16; '2.5)': 0.16; 'behavior?': 0.16; 'restoring': 0.16; 'subject:behavior': 0.16; 'cc:addr:python- list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'received:209.85.210.174': 0.18; 'received:mail- iy0-f174.google.com': 0.18; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'tue,': 0.23; "i'm": 0.27; 'skip:p 30': 0.28; '(see': 0.28; 'problem': 0.28; 'import': 0.28; 'pass': 0.29; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'none,': 0.30; 'object.': 0.30; 'sub': 0.30; 'thanks': 0.30; 'class': 0.30; 'equivalent': 0.31; 'anyone': 0.32; 'actually': 0.33; 'there': 0.33; '...': 0.34; 'setting': 0.34; 'idea': 0.34; 'test': 0.34; 'object': 0.35; 'class.': 0.37; 'but': 0.37; 'something': 0.37; 'think': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'why': 0.39; 'case': 0.39; "it's": 0.40; 'happens': 0.40; 'your': 0.61; 'below': 0.62; 'restore': 0.64; 'super': 0.64; 'roughly': 0.67; 'strange': 0.68; 'mock': 0.84; 'otten': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=wOdy9JKOWN260CrObMD3psYxr5ALDyrnwG1w0RUnR2s=; b=HbIRvv6T26CwaogQMlpkaZjpHH/L8XUDyY0YM3nV41yrLSTvUiS4bHgyHlYzQuNVNk 3Cbj4MqliraPLo9OOTrXzbcHS9xTVHRS70X94n13BpwIt44jPBq228+pRJeaWAiBceaT Q2kltCNCgmKuWUDPHtJxX+5WccxVuN480BjYc= |
| MIME-Version | 1.0 |
| In-Reply-To | <j868qb$o7u$1@dough.gmane.org> |
| References | <CANXBEFpDykfb2j0VxZCLwm1HP3KUDBfyN_fHTOa2Wt=UknT7Zw@mail.gmail.com> <j868qb$o7u$1@dough.gmane.org> |
| From | Fabio Zadrozny <fabiofz@gmail.com> |
| Date | Tue, 25 Oct 2011 20:59:26 -0200 |
| Subject | Re: Strange classmethod mock behavior |
| To | Peter Otten <__peter__@web.de> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2218.1319583589.27778.python-list@python.org> (permalink) |
| Lines | 81 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1319583589 news.xs4all.nl 6857 [2001:888:2000:d::a6]:45384 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:14987 |
Show key headers only | View raw
On Tue, Oct 25, 2011 at 10:08 AM, Peter Otten <__peter__@web.de> wrote: > Fabio Zadrozny wrote: > >> I'm trying to mock a classmethod in a superclass but when restoring it >> a strange behavior happens in subclasses (tested on Python 2.5) >> >> Anyone knows why this happens? (see test case below for details) >> Is there any way to restore that original method to have the original >> behavior? >> >> import unittest >> >> class Test(unittest.TestCase): >> >> def testClassmethod(self): >> class Super(): >> @classmethod >> def GetCls(cls): >> return cls >> >> class Sub(Super): >> pass >> >> self.assertEqual(Sub.GetCls(), Sub) >> >> original = Super.GetCls >> #Mock Super.GetCls, and do tests... >> Super.GetCls = original #Restore the original classmethod >> self.assertEqual(Sub.GetCls(), Sub) #The call to the >> classmethod subclass returns the cls as Super and not Sub as expected! >> >> if __name__ == '__main__': >> unittest.main() > > [Not related to your problem] When working with descriptors it's a good idea > to use newstyle classes, i. e. have Super inherit from object. > > The Super.GetCls attribute access is roughly equivalent to > > Super.__dict___["GetCls"].__get__(classmethod_instance, None, Super) > > and returns an object that knows about its class. So when you think you are > restoring the original method you are actually setting the GetCls attribute > to something else. You can avoid the problem by accessing the attribute > explicitly: > >>>> class Super(object): > ... @classmethod > ... def m(cls): return cls > ... >>>> bad = Super.m >>>> good = Super.__dict__["m"] >>>> class Sub(Super): pass > ... >>>> Sub.m() > <class '__main__.Sub'> >>>> Super.m = bad >>>> Sub.m() > <class '__main__.Super'> >>>> Super.m = good >>>> Sub.m() > <class '__main__.Sub'> > Hi Peter, thanks for the explanation. Printing it helped me understand it even better... print(Super.__dict__['GetCls']) print(Super.GetCls) <classmethod object at 0x022AF210> <bound method type.GetCls of <class '__main__.Super'>> Cheers, Fabio
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Strange classmethod mock behavior Fabio Zadrozny <fabiofz@gmail.com> - 2011-10-25 20:59 -0200
csiph-web