derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Patching fails for objects instantiated via ParentClass.__subclasses__() #169

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

from mock import *

class A(object):
    pass

class B(A):
    pass

def factory():
    return A.__subclasses__()[0]()

with patch('__main__.B') as mock_b:
    print factory()
    print mock_b()

What is the expected output? What do you see instead?

Expected: 
<MagicMock name='B()' id='...'>
<MagicMock name='B()' id='140105901247632'>

Got:
<__main__.B object at 0x7f6cf2798c90>
<MagicMock name='B()' id='140105901247632'>

What version of the product are you using? On what operating system?

$ uname -a
Linux natty64 2.6.38-15-server #65-Ubuntu SMP Thu Jul 26 20:30:51 UTC 2012 
x86_64 x86_64 x86_64 GNU/Linux

$ python -c 'import mock; print mock.__version__'
1.0b1

Original issue reported on code.google.com by jame...@percolate.com on 29 Aug 2012 at 6:47

Attachments:

GoogleCodeExporter commented 9 years ago
patch works by replacing individual references with a mock. When you patch 
__main__.B there is still a real B class, and it is still a subclass of A. So 
A.__subclasses__()[0] is still the real class B - it doesn't do a dynamic 
lookup in __main__ to find B and so patching B in __main__ can't affect it. You 
could instead patch B.__new__ to return a mock when B is instantiated.

Original comment by fuzzyman on 29 Aug 2012 at 7:15