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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-24 04:09 +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''' 

32 

33 

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

35 

36 

37class Omniexception( __.ImmutableObject, BaseException ): 

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

39 

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

41 frozenset( ( '__cause__', '__context__', ) ) ) 

42 

43 

44class Omnierror( Omniexception, Exception ): 

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

46 

47 

48class AttributeImmutabilityError( Omnierror, AttributeError, TypeError ): 

49 ''' Attempt to modify immutable attribute. ''' 

50 

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

52 super( ).__init__( 

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

54 

55 

56class DecoratorCompatibilityError( Omnierror, TypeError ): 

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

58 

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

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

61 super( ).__init__( 

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

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

64 

65 

66class EntryImmutabilityError( Omnierror, TypeError ): 

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

68 

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

70 super( ).__init__( 

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

72 

73 

74class EntryValidityError( Omnierror, ValueError ): 

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

76 

77 def __init__( 

78 self, indicator: __.cabc.Hashable, value: __.typx.Any 

79 ) -> None: 

80 super( ).__init__( 

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

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

83 

84 

85class OperationInvalidity( Omnierror, RuntimeError, TypeError ): 

86 ''' Attempt to perform invalid operation. ''' 

87 

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

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