question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cjw296commented, May 2, 2019

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 😕

1reaction
micboucommented, May 2, 2019

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

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found