osbot-utils 2.15.0__py3-none-any.whl → 2.17.0__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.
@@ -1,4 +1,5 @@
1
1
  from typing import get_args, Union, Optional, Any, ForwardRef
2
+ from osbot_utils.helpers.Obj_Id import Obj_Id
2
3
  from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
3
4
 
4
5
  EXACT_TYPE_MATCH = (int, float, str, bytes, bool, complex)
@@ -63,6 +64,8 @@ class Type_Safe__Base:
63
64
  if len(args) != len(item):
64
65
  raise TypeError(f"Expected tuple of length {len(args)}, but got {len(item)}")
65
66
  for idx, (elem, elem_type) in enumerate(zip(item, args)):
67
+ if elem_type is Obj_Id: # todo: refactor this out, and figure out better way to handle this kind of de-serialisation
68
+ elem = elem_type(elem)
66
69
  try:
67
70
  self.is_instance_of_type(elem, elem_type)
68
71
  except TypeError as e:
@@ -0,0 +1,41 @@
1
+ from osbot_utils.type_safe.Type_Safe__Base import Type_Safe__Base, type_str
2
+
3
+ class Type_Safe__Tuple(Type_Safe__Base, tuple):
4
+
5
+ def __new__(cls, expected_types, items=None):
6
+ items = items or tuple()
7
+ instance = super().__new__(cls, items)
8
+ instance.expected_types = expected_types
9
+ return instance
10
+
11
+ def __init__(self, expected_types, items=None): # todo: see if we should be assining expected_types to self here
12
+ if items:
13
+ self.validate_items(items)
14
+
15
+ def validate_items(self, items):
16
+ if len(items) != len(self.expected_types):
17
+ raise ValueError(f"Expected {len(self.expected_types)} elements, got {len(items)}")
18
+ for item, expected_type in zip(items, self.expected_types):
19
+ try:
20
+ self.is_instance_of_type(item, expected_type)
21
+ except TypeError as e:
22
+ raise TypeError(f"In Type_Safe__Tuple: Invalid type for item: {e}")
23
+
24
+ def __repr__(self):
25
+ types_str = ', '.join(type_str(t) for t in self.expected_types)
26
+ return f"tuple[{types_str}] with {len(self)} elements"
27
+
28
+ def json(self):
29
+ from osbot_utils.type_safe.Type_Safe import Type_Safe
30
+
31
+ result = []
32
+ for item in self:
33
+ if isinstance(item, Type_Safe):
34
+ result.append(item.json())
35
+ elif isinstance(item, (list, tuple)):
36
+ result.append([x.json() if isinstance(x, Type_Safe) else x for x in item])
37
+ elif isinstance(item, dict):
38
+ result.append({k: v.json() if isinstance(v, Type_Safe) else v for k, v in item.items()})
39
+ else:
40
+ result.append(item)
41
+ return result
@@ -246,7 +246,7 @@ class Type_Safe__Validation:
246
246
  type_safe_raise_exception.immutable_type_error(var_name, var_type)
247
247
 
248
248
  def validate_variable_type(self, var_name, var_type, var_value): # Validate type compatibility
249
- if var_type and not isinstance(var_value, var_type):
249
+ if type(var_type) is type and not isinstance(var_value, var_type):
250
250
  type_safe_raise_exception.type_mismatch_error(var_name, var_type, type(var_value))
251
251
 
252
252
  type_safe_validation = Type_Safe__Validation()
@@ -3,11 +3,12 @@ import sys
3
3
  import inspect
4
4
  import typing
5
5
 
6
- from osbot_utils.type_safe.Type_Safe__Set import Type_Safe__Set
7
- from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
8
- from osbot_utils.utils.Objects import default_value
9
- from osbot_utils.type_safe.Type_Safe__List import Type_Safe__List
10
- from osbot_utils.type_safe.Type_Safe__Dict import Type_Safe__Dict
6
+ from osbot_utils.type_safe.Type_Safe__Set import Type_Safe__Set
7
+ from osbot_utils.type_safe.Type_Safe__Tuple import Type_Safe__Tuple
8
+ from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
9
+ from osbot_utils.utils.Objects import default_value
10
+ from osbot_utils.type_safe.Type_Safe__List import Type_Safe__List
11
+ from osbot_utils.type_safe.Type_Safe__Dict import Type_Safe__Dict
11
12
 
12
13
 
13
14
  # Backport implementations of get_args for Python 3.7 # todo: refactor into separate class (focused on past python version compatibility)
@@ -27,6 +28,11 @@ class Type_Safe__Step__Default_Value:
27
28
  def default_value(self, _cls, var_type):
28
29
 
