[New-bugs-announce] [issue24928] mock.patch.dict spoils order of items in collections.OrderedDict
Alexander Oblovatniy
report at bugs.python.org
Mon Aug 24 21:19:55 CEST 2015
New submission from Alexander Oblovatniy:
Hi!
Current implementation of `patch.dict` spoils order of items in `collections.OrderedDict`, because it explicitly converts passed `values` to `dict` (https://github.com/python/cpython/blob/923527f560acd43d4cc11293060900e56c7cb39b/Lib/unittest/mock.py#L1559-L1560):
```python
# support any argument supported by dict(...) constructor
self.values = dict(values)
```
Most obvious way is to check if `values` is an instance of `dict`. If it's not, then we need to convert it to dict:
```python
if not isinstance(values, dict):
values = dict(values)
self.values = values
```
This will work for `OrderedDict`, because it's a subclass of `dict`. But this will not work for `UserDict.UserDict`, `UserDict.DictMixin`, `collections.MutableMapping`, etc., because they do not subclass `dict`. So, better way is to less strict check and look if `values` implements `dict`-like interface, e.g.:
```python
if not hasattr(values, 'update'):
values = dict(values)
self.values = values
```
Here is a question existence of what attrs to check.
Any ideas?
Thanks!
----------
components: Library (Lib)
messages: 249069
nosy: Alexander Oblovatniy
priority: normal
severity: normal
status: open
title: mock.patch.dict spoils order of items in collections.OrderedDict
type: behavior
versions: Python 2.7, Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24928>
_______________________________________
More information about the New-bugs-announce
mailing list