Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Re: [Python-checkins] cpython: PEP 417: Adding unittest.mock

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


tjreedy at udel

Mar 14, 2012, 1:08 PM

Post #1 of 4 (130 views)
Permalink
Re: [Python-checkins] cpython: PEP 417: Adding unittest.mock

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.

---
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


fuzzyman at voidspace

Mar 14, 2012, 1:22 PM

Post #2 of 4 (132 views)
Permalink
Re: [Python-checkins] cpython: PEP 417: Adding unittest.mock [In reply to]

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.

I'm maintaining a "clean" (no Python 2 compatibility code) version in the standard library. 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.

All the best,

Michael Foord

>
> ---
> Terry Jan Reedy
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
>


--
http://www.voidspace.org.uk/


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing
http://www.sqlite.org/different.html





_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


tjreedy at udel

Mar 14, 2012, 1:46 PM

Post #3 of 4 (132 views)
Permalink
Re: [Python-checkins] cpython: PEP 417: Adding unittest.mock [In reply to]

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

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


fuzzyman at voidspace

Mar 14, 2012, 2:41 PM

Post #4 of 4 (126 views)
Permalink
Re: [Python-checkins] cpython: PEP 417: Adding unittest.mock [In reply to]

On 14 Mar 2012, at 13:46, Terry Reedy wrote:

> 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.

Thanks very much for finding these, I'm pretty sure I've fixed all the ones you reported - and one more case where try...except...finally can now be used.

All the best,

Michael Foord


>
>> 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
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
>


--
http://www.voidspace.org.uk/


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing
http://www.sqlite.org/different.html





_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.