29
30
  origin = type_safe_cache.get_origin(var_type) # todo: refactor this to use the get_origin method
31
+
32
+ if origin is tuple:
33
+ item_types = get_args(var_type)
34
+ return Type_Safe__Tuple(expected_types=item_types)
35
+
30
36
  if origin is type: # Special handling for Type[T] # todo: reuse the get_origin value
31
37
  type_args = get_args(var_type)
32
38
  if type_args:
@@ -73,8 +79,9 @@ class Type_Safe__Step__Default_Value:
73
79
  if forward_name == _cls.__name__: # if the forward reference is to the current class (simple name check)
74
80
  item_type = _cls # set the item_type to the current class
75
81
  return Type_Safe__List(expected_type=item_type) # and used it as expected_type in Type_Safe__List
76
- else:
77
- return default_value(var_type) # for all other cases call default_value, which will try to create a default instance
82
+
83
+
84
+ return default_value(var_type) # for all other cases call default_value, which will try to create a default instance
78
85
 
79
86
 
80
87
  type_safe_step_default_value = Type_Safe__Step__Default_Value()
@@ -53,6 +53,9 @@ class Type_Safe__Step__From_Json:
53
53
  value = self.deserialize_type__using_value(value)
54
54
  elif annotation_origin == type: # Handle type objects inside ForwardRef
55
55
  value = self.deserialize_type__using_value(value)
56
+ if annotation_origin is tuple and isinstance(value, list):
57
+ # item_types = get_args(annotation) # todo: see if we should do type safety here
58
+ value = tuple(value)
56
59
  elif type_safe_annotations.obj_is_attribute_annotation_of_type(_self, key, dict): # handle the case when the value is a dict
57
60
  value = self.deserialize_dict__using_key_value_annotations(_self, key, value)
58
61
  elif type_safe_annotations.obj_is_attribute_annotation_of_type(_self, key, set): # handle the case when the value is a list
@@ -99,6 +102,7 @@ class Type_Safe__Step__From_Json:
99
102
  value = Random_Guid_Short(value)
100
103
  elif type_safe_annotations.obj_is_attribute_annotation_of_type(_self, key, Timestamp_Now): # handle Timestamp_Now
101
104
  value = Timestamp_Now(value)
105
+
102
106
  setattr(_self, key, value) # Direct assignment for primitive types and other structures
103
107
 
104
108
  return _self
osbot_utils/utils/Json.py CHANGED
@@ -170,7 +170,7 @@ json_file_create_gz = Json.save_file_gz
170
170
  json_file_contents = Json.load_file
171
171
  json_file_contents_gz = Json.load_file_gz
172
172
  json_file_load = Json.load_file
173
- json_file_safe = Json.save_file
173
+ json_file_save = Json.save_file
174
174
  json_from_file = Json.load_file
175
175
  json_load_file = Json.load_file
176
176
  json_load_file_and_delete = Json.load_file_and_delete
@@ -184,6 +184,7 @@ json_lines_loads = Json.loads_json_lines
184
184
  json_parse = Json.loads
185
185
  json_lines_parse = Json.loads_json_lines
186
186
  json_to_bytes = json_dumps_to_bytes
187
+ json_to_file = json_file_save
187
188
  json_to_str = json_dumps
188
189
  json_round_trip = Json.round_trip
189
190
  json_save = Json.save_file
@@ -279,7 +279,7 @@ def serialize_to_dict(obj):
279
279
  return obj.name
280
280
  elif isinstance(obj, type):
281
281
  return f"{obj.__module__}.{obj.__name__}" # save the full type name
282
- elif isinstance(obj, list) or isinstance(obj, List):
282
+ elif isinstance(obj, (list, tuple, List)): # Added tuple here
283
283
  return [serialize_to_dict(item) for item in obj]
284
284
  elif isinstance(obj, set):
285
285
  return [serialize_to_dict(item) for item in obj]
osbot_utils/version CHANGED
@@ -1 +1 @@
1
- v2.15.0
1
+ v2.17.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: osbot_utils
3
- Version: 2.15.0
3
+ Version: 2.17.0
4
4
  Summary: OWASP Security Bot - Utils
5
5
  License: MIT
6
6
  Author: Dinis Cruz
@@ -23,7 +23,7 @@ Description-Content-Type: text/markdown
23
23
 
24
24
  Powerful Python util methods and classes that simplify common apis and tasks.
25
25
 
