This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Many of MutableSet's methods assume that the other parameter is not self
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: stutzbach Nosy List: rhettinger, stutzbach
Priority: normal Keywords: patch

Created on 2010-05-18 13:44 by stutzbach, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
set-isub.patch stutzbach, 2010-05-21 16:03
Messages (5)
msg105974 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-05-18 13:44
For example, here is one of MutableSet's methods:

    def __isub__(self, it):
        for value in it:
        return self

However, if I do "x -= x", then it mutates my set-like object during iteration, which even the built-in set does not support.  ior, iand, and ixor have the same problem.

I'm working on running (with suitable modifications) on my class that derives from collections.MutableSet, so I'm going to be bumping into all kinds of fun problems like this.

I'm going to override the methods in my class, and I can contribute my new methods back to Python as a patch once they're working.
msg106037 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-05-19 05:58
Here's a possible fix:

def __isub__(self, it):
    if it is self:
        for value in it:
    return self
msg106250 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-05-21 16:03
Patch with unit test attached for MutableSet's:

x ^= x
x -= x

I was wrong about |= and &= having a problem.  Since they don't mutate the set, they don't raise an exception (they only .add items that are already in the set).  I added tests for them but left the implementation alone.
msg114639 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-08-22 06:24
The patch looks good.
Feel free to apply.
msg114831 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-08-24 21:21
Committed as r84301, r84302, and r84305.
Date User Action Args
2022-04-11 14:57:01adminsetgithub: 52996
2010-08-24 21:21:07stutzbachsetstatus: open -> closed
versions: - Python 2.6
messages: + msg114831

keywords: - needs review
stage: patch review -> resolved
2010-08-22 06:24:49rhettingersetassignee: rhettinger -> stutzbach
messages: + msg114639
2010-08-21 17:21:45rhettingersetassignee: stutzbach -> rhettinger
2010-05-24 04:49:54rhettingersetresolution: accepted
2010-05-21 16:03:53stutzbachsetkeywords: + patch, needs review
files: + set-isub.patch
messages: + msg106250

stage: test needed -> patch review
2010-05-19 05:58:48rhettingersetnosy: + rhettinger

messages: + msg106037
versions: + Python 2.6, Python 3.1
2010-05-18 13:44:20stutzbachcreate