testing-cabal / mock

The Python mock library

Home Page:https://docs.python.org/dev/library/unittest.mock.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError when patching a dictionary with a Unicode string literal on Python 2

micbou opened this issue · comments

Patching a dictionary with a Unicode string literal on Python 2 raises the exception TypeError: string indices must be integers. Running a test.py file containing the following code

from mock import patch

some_dict = {}

with patch.dict(u'test.some_dict'):
    pass

results in

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    with patch.dict(u'test.some_dict'):
  File "C:\Python27\lib\site-packages\mock\mock.py", line 1766, in __enter__
    self._patch_dict()
  File "C:\Python27\lib\site-packages\mock\mock.py", line 1783, in _patch_dict
    original[key] = in_dict[key]
TypeError: string indices must be integers

This is a regression introduced by commit 5268eda.

@tirkarthi - any ideas on a fix?

Thanks for the quick resolution. Will there be a new release containing the fix?

3.0.3 should be out shortly.

Nice. We won't even have to freeze the version of mock to 2.0.0 then.

@cjw296 Good catch! I think backports should be little more careful since Python 3 has no basestring and the upstream would be using str all along :( Upstream doesn't check for unicode values in most cases. Backports would also be a problem when 3.x only features like f-strings are used.

There is also a large PR to add asyncio support for mock : python/cpython#9296

We just have to hope for test coverage to catch things, or add test coverage when bug reports come in.

Backporting is only automated in so much as the path renaming and iteration over patches since the last sync. There's no code re-writing and I don't think there'd be much of a win with this.

Still, the new CI for the backport will exercise all our supported versions before a merge, and will fail if an incoming patch drops coverage below 100%.

That async patch will be fun to backport :-/