26
- ![Current Release](https://img.shields.io/badge/release-v2.15.0-blue)
26
+ ![Current Release](https://img.shields.io/badge/release-v2.17.0-blue)
27
27
  [![codecov](https://codecov.io/gh/owasp-sbot/OSBot-Utils/graph/badge.svg?token=GNVW0COX1N)](https://codecov.io/gh/owasp-sbot/OSBot-Utils)
28
28
 
29
29
 
@@ -284,11 +284,12 @@ osbot_utils/testing/performance/models/Model__Performance_Measure__Measurement.p
284
284
  osbot_utils/testing/performance/models/Model__Performance_Measure__Result.py,sha256=k9HJYNLmW6sjRVsfpduSxHFiVANc1zmYtO_Oz9azpW8,755
285
285
  osbot_utils/testing/performance/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
286
286
  osbot_utils/type_safe/Type_Safe.py,sha256=Yd6s60Ykgke8YuIone-69Ozc4uzEq2fGHu97HTmhydU,5514
287
- osbot_utils/type_safe/Type_Safe__Base.py,sha256=MD9Peg_nI1X0Z_eaEYfxUkCLgu_jFNAMq3KoUvI_wrw,4945
287
+ osbot_utils/type_safe/Type_Safe__Base.py,sha256=Bzrh8TxFglB0FEUAQTlgovlnFHnBN4lOX1Vu327r1Vc,5223
288
288
  osbot_utils/type_safe/Type_Safe__Dict.py,sha256=vE_Ut7MabBjOq5Hpr3vdFO5RNf-M-cL83S76CvxD-9g,2488
289
289
  osbot_utils/type_safe/Type_Safe__List.py,sha256=SzSIBkwSOAEpW_V2qh4-f0YHzmgB0T8PczBLbDgZGvg,1340
290
290
  osbot_utils/type_safe/Type_Safe__Method.py,sha256=LOG2sqQyKBNNEiJxk_jngH6IWvpyVueouAzNTlj-_pY,12676
291
291
  osbot_utils/type_safe/Type_Safe__Set.py,sha256=hNpAokK9nldwAXVDarJ3DXdf8sDrxog7GOMwciPLxg0,1216
292
+ osbot_utils/type_safe/Type_Safe__Tuple.py,sha256=A7CuAy_Hz1iUMSojumReOG-OwPQqEO7MSiCHL2sJRKU,1709
292
293
  osbot_utils/type_safe/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
293
294
  osbot_utils/type_safe/decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
294
295
  osbot_utils/type_safe/decorators/type_safe.py,sha256=ERFfJuAIo5qLp03YEDu2zu5wxu65OhR7hOybwuTfLlc,1006
@@ -300,12 +301,12 @@ osbot_utils/type_safe/shared/Type_Safe__Convert.py,sha256=mS92_sKjKM_aNSB3ERMEgv
300
301
  osbot_utils/type_safe/shared/Type_Safe__Not_Cached.py,sha256=25FAl6SOLxdStco_rm9tgOYLfuKyBWheGdl7vVa56UU,800
301
302
  osbot_utils/type_safe/shared/Type_Safe__Raise_Exception.py,sha256=pbru8k8CTQMNUfmFBndiJhg2KkqEYzFvJAPcNZHeHfQ,829
302
303
  osbot_utils/type_safe/shared/Type_Safe__Shared__Variables.py,sha256=SuZGl9LryQX6IpOE0I_lbzClT-h17UNylC__-M8ltTY,129
303
- osbot_utils/type_safe/shared/Type_Safe__Validation.py,sha256=LW0vqaW1ugOMuMmuofstC_DRr2QCtRvhtaK2IC2N7KA,16301
304
+ osbot_utils/type_safe/shared/Type_Safe__Validation.py,sha256=CNGuTlfGCwm35d_MQ1ssiNFr2OxsbgI_MczqH_-EK6g,16315
304
305
  osbot_utils/type_safe/shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
305
306
  osbot_utils/type_safe/steps/Type_Safe__Step__Class_Kwargs.py,sha256=snoyJKvZ1crgF2fp0zexwNPnV_E63RfyRIsMAZdrKNY,6995
306
307
  osbot_utils/type_safe/steps/Type_Safe__Step__Default_Kwargs.py,sha256=tzKXDUc0HVP5QvCWsmcPuuZodNvQZ9FeMDNI2x00Ngw,1943
307
- osbot_utils/type_safe/steps/Type_Safe__Step__Default_Value.py,sha256=MF2aNV3iE3gedohueXFQyp54yLi5q5ZsRFCFavNrUwo,4283
308
- osbot_utils/type_safe/steps/Type_Safe__Step__From_Json.py,sha256=wUIWmWRF1b2v1aavw-JGkoo462fWool4TmQ6yaycIsQ,11658
308
+ osbot_utils/type_safe/steps/Type_Safe__Step__Default_Value.py,sha256=K_tkVQyLUbbWYzDnzoPLCgDBAFYyUjAG4VdLsvjzL1g,4485
309
+ osbot_utils/type_safe/steps/Type_Safe__Step__From_Json.py,sha256=BO1WNfudAP9PqyXQZkRDTldeL2InxHhgU_6q_27MH3M,11890
309
310
  osbot_utils/type_safe/steps/Type_Safe__Step__Init.py,sha256=v4FD7zxQiOFLiOF1Ma8wZMP8aLgRlXwJZnsIfBu2zeg,1266
310
311
  osbot_utils/type_safe/steps/Type_Safe__Step__Set_Attr.py,sha256=VuKHH9QEYlbAL9R4zwQ5dwexx2sFY6wMx52QmF7eqcg,5219
311
312
  osbot_utils/type_safe/steps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -325,11 +326,11 @@ osbot_utils/utils/Files.py,sha256=yxteAcyhDcUy1_r9Eihx80V16lV_UAE6cvoOe2Dc7DU,23
325
326
  osbot_utils/utils/Functions.py,sha256=0E6alPJ0fJpBiJgFOWooCOi265wSRyxxXAJ5CELBnso,3498
326
327
  osbot_utils/utils/Http.py,sha256=Cm_-b2EgxKoQJ47ThZp-dgHCdeGv4UcCNLfTOH94-7s,7790
327
328
  osbot_utils/utils/Int.py,sha256=PmlUdU4lSwf4gJdmTVdqclulkEp7KPCVUDO6AcISMF4,116
328
- osbot_utils/utils/Json.py,sha256=0t7Hwefx8bg4JiZVr-xIbWP3BAk6_ZsnY7iV5pnRLDQ,7137
329
+ osbot_utils/utils/Json.py,sha256=Ux9lXFAjr51etPpzG3J4phlTnY78_4iA-l0uphWTCEY,7183
329
330
  osbot_utils/utils/Json_Cache.py,sha256=mLPkkDZN-3ZVJiDvV1KBJXILtKkTZ4OepzOsDoBPhWg,2006
330
331
  osbot_utils/utils/Lists.py,sha256=tPz5x5s3sRO97WZ_nsxREBPC5cwaHrhgaYBhsrffTT8,5599
331
332
  osbot_utils/utils/Misc.py,sha256=H_xexJgiTxB3jDeDiW8efGQbO0Zuy8MM0iQ7qXC92JI,17363
332
- osbot_utils/utils/Objects.py,sha256=cLqAcWZDmg0fQ-UzvaGfPJUNTDysjpe9vvnUMdS7uQ8,12845
333
+ osbot_utils/utils/Objects.py,sha256=Jw_oe906vQXZ9TQfticocW_nHbUgitx7fVNdztfTOhs,12855
333
334
  osbot_utils/utils/Png.py,sha256=V1juGp6wkpPigMJ8HcxrPDIP4bSwu51oNkLI8YqP76Y,1172
334
335
  osbot_utils/utils/Process.py,sha256=lr3CTiEkN3EiBx3ZmzYmTKlQoPdkgZBRjPulMxG-zdo,2357
335
336
  osbot_utils/utils/Python_Logger.py,sha256=M9Oi62LxfnRSlCN8GhaiwiBINvcSdGy39FCWjyDD-Xg,12792
@@ -341,8 +342,8 @@ osbot_utils/utils/Toml.py,sha256=Rxl8gx7mni5CvBAK-Ai02EKw-GwtJdd3yeHT2kMloik,166
341
342
  osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
342
343
  osbot_utils/utils/Zip.py,sha256=pR6sKliUY0KZXmqNzKY2frfW-YVQEVbLKiyqQX_lc-8,14052
343
344
  osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
344
- osbot_utils/version,sha256=gf67sFGJXvIGewhJ2sIJVpmhyMDhFOebBB4lpwFdnJQ,8
345
- osbot_utils-2.15.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
346
- osbot_utils-2.15.0.dist-info/METADATA,sha256=dEoOytlb_nsLsdH77ScV2OiCPq6yZJQeXdICjziNQOE,1329
347
- osbot_utils-2.15.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
348
- osbot_utils-2.15.0.dist-info/RECORD,,
345
+ osbot_utils/version,sha256=ZHBqd_qW1PoBoW3OWG9plgM2aZqI6Nk0w_6jIgeVD8k,8
346
+ osbot_utils-2.17.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
347
+ osbot_utils-2.17.0.dist-info/METADATA,sha256=tErQvlAQMNNnFWmAWdtStwvyVOhF2kNJveDfuEPTWsI,1329
348
+ osbot_utils-2.17.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
349
+ osbot_utils-2.17.0.dist-info/RECORD,,