kisa-utils 0.37.9__py3-none-any.whl → 0.37.11__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.
@@ -59,7 +59,12 @@ def protected(func:Callable) -> Callable:
59
59
  return func(*args, **kwargs)
60
60
  return wrapper
61
61
 
62
- def enforceRequirements(func:Callable) -> Callable:
62
+ # ------------------------------------------------------------------------------
63
+ from typing import ParamSpec, TypeVar
64
+ P = ParamSpec('P')
65
+ T = TypeVar('T')
66
+
67
+ def enforceRequirements(func:Callable[P, T]) -> Callable[P, T]:
63
68
  '''
64
69
  force decorated function to observe the following
65
70
  - have type hints for all `args`, and `kwargs` as well as the `return` type-hint
@@ -68,9 +73,9 @@ def enforceRequirements(func:Callable) -> Callable:
68
73
  - ensure all arguments are given values of the right type when the function is called
69
74
  - return the right data-type as specified by the return type-hint when invoked
70
75
  '''
76
+ signature = inspect.signature(func)
77
+ typeHints = func.__annotations__
71
78
  if 1:
72
- signature = inspect.signature(func)
73
- typeHints = func.__annotations__
74
79
  parameters = signature.parameters
75
80
 
76
81
  if not (func.__doc__ and func.__doc__.strip()):
@@ -84,11 +89,17 @@ def enforceRequirements(func:Callable) -> Callable:
84
89
  registeredArgs = []
85
90
  registeredKwargs = {}
86
91
 
92
+ argCount = 0
87
93
  for key, value in signature.parameters.items():
88
- if 'self' == key:
94
+ argCount += 1
95
+ if (key in ['self', 'cls']) and 1==argCount:
89
96
  registeredArgs.append((key, None))
90
97
  continue
91
98
 
99
+ hint = typeHints.get(key, None)
100
+ if not hint:
101
+ raise TypeError(f"function/method `{func.__name__}`: parameter `{key}` has no type hint")
102
+
92
103
  if value.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
93
104
  raise TypeError(
94
105
  f"function/method `{func.__name__}` should take either positional-only or keyword-only parameters. `{key}` is neither")
@@ -109,18 +120,14 @@ def enforceRequirements(func:Callable) -> Callable:
109
120
  raise ValueError(
110
121
  f'function/method `{func.__name__}`: *{key} or **{key} not allowed in function signature')
111
122
 
112
- hint = typeHints.get(key, None)
113
- if not hint:
114
- raise TypeError(f"parameter `{key}` has no type hint")
115
-
116
123
  expectectedReturnType = typeHints['return']
117
124
 
118
125
  # print(registeredArgs, registeredKwargs)
119
126
 
120
127
  @wraps(func)
121
- def w(*args, **kwargs):
128
+ def w(*args:P.args, **kwargs:P.kwargs):
122
129
  for index, arg in enumerate(args):
123
- if registeredArgs[index][0] in ['self']: continue
130
+ if (registeredArgs[index][0] in ['self', 'cls']) and 0==index: continue
124
131
  if not (resp := validateWithResponse(arg, registeredArgs[index][1])):
125
132
  log = f'arg `{registeredArgs[index][0]}`, arg-index {index}: {resp.log}'
126
133
  if Response == expectectedReturnType: return Error(log)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kisa-utils
3
- Version: 0.37.9
3
+ Version: 0.37.11
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=gU6BSBSglrndUMhmhOmIEVNXKSiRV6vqf2pskJKYmlQ,44463
7
7
  kisa_utils/encryption.py,sha256=nFzNpzWV_D9uSEq4FsgCnlS7FQtqWP9fvM_81rsfcLo,4218
8
8
  kisa_utils/enqueue.py,sha256=VIliaMvw4MUdOqts0dXdZCYNxs-QrOVjIRAR3scGrRM,11786
9
9
  kisa_utils/figures.py,sha256=pYIpQzu1OXRSsY1d98GhgPifnIRmgl-r7S32ai-Ms0c,3731
10
- kisa_utils/functionUtils.py,sha256=BV1HMkALLbBM-n-7qCFZ1u2qzITRBA8WQZK9B3nOTUE,6246
10
+ kisa_utils/functionUtils.py,sha256=PlXjnmU1uJWNdISlJJ3SCgavTsgNBoebaa9dtWSFhRA,6553
11
11
  kisa_utils/log.py,sha256=0TYdxcIBts026RCSuVIQBcZ-CW1ES7n3M1nEIjmeLTM,2295
12
12
  kisa_utils/queues.py,sha256=9QqPtDujw6tbWk7uUiXrsd0rVBTIkzeQw9b45l5Fo3k,6502
13
13
  kisa_utils/remote.py,sha256=0RDrfC4RUW4m6JLziC0_EXJYqzWp38Rw8NDroJ0MuqI,2149
@@ -23,7 +23,7 @@ kisa_utils/servers/flask.py,sha256=o76cJKlQ3L8EOVdHUF092qwoAZMzgttuLt0mMhtCsGI,4
23
23
  kisa_utils/structures/__init__.py,sha256=JBU1j3A42jQ62ALKnsS1Hav9YXcYwjDw1wQJtohXPbU,83
24
24
  kisa_utils/structures/utils.py,sha256=l56NQiPVcFijpuLqt2n9ZwnVKT4XzK6oknRVallRzwQ,2573
25
25
  kisa_utils/structures/validator.py,sha256=Y4UmB4TH7N-GkK22EV1WOsPWjTeqxVWLTentl1keZD4,4053
26
- kisa_utils-0.37.9.dist-info/METADATA,sha256=uYcqbW-zurJOnLotGzCOZMbmO-hDvjFBVLFkYGHMsRQ,477
27
- kisa_utils-0.37.9.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
- kisa_utils-0.37.9.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
- kisa_utils-0.37.9.dist-info/RECORD,,
26
+ kisa_utils-0.37.11.dist-info/METADATA,sha256=j_xQph98QIGh8sRWaVx9mXa8Jy-cIiz14egzhgxtjmc,478
27
+ kisa_utils-0.37.11.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
+ kisa_utils-0.37.11.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
+ kisa_utils-0.37.11.dist-info/RECORD,,