8000 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. Retry
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added | and |= operators to WeakValueDictionary
Updated weakref.py
Added tests
Updated docs
Updated news
  • Loading branch information
curtisbucher committed Mar 18, 2020
commit bc4e7e7cf5656b7709d60714da1ef6f99ca9b0e5
2 changes: 2 additions & 0 deletions Doc/library/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ than needed.
same issues as the :meth:`keyrefs` method of :class:`WeakKeyDictionary`
objects.

.. versionchanged:: 3.9
Added support for ``|`` and ``|=`` operators, specified in :pep:`584`.
Copy link
Member
@brandtbucher brandtbucher Mar 23, 2020

Choose a reason for hiding this comment

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

I think this is better moved up to line 174, just after the ..note:: (and at the same indentation level).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it should be more like line 200, because I believe line 174 is under WeakKeyDictionary rather than WeakValueDictionary?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, nice catch.


.. method:: WeakValueDictionary.valuerefs()

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,21 @@ def test_weak_valued_dict_update(self):
self.assertEqual(list(d.keys()), [kw])
self.assertEqual(d[kw], o)

def test_weak_valued_union_operators(self):
class C: pass
c1 = C()
c2 = C()
c3 = C()

wvd1 = weakref.WeakValueDictionary({'1' : c1, '2': c2})
wvd2 = weakref.WeakValueDictionary({'1': c3, '4': c1})

wvd3 = wvd1 | wvd2
self.assertEqual(dict(wvd3), dict(wvd1) | dict(wvd2))

wvd1 |= wvd2
self.assertEqual(wvd1, wvd3)

def test_weak_keyed_dict_update(self):
self.check_update(weakref.WeakKeyDictionary,
{C(): 1, C(): 2, C(): 3})
Expand Down
14 changes: 14 additions & 0 deletions Lib/weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ def valuerefs(self):
self._commit_removals()
return list(self.data.values())

def __ior__(self, other):
self.update(other)
return self

def __or__(self, other):
c = self.copy()
c.update(other)
return c

def __ror__(self, other):
c = other.copy()
c.update(self)
return c


class KeyedRef(ref):
"""Specialized reference that includes a key corresponding to the value.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :pep:`584` operators to :class:`weakref.WeakValueDictionary`.
0