From 1ad7d801721d9de1d6580e7df2e18360f0961d12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Tue, 28 Apr 2020 15:52:50 +0200
Subject: [PATCH] __slots__ for Immutable

---
 CHANGELOG.md                           |  1 +
 satella/coding/structures/immutable.py | 26 ++++++++++++++++----------
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 512182b9..c4bad5f3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,4 @@
 # v2.7.16
 
 * extended `Proxy`
+* made `Immutable` utilize `__slots__`
\ No newline at end of file
diff --git a/satella/coding/structures/immutable.py b/satella/coding/structures/immutable.py
index cb8690ea..c529bd86 100644
--- a/satella/coding/structures/immutable.py
+++ b/satella/coding/structures/immutable.py
@@ -6,7 +6,7 @@ __all__ = ['Immutable', 'frozendict']
 class ImmutableMetaType(ABCMeta):
     def __call__(cls, *args, **kwargs):
         p = type.__call__(cls, *args, **kwargs)
-        p.__dict__['_Immutable__locked_for_writes'] = True
+        setattr(p, '_Immutable__locked_for_writes', True)
         return p
 
 
@@ -21,21 +21,27 @@ class Immutable(metaclass=ImmutableMetaType):
     >>>         self.attribute = 'value'
     """
 
-    __locked_for_writes = False  # type: bool
+    __slots__ = ('__locked_for_writes', )
 
     # Following make this class immutable
     def __setattr__(self, attr, value):
-        if self.__locked_for_writes:
-            raise TypeError(
-                '%s does not support attribute assignment' % (self.__class__.__qualname__,))
-        else:
+        try:
+            if self.__locked_for_writes:
+                raise TypeError(
+                    '%s does not support attribute assignment' % (self.__class__.__qualname__,))
+            else:
+                super().__setattr__(attr, value)
+        except AttributeError:
             super().__setattr__(attr, value)
 
     def __delattr__(self, attr):
-        if self.__locked_for_writes:
-            raise TypeError(
-                '%s does not support attribute deletion' % (self.__class__.__qualname__,))
-        else:
+        try:
+            if self.__locked_for_writes:
+                raise TypeError(
+                    '%s does not support attribute deletion' % (self.__class__.__qualname__,))
+            else:
+                super().__delattr__(attr)
+        except AttributeError:
             super().__delattr__(attr)
 
 
-- 
GitLab