10000 bpo-36144: Add union operators to WeakValueDictionary584 by curtisbucher · Pull Request #19127 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-36144: Add union operators to WeakValueDictionary584 #19127

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

Merged
merged 5 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update test_weakref.py and weakref.py
  • Loading branch information
curtisbucher committed Mar 23, 2020
commit cef9fd2d218740eafc81329ce8f5d251a8cf23da
42 changes: 32 additions & 10 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,19 +1610,41 @@ def test_weak_valued_dict_update(self):
self.assertEqual(d[kw], o)

def test_weak_valued_union_operators(self):
class C: pass
c1 = C()
c2 = C()
c3 = C()
a = C()
b = C()
c = C()
wvd1 = weakref.WeakValueDictionary({1 : a})
wvd2 = weakref.WeakValueDictionary({1 : b, 2 : a})
wvd3 = wvd1.copy()
d1 = {1 : c, 3 : b}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space before :, ever!

Suggested change
wvd1 = weakref.WeakValueDictionary({1 : a})
wvd2 = weakref.WeakValueDictionary({1 : b, 2 : a})
wvd3 = wvd1.copy()
d1 = {1 : c, 3 : b}
wvd1 = weakref.WeakValueDictionary({1: a})
wvd2 = weakref.WeakValueDictionary({1: b, 2: a})
wvd3 = wvd1.copy()
d1 = {1: c, 3: b}

pairs = [(5, c), (5, b)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting test case, but I just want to verify that you intended to use the same key twice here.

You didn't mean something like this, which results in a mapping of length 2?

Suggested change
pairs = [(5, c), (5, b)]
pairs = [(5, c), (6, b)]


tmp1 = wvd1 | wvd2 # Between two WeakValueDictionaries
self.assertEqual(dict(tmp1), dict(wvd1) | dict(wvd2))
self.assertIs(type(tmp1), weakref.WeakValueDictionary)
wvd1 |= wvd2
self.assertEqual(wvd1, tmp1)

wvd1 = weakref.WeakValueDictionary({'1' : c1, '2': c2})
wvd2 = weakref.WeakValueDictionary({'1': c3, '4': c1})
tmp2 = wvd2 | d1 # Between WeakValueDictionary and mapping
self.assertEqual(dict(tmp2), dict(wvd2) | d1)
self.assertIs(type(tmp2), weakref.WeakValueDictionary)
wvd2 |= d1
self.assertEqual(wvd2, tmp2)

wvd3 = wvd1 | wvd2
self.assertEqual(dict(wvd3), dict(wvd1) | dict(wvd2))
tmp3 = wvd3.copy() # Between WeakValueDictionary and iterable key, value
tmp3 |= pairs
self.assertEqual(dict(tmp3), dict(wvd3) | dict(pairs))
self.assertIs(type(tmp3), weakref.WeakValueDictionary)

wvd1 |= wvd2
self.assertEqual(wvd1, wvd3)
tmp4 = d1 | wvd3 # Testing .__ror__
self.assertEqual(dict(tmp4), d1 | dict(wvd3))
self.assertIs(type(tmp4), weakref.WeakValueDictionary)

del a
self.assertNotIn(2, tmp1.keys())
self.assertNotIn(2, tmp2.keys())
self.assertNotIn(1, tmp3.keys())
self.assertNotIn(1, tmp4.keys())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the keys() calls here:

Suggested change
self.assertNotIn(2, tmp1.keys())
self.assertNotIn(2, tmp2.keys())
self.assertNotIn(1, tmp3.keys())
self.assertNotIn(1, tmp4.keys())
self.assertNotIn(2, tmp1)
self.assertNotIn(2, tmp2)
self.assertNotIn(1, tmp3)
self.assertNotIn(1, tmp4)


def test_weak_keyed_dict_update(self):
self.check_update(weakref.WeakKeyDictionary,
Expand Down
17 changes: 11 additions & 6 deletions Lib/weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,19 @@ def __ior__(self, other):
return self

def __or__(self, other):
c = self.copy()
c.update(other)
return c
if isinstance(other, _collections_abc.Mapping):
c = self.copy()
c.update(other)
return c
return NotImplemented

def __ror__(self, other):
c = other.copy()
c.update(self)
return c
if isinstance(other, _collections_abc.Mapping):
c = self.__class__()
c.update(other)
c.update(self)
return c
return NotImplemented


class KeyedRef(ref):
Expand Down
0