TypeError when patching a dictionary with a Unicode string literal on Python 2
See original GitHub issuePatching 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.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
python - How to get string objects instead of Unicode from JSON
You have json in Python 2.6+, and this gives you Unicode values, whereas simplejson returns string objects.
Read more >Working of Unicode Error in Python with Examples - eduCBA
When we use such a string as a parameter to any function, there is a possibility of the occurrence of an error. Such...
Read more >Built-in Types — Python 3.11.1 documentation
Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including hex, octal and binary ......
Read more >What else you need to know — Python-Future documentation
int; isinstance; Passing data to/from Python 2 libraries; Native string ... in <module> TypeError: sequence item 0: expected bytes, found unicode string ......
Read more >How to Convert a Unicode String to a Dictionary in Python?
Problem Formulation · Method 1: eval() · Method 2: ast.literal_eval() · Method 3: json.loads() & s.replace() · Method 4: Iterative Approach · Where...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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 😕
Nice. We won’t even have to freeze the version of mock to 2.0.0 then.