Coverage for sources/ictr/__/validators.py: 87%

37 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-23 14:03 +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''' Validators for internal use. ''' 

22 

23 

24 

25from . import imports as __ 

26 

27 

28def validate_arguments( 

29 globalvars: dict[ str, __.typx.Any ], 

30 errorclass: type[ Exception ], 

31): 

32 ''' Decorator factory which produces argument validators. ''' 

33 

34 def decorate( function: __.cabc.Callable[ ..., __.typx.Any ] ): 

35 ''' Decorates function to be validated. ''' 

36 

37 @__.funct.wraps( function ) 

38 def validate( *posargs: __.typx.Any, **nomargs: __.typx.Any ): 

39 ''' Validates arguments before invocation. ''' 

40 signature = __.inspect.signature( function ) 

41 inspectee = signature.bind( *posargs, **nomargs ) 

42 inspectee.apply_defaults( ) 

43 for name, value in inspectee.arguments.items( ): 

44 param = signature.parameters[ name ] 

45 annotation = param.annotation 

46 if __.is_absent( value ): continue 

47 if annotation is param.empty: continue 

48 classes = _reduce_annotation( 

49 annotation, globalvars = globalvars ) 

50 if not isinstance( value, classes ): 

51 if ( __.io.TextIOBase in classes 51 ↛ 54line 51 didn't jump to line 54 because the condition on line 51 was always true

52 and callable( getattr( value, 'write', None ) ) ): 

53 continue # Duck-typed text stream. 

54 raise errorclass( name, classes ) 

55 return function( *posargs, **nomargs ) 

56 

57 return validate 

58 

59 return decorate 

60 

61 

62def _reduce_annotation( # noqa: PLR0911 

63 annotation: __.typx.Any, globalvars: dict[ str, __.typx.Any ] 

64) -> tuple[ type, ... ]: 

65 if isinstance( annotation, str ): 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true

66 return _reduce_annotation( 

67 eval( annotation, globalvars ), # noqa: S307 

68 globalvars = globalvars ) 

69 origin = __.typx.get_origin( annotation ) 

70 if isinstance( annotation, __.types.UnionType ) or origin is __.typx.Union: 

71 return tuple( __.itert.chain.from_iterable( 

72 map( 

73 lambda a: _reduce_annotation( a, globalvars = globalvars ), 

74 __.typx.get_args( annotation ) ) ) ) 

75 if origin is None: 

76 if annotation is __.typx.IO: return ( __.io.IOBase, ) 76 ↛ exitline 76 didn't return from function '_reduce_annotation' because the return on line 76 wasn't executed

77 if annotation is __.typx.TextIO: return ( __.io.TextIOBase, ) 

78 if annotation is __.typx.BinaryIO: 78 ↛ 79line 78 didn't jump to line 79 because the condition on line 78 was never true

79 return ( __.io.RawIOBase, __.io.BufferedIOBase ) 

80 return ( annotation, ) 

81 if origin is __.typx.IO: return ( __.io.IOBase, ) 81 ↛ exitline 81 didn't return from function '_reduce_annotation' because the return on line 81 wasn't executed

82 if origin is __.typx.Annotated: 

83 return _reduce_annotation( 

84 annotation.__origin__, globalvars = globalvars ) 

85 # TODO? Other special forms. 

86 return ( origin, )