bpo-36144: Dictionary Union (PEP 584) (#12088)

This commit is contained in:
Brandt Bucher 2020-02-24 19:47:34 -08:00 committed by GitHub
parent ba22e8f174
commit eb8ac57af2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 18 deletions

View file

@ -994,6 +994,26 @@ class UserDict(_collections_abc.MutableMapping):
# Now, add the methods in dicts but not in MutableMapping
def __repr__(self): return repr(self.data)
def __or__(self, other):
if isinstance(other, UserDict):
return self.__class__(self.data | other.data)
if isinstance(other, dict):
return self.__class__(self.data | other)
return NotImplemented
def __ror__(self, other):
if isinstance(other, UserDict):
return self.__class__(other.data | self.data)
if isinstance(other, dict):
return self.__class__(other | self.data)
return NotImplemented
def __ior__(self, other):
if isinstance(other, UserDict):
self.data |= other.data
else:
self.data |= other
return self
def __copy__(self):
inst = self.__class__.__new__(self.__class__)
inst.__dict__.update(self.__dict__)