kisa-utils 0.36.3__py3-none-any.whl → 0.36.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ from kisa_utils.response import Response, Ok, Error
6
6
  from kisa_utils.storage import Path
7
7
  from kisa_utils.structures.validator import Value, validateWithResponse
8
8
  import copy
9
- from typing import Any
9
+ from typing import Any, get_args
10
10
 
11
11
  class Definition:
12
12
  @staticmethod
@@ -83,7 +83,8 @@ def enforceRequirements(func):
83
83
  if value.default is not inspect.Parameter.empty:
84
84
  if not (resp := validateWithResponse(value.default, typeHints[key])):
85
85
  raise ValueError(f'arg `{key}` default value: {resp.log}')
86
- registeredArgs.append((key, typeHints[key]))
86
+
87
+ registeredArgs.append((key, typeHints[key] ))
87
88
  elif value.kind == inspect.Parameter.KEYWORD_ONLY:
88
89
  if value.default is not inspect.Parameter.empty:
89
90
  if not (resp := validateWithResponse(value.default, typeHints[key])):
@@ -100,6 +101,8 @@ def enforceRequirements(func):
100
101
 
101
102
  expectectedReturnType = typeHints['return']
102
103
 
104
+ # print(registeredArgs, registeredKwargs)
105
+
103
106
  @wraps(func)
104
107
  def w(*args, **kwargs):
105
108
  for index, arg in enumerate(args):
@@ -120,14 +123,19 @@ def enforceRequirements(func):
120
123
 
121
124
  resp = func(*args, **kwargs)
122
125
 
123
- if not isinstance(resp, expectectedReturnType):
124
- if isinstance(resp, type):
125
- log = f'`{func.__name__}` returned `{resp.__name__}`, expected `{expectectedReturnType}`'
126
- else:
127
- log = f'`{func.__name__}` returned `{type(resp).__name__}`, expected `{expectectedReturnType}`'
128
-
126
+ # if not isinstance(resp, expectectedReturnType):
127
+ if not (resp := validateWithResponse(resp, expectectedReturnType)):
129
128
  if Response == expectectedReturnType: return Error(log)
130
- raise TypeError(log)
129
+
130
+ raise TypeError(f'`{func.__name__}` return error: {resp.log}')
131
+
132
+ # if isinstance(resp, type):
133
+ # log = f'`{func.__name__}` returned `{resp.__name__}`, expected `{expectectedReturnType}`'
134
+ # else:
135
+ # log = f'`{func.__name__}` returned `{type(resp).__name__}`, expected `{expectectedReturnType}`'
136
+
137
+ # if Response == expectectedReturnType: return Error(log)
138
+ # raise TypeError(log)
131
139
 
132
140
  return resp
133
141
 
@@ -1,6 +1,6 @@
1
1
  import inspect
2
2
  import typing
3
- from typing import Any, Callable
3
+ from typing import Any, Callable, get_origin
4
4
  from types import UnionType
5
5
  from kisa_utils.response import Response
6
6
 
@@ -30,13 +30,23 @@ class Value:
30
30
  raise TypeError(f'validator should take only 1 argument (not counting `self` for methods)')
31
31
 
32
32
  reply = validator(
33
- valueType() if type(valueType)==type(type) else list(typing.get_args(valueType))[0]()
33
+ valueType() if type(valueType)==type(type) \
34
+ else (
35
+ (
36
+ _:=list(typing.get_args(valueType))[0]
37
+ )
38
+ and (
39
+ get_origin(_) or _
40
+ )
41
+ )()
34
42
  )
35
43
 
36
44
  if not isinstance(reply, Response):
37
45
  raise TypeError(f'`validator` must return kutils.response.Response object')
38
46
 
39
- self._valueType = valueType
47
+ typesTuple = valueType
48
+
49
+ self._valueType = typesTuple
40
50
  self._validator = validator
41
51
 
42
52
  def validate(self, valueInstance:Any, /) -> Response:
@@ -47,8 +57,9 @@ class Value:
47
57
 
48
58
  @property
49
59
  def __name__(self) -> str:
60
+ print(self._valueType)
50
61
  if not isinstance(self._valueType, UnionType):
51
- _type = self._valueType.__name__
62
+ _type = (get_origin(self._valueType) or self._valueType).__name__
52
63
  else:
53
64
  types = self._valueType.__args__
54
65
  _type = '|'.join(_.__name__ for _ in types)
@@ -3,7 +3,7 @@ this modules handle data structure validation to ensure that
3
3
  data is passed in expected formats/structures
4
4
  '''
5
5
 
6
- from typing import Any, get_args
6
+ from typing import Any, get_args, get_origin
7
7
  from types import UnionType
8
8
  from kisa_utils.structures.utils import Value
9
9
  from kisa_utils.response import Response, Ok, Error
@@ -20,18 +20,23 @@ def validate(instance:Any, structure:Any, path:str='$') -> dict:
20
20
 
21
21
  result = {'status':False, 'log':''}
22
22
 
23
+ print(instance, structure, end=' => ')
24
+ if not isinstance(structure, UnionType) and get_origin(structure):
25
+ structure = get_origin(structure)
26
+ print(instance, structure)
27
+
23
28
  # union types such as int|float...
24
29
  if isinstance(structure, UnionType):
25
- if not isinstance(instance, get_args(structure)):
26
- expectedTypes = [str(_).split("'")[1] for _ in get_args(structure)]
30
+ if not isinstance(instance, tuple([get_origin(t) or t for t in get_args(structure)])):
31
+ expectedTypes = [str(_).split("'")[1] for _ in tuple([(get_args(t) or t) for t in get_args(structure)])]
27
32
  result['log'] = f'E06: types not similar:: {path}, expected one of {"|".join(expectedTypes)} but got {str(type(instance))[7:-1]}'
28
33
  return result
29
34
 
30
35
  # when the structure is a block type/class eg dict,list,tuple,etc
31
- elif type(structure)==type(type) or isinstance(structure, Value):
32
- _structure = structure._valueType if isinstance(structure, Value) else structure
36
+ elif type(structure)==type(type) or isinstance(structure, Value) or get_args(type):
37
+ _structure = structure._valueType if isinstance(structure, Value) else (get_args(structure) or structure)
33
38
 
34
- if instance!=_structure and not isinstance(instance, _structure):
39
+ if instance != _structure and not isinstance(instance, _structure):
35
40
  result['log'] = f'E01: types not similar:: {path}, expected {str(_structure)[7:-1] if type(structure)==type(type) else structure.__name__} but got {str(type(instance))[7:-1]}'
36
41
  return result
37
42
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kisa-utils
3
- Version: 0.36.3
3
+ Version: 0.36.4
4
4
  Summary: Utility functions and modules for KISA Developers
5
5
  Author: Tom Bukenya
6
6
  Author-email: glayn2bukman@gmail.com
@@ -7,7 +7,7 @@ kisa_utils/db.py,sha256=qMOPfBHz9qJIKUwGvSk32EyyhvEFqOpDv0MX4QseXl0,40788
7
7
  kisa_utils/encryption.py,sha256=KwSUtjZj6m2JqEeeg0GW3bx93PCpEwJlcBzLZrnReyE,3522
8
8
  kisa_utils/enqueue.py,sha256=RbImgoPNFFCQHT1ow9zJEM-tHwWE1bNnHznJqEVXL9k,11290
9
9
  kisa_utils/figures.py,sha256=ossQHBR7T9rOV1yhQJLDbwrY23xf0RIGOmjcFH7P0ss,1860
10
- kisa_utils/functionUtils.py,sha256=v8ShUZ56yxdYLMTsxRdj4vQfk6wTo0ATqB1wD1dJYYw,5420
10
+ kisa_utils/functionUtils.py,sha256=OPP9by-bpc7DssUFBpNNb0fPrUYxCnji4VlmxswHOrc,5718
11
11
  kisa_utils/log.py,sha256=EKBAVvDpY_hgALDCC6i-ARdqQzZwxBxxeHR4NsYgs9U,2120
12
12
  kisa_utils/queues.py,sha256=D0bCtI95VEg-xLuzf-Wp0Pfjc5hoEwlmzEJHuokx-i0,5418
13
13
  kisa_utils/remote.py,sha256=2EMG2kJudCYqpNPsACe3riQCqTsg-MzviVSZPbjCtxk,1793
@@ -21,9 +21,9 @@ kisa_utils/permissions/__init__.py,sha256=k7WbNlE8i9Vyf_SdbXbTh8D3gt4obDe3f8rONV
21
21
  kisa_utils/servers/__init__.py,sha256=lPqDyGTrFo0qwPZ2WA9Xtcpc5D8AIU4huqgFx1iZf68,19
22
22
  kisa_utils/servers/flask.py,sha256=niD6Cv04cs6YVMXB7MVjOQ4__78UfLLia7qHdz2FgUs,30354
23
23
  kisa_utils/structures/__init__.py,sha256=JBU1j3A42jQ62ALKnsS1Hav9YXcYwjDw1wQJtohXPbU,83
24
- kisa_utils/structures/utils.py,sha256=doZnnrKT5qGWZIOhXqBnD7mBBc7r-lhwcfpRKcK95Is,2237
25
- kisa_utils/structures/validator.py,sha256=2cKaVuY6ia6-pt6o73A6L11qInDz_tygFZdsbZU-RbA,3328
26
- kisa_utils-0.36.3.dist-info/METADATA,sha256=q8DInAIFbutxylvRMLc-_VibGckjlnXKlwDJv7baeBI,477
27
- kisa_utils-0.36.3.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
- kisa_utils-0.36.3.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
- kisa_utils-0.36.3.dist-info/RECORD,,
24
+ kisa_utils/structures/utils.py,sha256=svnzNyVL-wLBEgkovgrqDKvGM4wxYaQGuQ4fFDUR_mo,2515
25
+ kisa_utils/structures/validator.py,sha256=MPPZHXAepBWlF1UFuh5sShkqTzlqANWRLTE42gPz3oA,3647
26
+ kisa_utils-0.36.4.dist-info/METADATA,sha256=kSNW8__fbO_VuZjb5d-eN3WjLE9lr9rFUqijPoKmVKY,477
27
+ kisa_utils-0.36.4.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
+ kisa_utils-0.36.4.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
+ kisa_utils-0.36.4.dist-info/RECORD,,