Skip to content
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

Closed
stutzbach mannequin opened this issue Jul 9, 2010 · 10 comments
Closed

Most Set methods of KeysView and ItemsView do not work right #53460

stutzbach mannequin opened this issue Jul 9, 2010 · 10 comments
Assignees
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@stutzbach
Copy link
Mannequin

stutzbach mannequin commented Jul 9, 2010

BPO 9214
Nosy @rhettinger, @merwok, @durban
Files
  • keysview_test.py: Python 3 script to illustrate the problem
  • issue9214.1.patch
  • issue9214.2.patch
  • 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:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2010-08-22.08:02:24.830>
    created_at = <Date 2010-07-09.19:31:43.889>
    labels = ['easy', 'type-bug', 'library']
    title = 'Most Set methods of KeysView and ItemsView do not work right'
    updated_at = <Date 2010-08-22.08:02:24.828>
    user = 'https://bugs.python.org/stutzbach'

    bugs.python.org fields:

    activity = <Date 2010-08-22.08:02:24.828>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2010-08-22.08:02:24.830>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2010-07-09.19:31:43.889>
    creator = 'stutzbach'
    dependencies = []
    files = ['17923', '18208', '18267']
    hgrepos = []
    issue_num = 9214
    keywords = ['patch', 'easy']
    message_count = 10.0
    messages = ['109788', '109789', '109790', '109805', '111593', '111990', '112046', '112117', '114459', '114641']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'stutzbach', 'eric.araujo', 'eli.bendersky', 'daniel.urban']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue9214'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @stutzbach
    Copy link
    Mannequin Author

    stutzbach mannequin commented Jul 9, 2010

    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:
    ['blue', 'red', 'orange']
    []

    Expected Output:
    ['orange', 'red']
    ['orange', 'red']

    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.

    @stutzbach stutzbach mannequin self-assigned this Jul 9, 2010
    @stutzbach stutzbach mannequin added stdlib Python modules in the Lib dir easy type-bug An unexpected behavior, bug, or error labels Jul 9, 2010
    @stutzbach
    Copy link
    Mannequin Author

    stutzbach mannequin commented Jul 9, 2010

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

    @stutzbach
    Copy link
    Mannequin Author

    stutzbach mannequin commented Jul 9, 2010

    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.

    @rhettinger
    Copy link
    Contributor

    To solve the problem, KeysView and ItemsView need
    to override _from_iterable to return a set instead
    of a new view.

    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

    @elibendersky
    Copy link
    Mannequin

    elibendersky mannequin commented Jul 26, 2010

    Hello,

    I'm attaching a patch for this issue.

    1. _from_iterable in KeysView and ItemsView overridden as per Daniel's suggestion (Lib/_abcoll.py)
    2. Added a test case to Lib/test/test_collections.py that uses this test case (creates the subclass and attempts modifying the view), with Raymond's suggestions incorporated, as well as a simple correctness test.

    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.

    @durban
    Copy link
    Mannequin

    durban mannequin commented Jul 29, 2010

    The patch applies cleanly to the py3k branch (r83238). The unittests pass.
    The code looks good to me (it does exactly what was described as the solution). There are some trailing whitespace on some lines, that will need to be deleted.

    @elibendersky
    Copy link
    Mannequin

    elibendersky mannequin commented Jul 30, 2010

    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.

    @durban
    Copy link
    Mannequin

    durban mannequin commented Jul 31, 2010

    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.

    @stutzbach
    Copy link
    Mannequin Author

    stutzbach mannequin commented Aug 20, 2010

    The patch looks good to me, too. The new tests fail without the fix, and pass with the fix.

    @stutzbach stutzbach mannequin assigned rhettinger and unassigned stutzbach Aug 20, 2010
    @rhettinger
    Copy link
    Contributor

    Fixed in r84252, r84252, and r84254.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant