Skip to content

Fix UserDict move in Python2#365

Merged
benjaminp merged 3 commits into
benjaminp:masterfrom
rollbar:bxsx/userdict
Nov 21, 2021
Merged

Fix UserDict move in Python2#365
benjaminp merged 3 commits into
benjaminp:masterfrom
rollbar:bxsx/userdict

Conversation

@bxsx

@bxsx bxsx commented Nov 19, 2021

Copy link
Copy Markdown
Contributor

Background

The current implementation of UserDict attribute maps UserDict.UserDict to collections.UserDict:

MovedAttribute("UserDict", "UserDict", "collections"),

From the Python 2.7 documentation:

For backward compatibility, instances of UserDict are not iterable.

This limitation does not exist in Python3.
As a result, six introduces a bug in Python2.

Reproduction steps:

from six.moves import UserDict

d = UserDict({'key': 'output'})
for k in d:
    print(d[k])

PY3 output:

output

PY2 output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bxsx/.pyenv/versions/2.7.18/lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 0

Solution

This PR addresses it by updating UserDict attribute to mapUserDict.IterableUserDict in Python2 and collections.UserDict in Python3.
PR also adds moves for IterableUserDict which maps to the same attribute as UserDict.

PR fixes #281 issue

bxsx added 2 commits November 19, 2021 20:52
instances of UserDict.UserDict are not iterable
but instances of collections.UserDict are iterable
@benjaminp

Copy link
Copy Markdown
Owner

Thanks for the PR. I don't really understand the point of having a attribute IterableUserDict attribute that maps to the same thing as UserDict.

@bxsx

bxsx commented Nov 21, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @benjaminp.
I assumed it's useful for migrating code from Python 2 as both UseDict and IterableUserDict are available in Python 2.

I can revert a5610fc if it is a blocker.

@bxsx

bxsx commented Nov 21, 2021

Copy link
Copy Markdown
Contributor Author

Ok, @benjaminp , a5610fc is now reverted and PR no longer adds IterableUserDictattribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

six.moves.UserDict should alias to UserDict.IterableUserDict on Python 2

2 participants