New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Most Set methods of KeysView and ItemsView do not work right #53460
Comments
Attached is a simple Python 3 example script that defines a minimalist MutableMapping that simply wraps a dict, followed by: x = MySimpleMapping(red=5)
y = x.keys()
z = x.keys() | {'orange'} x['blue'] = 7
print(list(z))
print(list(z)) Output: Expected Output: The problem is that __or__ ends up returning a new KeysView wrapping a generator instead of returning a set. To solve the problem, KeysView and ItemsView need to override _from_iterable to return a set instead of a new view. |
Oops. Somehow deleted a linefeed in there. The example should read: x = MySimpleMapping(red=5)
y = x.keys()
z = x.keys() | {'orange'} x['blue'] = 7
print(list(z))
print(list(z)) |
I guess roundup is deleting my linefeed? I'm sure I didn't do it that time. I'm not sure what's going on there, but the "x['blue'] = 7" should be on a line by itself. |
Thanks for the good analysis and suggested fix. I believe both are correct and they match the behavior of real dictionaries. def _from_iterable(self, it):
return set(it) Proposed tests to match real dicts: x = MySimpleMapping() # from stuzback's example
x['red'] = 5
y = x.keys()
assert isinstance(y, collections.Set)
assert not isinstance(y, collections.MutableSet)
z = x.keys() | {'orange'}
assert type(z) is set |
Hello, I'm attaching a patch for this issue.
The patch was made against the latest dev checkout. If it's found sufficient, I can backport it to 2.6/2.7 --- P.S. The patch was generated from a Hg mirror, so to patch it -p1 is to be used. If this is a problem, let me know and I'll generate a patch from an SVN working directory. I got the impression from the #python-dev IRC channel that submitting patches from Hg is fine, but I may be wrong here. |
The patch applies cleanly to the py3k branch (r83238). The unittests pass. |
Attaching an updated patch, with the trailing whitespace removed. Hope it's more acceptable now. P.S. Please let me know how to detect such issues in future patches. |
In some editors there is an option "Remove trailing spaces" or something like that. Also, some editors (for example Kate) show trailing spaces. |
The patch looks good to me, too. The new tests fail without the fix, and pass with the fix. |
Fixed in r84252, r84252, and r84254. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: