From 1a459eebf4b85bd6e07543e3a0deec3bb85d2411 Mon Sep 17 00:00:00 2001 From: "Vgr E. Barry" Date: Wed, 25 Apr 2018 20:39:57 -0400 Subject: [PATCH] Add ability to delete items only if they exist from UserDict --- src/containers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/containers.py b/src/containers.py index bd397d5..d911982 100644 --- a/src/containers.py +++ b/src/containers.py @@ -314,6 +314,12 @@ class UserDict(dict): value.dict_values.append(self) def __delitem__(self, item): + if isinstance(item, slice): # special-case: delete if it exists, otherwise don't + if item.start is item.step is None: # checks out + item = item.stop + if item not in self: + return + value = self[item] super().__delitem__(item) if isinstance(item, User):