[Python-Dev] [Python-checkins] cpython: PEP 417: Adding unittest.mock
Terry Reedy
tjreedy at udel.edu
Wed Mar 14 21:46:29 CET 2012
On 3/14/2012 4:22 PM, Michael Foord wrote:
>
> On 14 Mar 2012, at 13:08, Terry Reedy wrote:
>
>> On 3/14/2012 3:25 PM, michael.foord wrote:
>>> +# mock.py +# Test tools for mocking and patching.
>> Should there be a note here about restrictions on editing this
>> file? I notice that there are things like
>>
>>> +class OldStyleClass: + pass +ClassType = type(OldStyleClass)
>>
>> which are only present for running under Py2 and which would
>> normally be removed for Py3.
>
>
> Yeah, I removed as much of the Python 2 compatibility code and
> thought I'd got it all. Thanks for pointing it out.
2000 lines is a lot to check through.
>
> I'm maintaining a "clean" (no Python 2 compatibility code) version in
> the standard library.
Great. Here is something else, which is why I thought otherwise ;-).
+def _instance_callable(obj):
+ """Given an object, return True if the object is callable.
+ For classes, return True if instances would be callable."""
+ if not isinstance(obj, type):
+ # already an instance
+ return getattr(obj, '__call__', None) is not None
+
+ klass = obj
+ # uses __bases__ instead of __mro__ so that we work with
>>> old style classes
+ if klass.__dict__.get('__call__') is not None:
+ return True
+
+ for base in klass.__bases__:
+ if _instance_callable(base):
+ return True
+ return False
If you want to leave the code as is, remove or revise the comment.
> I'll be maintaining mock, so I'd like to be
> assigned any issues on it and at least talked to before changes are
> made. I am maintaining a backport still, but the Python standard
> library version is the canonical version.
Add unittest.mock to devguide/experts.rst and yourself with * appended.
---
Searching for 'old', I also found
+def _must_skip(spec, entry, is_type):
+ if not isinstance(spec, type):
+ if entry in getattr(spec, '__dict__', {}):
+ # instance attribute - shouldn't skip
+ return False
>>>+ # can't use type because of old style classes
+ spec = spec.__class__
+ if not hasattr(spec, '__mro__'):
>>>+ # old style class: can't have descriptors anyway
+ return is_type
In testcallable.py
+ def test_patch_spec_callable_class(self):
+ class CallableX(X):
+ def __call__(self):
+ pass
+
+ class Sub(CallableX):
+ pass
+
+ class Multi(SomeClass, Sub):
+ pass
+
>>>+ class OldStyle:
+ def __call__(self):
+ pass
+
>>>+ class OldStyleSub(OldStyle):
+ pass
+
+ for arg in 'spec', 'spec_set':
>>>+ for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:
This is the last.
--
Terry Jan Reedy
More information about the Python-Dev
mailing list