Coverage for sources/frigid/exceptions.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-05 03:04 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Family of exceptions for package API. 

22 

23 Provides a hierarchy of exceptions that are raised when immutability is 

24 violated. The hierarchy is designed to allow both specific and general 

25 exception handling. 

26 

27 * ``Omniexception``: Base for all package exceptions 

28 * ``Omnierror``: Base for all package errors 

29 * ``AttributeImmutabilityError``: Raised for attribute modification 

30 * ``EntryImmutabilityError``: Raised for dictionary entry modification 

31 * ``OperationValidityError``: Raised for invalid operations 

32''' 

33 

34 

35from . import __ # pylint: disable=cyclic-import 

36 

37 

38class Omniexception( __.InternalObject, BaseException ): 

39 ''' Base for all exceptions raised by package API. ''' 

40 

41 _attribute_visibility_includes_: __.cabc.Collection[ str ] = ( 

42 frozenset( ( '__cause__', '__context__', ) ) ) 

43 

44 

45class Omnierror( Omniexception, Exception ): 

46 ''' Base for error exceptions raised by package API. ''' 

47 

48 

49class AttributeImmutabilityError( Omnierror, AttributeError, TypeError ): 

50 ''' Attempt to modify immutable attribute. ''' 

51 

52 def __init__( self, name: str ) -> None: 

53 super( ).__init__( 

54 f"Cannot assign or delete attribute {name!r}." ) 

55 

56 

57class DecoratorCompatibilityError( Omnierror, TypeError ): 

58 ''' Attempt to apply decorator to incompatible class. ''' 

59 

60 def __init__( self, class_name: str, method_name: str ) -> None: 

61 # TODO: Use helper function to extract class name from class. 

62 super( ).__init__( 

63 f"Cannot apply immutable decorator to {class_name!r} " 

64 f"because it defines {method_name!r}.") 

65 

66 

67class EntryImmutabilityError( Omnierror, TypeError ): 

68 ''' Attempt to modify immutable dictionary entry. ''' 

69 

70 def __init__( self, key: __.cabc.Hashable ) -> None: 

71 super( ).__init__( 

72 f"Cannot assign or delete entry for {key!r}." ) 

73 

74 

75class EntryValidityError( Omnierror, ValueError ): 

76 ''' Attempt to add invalid entry to dictionary. ''' 

77 

78 def __init__( self, indicator: __.cabc.Hashable, value: __.a.Any ) -> None: 

79 super( ).__init__( 

80 f"Cannot add invalid entry with key, {indicator!r}, " 

81 f"and value, {value!r}, to dictionary." ) 

82 

83 

84class OperationValidityError( Omnierror, RuntimeError, TypeError ): 

85 ''' Attempt to perform invalid operation. ''' 

86 

87 def __init__( self, name: str ) -> None: 

88 super( ).__init__( f"Operation {name!r} is not valid on this object." )