osbot-utils 2.15.0__py3-none-any.whl → 2.16.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.
@@ -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
@@ -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
@@ -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.16.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: osbot_utils
3
- Version: 2.15.0
3
+ Version: 2.16.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.16.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
 
@@ -289,6 +289,7 @@ osbot_utils/type_safe/Type_Safe__Dict.py,sha256=vE_Ut7MabBjOq5Hpr3vdFO5RNf-M-cL8
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
@@ -304,8 +305,8 @@ osbot_utils/type_safe/shared/Type_Safe__Validation.py,sha256=LW0vqaW1ugOMuMmuofs
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
@@ -329,7 +330,7 @@ osbot_utils/utils/Json.py,sha256=0t7Hwefx8bg4JiZVr-xIbWP3BAk6_ZsnY7iV5pnRLDQ,713
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=kbPgKTSrHcTYIQSNpjw5655sd1n45CbPrdcXIS_fj5I,8
346
+ osbot_utils-2.16.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
347
+ osbot_utils-2.16.0.dist-info/METADATA,sha256=rZqC0khpO-fwhN6NfHb27LuW5r5rCgL98vOF_yn2pYA,1329
348
+ osbot_utils-2.16.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
349
+ osbot_utils-2.16.0.dist-info/RECORD,,