Coverage for Users / vladimirpavlov / PycharmProjects / parameterizable / src / mixinforge / not_picklable_mixin.py: 100%

7 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-01 16:37 -0600

1class NotPicklableMixin: 

2 """A mixin class that prevents objects from being pickled or unpickled. 

3 

4 This class provides a mechanism to explicitly prevent instances from being 

5 serialized using Python's pickle module. Classes that inherit from this 

6 mixin will raise TypeError exceptions when pickle attempts to serialize 

7 or deserialize them. 

8 

9 This is useful for objects that contain non-serializable resources or 

10 should not be persisted for security or architectural reasons. 

11 """ 

12 

13 def __reduce__(self): 

14 """Prevent object serialization by raising TypeError. 

15 

16 This method is called by the pickle module to get a picklable 

17 representation of the object. It unconditionally raises 

18 a TypeError to prevent pickling. 

19 

20 Raises: 

21 TypeError: Always raised to prevent the object from being pickled. 

22 """ 

23 raise TypeError(f"{type(self).__name__} cannot be pickled") 

24 

25 

26 def __getstate__(self): 

27 """Prevent object serialization by raising TypeError. 

28 

29 This method is called by pickle when attempting to serialize the object. 

30 It unconditionally raises a TypeError to prevent pickling. 

31 

32 Raises: 

33 TypeError: Always raised to prevent the object from being pickled. 

34 """ 

35 raise TypeError(f"{type(self).__name__} cannot be pickled") 

36 

37 

38 def __setstate__(self, state): 

39 """Prevent object deserialization by raising TypeError. 

40 

41 This method is called by pickle when attempting to deserialize the object. 

42 It unconditionally raises a TypeError to prevent unpickling. 

43 

44 Args: 

45 state: The state dictionary that would be used for unpickling. 

46 

47 Raises: 

48 TypeError: Always raised to prevent the object from being unpickled. 

49 """ 

50 raise TypeError(f"{type(self).__name__} cannot be unpickled")