osbot-utils 2.14.0__py3-none-any.whl → 2.15.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.
- osbot_utils/helpers/xml/Xml__File__Load.py +1 -1
- osbot_utils/type_safe/Type_Safe__Base.py +2 -3
- osbot_utils/type_safe/Type_Safe__Set.py +32 -0
- osbot_utils/type_safe/shared/Type_Safe__Validation.py +4 -1
- osbot_utils/type_safe/steps/Type_Safe__Step__Default_Value.py +8 -2
- osbot_utils/type_safe/steps/Type_Safe__Step__From_Json.py +16 -2
- osbot_utils/utils/Objects.py +2 -0
- osbot_utils/version +1 -1
- {osbot_utils-2.14.0.dist-info → osbot_utils-2.15.0.dist-info}/METADATA +2 -2
- {osbot_utils-2.14.0.dist-info → osbot_utils-2.15.0.dist-info}/RECORD +12 -11
- {osbot_utils-2.14.0.dist-info → osbot_utils-2.15.0.dist-info}/LICENSE +0 -0
- {osbot_utils-2.14.0.dist-info → osbot_utils-2.15.0.dist-info}/WHEEL +0 -0
@@ -1,7 +1,7 @@
|
|
1
1
|
from io import StringIO
|
2
2
|
from typing import List, Union, Dict
|
3
3
|
from xml.etree.ElementTree import iterparse, Element, fromstring, ParseError
|
4
|
-
from osbot_utils.type_safe.Type_Safe
|
4
|
+
from osbot_utils.type_safe.Type_Safe import Type_Safe
|
5
5
|
from osbot_utils.helpers.xml.Xml__Attribute import Xml__Attribute
|
6
6
|
from osbot_utils.helpers.xml.Xml__Element import XML__Element
|
7
7
|
from osbot_utils.helpers.xml.Xml__File import Xml__File
|
@@ -1,6 +1,5 @@
|
|
1
|
-
from typing
|
2
|
-
|
3
|
-
from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
|
1
|
+
from typing import get_args, Union, Optional, Any, ForwardRef
|
2
|
+
from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
|
4
3
|
|
5
4
|
EXACT_TYPE_MATCH = (int, float, str, bytes, bool, complex)
|
6
5
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
from osbot_utils.type_safe.Type_Safe__Base import Type_Safe__Base, type_str
|
2
|
+
|
3
|
+
class Type_Safe__Set(Type_Safe__Base, set):
|
4
|
+
def __init__(self, expected_type, *args):
|
5
|
+
super().__init__(*args)
|
6
|
+
self.expected_type = expected_type
|
7
|
+
|
8
|
+
def __repr__(self):
|
9
|
+
expected_type_name = type_str(self.expected_type)
|
10
|
+
return f"set[{expected_type_name}] with {len(self)} elements"
|
11
|
+
|
12
|
+
def add(self, item):
|
13
|
+
try:
|
14
|
+
self.is_instance_of_type(item, self.expected_type)
|
15
|
+
except TypeError as e:
|
16
|
+
raise TypeError(f"In Type_Safe__Set: Invalid type for item: {e}")
|
17
|
+
super().add(item)
|
18
|
+
|
19
|
+
def json(self):
|
20
|
+
from osbot_utils.type_safe.Type_Safe import Type_Safe
|
21
|
+
|
22
|
+
result = []
|
23
|
+
for item in self:
|
24
|
+
if isinstance(item, Type_Safe):
|
25
|
+
result.append(item.json())
|
26
|
+
elif isinstance(item, (list, tuple, set)):
|
27
|
+
result.append([x.json() if isinstance(x, Type_Safe) else x for x in item])
|
28
|
+
elif isinstance(item, dict):
|
29
|
+
result.append({k: v.json() if isinstance(v, Type_Safe) else v for k, v in item.items()})
|
30
|
+
else:
|
31
|
+
result.append(item)
|
32
|
+
return result
|
@@ -159,6 +159,9 @@ class Type_Safe__Validation:
|
|
159
159
|
attr_type = annotations.get(attr_name)
|
160
160
|
if attr_type:
|
161
161
|
origin_attr_type = get_origin(attr_type) # to handle when type definition contains a generic
|
162
|
+
if origin_attr_type is set:
|
163
|
+
if type(value) is list:
|
164
|
+
return True # if the attribute is a set and the value is a list, then they are compatible
|
162
165
|
if origin_attr_type is type: # Add handling for Type[T]
|
163
166
|
type_arg = get_args(attr_type)[0] # Get T from Type[T]
|
164
167
|
if type_arg == value:
|
@@ -220,7 +223,7 @@ class Type_Safe__Validation:
|
|
220
223
|
) -> None: # Raises ValueError if invalid
|
221
224
|
|
222
225
|
direct_type_match = type_safe_validation.check_if__type_matches__obj_annotation__for_attr(target, name, value)
|
223
|
-
union_type_match
|
226
|
+
union_type_match = type_safe_validation.check_if__type_matches__obj_annotation__for_union_and_annotated(target, name, value)
|
224
227
|
|
225
228
|
is_invalid = (direct_type_match is False and union_type_match is None) or \
|
226
229
|
(direct_type_match is None and union_type_match is False) or \
|
@@ -3,6 +3,7 @@ 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
|
6
7
|
from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
|
7
8
|
from osbot_utils.utils.Objects import default_value
|
8
9
|
from osbot_utils.type_safe.Type_Safe__List import Type_Safe__List
|
@@ -40,12 +41,17 @@ class Type_Safe__Step__Default_Value:
|
|
40
41
|
return set()
|
41
42
|
|
42
43
|
if origin is set:
|
43
|
-
|
44
|
+
item_type = get_args(var_type)[0]
|
45
|
+
if isinstance(item_type, ForwardRef):
|
46
|
+
forward_name = item_type.__forward_arg__
|
47
|
+
if forward_name == _cls.__name__:
|
48
|
+
item_type = _cls
|
49
|
+
return Type_Safe__Set(expected_type=item_type)
|
44
50
|
|
45
51
|
if var_type is typing.Dict:
|
46
52
|
return {}
|
47
53
|
|
48
|
-
if origin is dict:
|
54
|
+
if origin is dict: # e.g. Dict[key_type, value_type]
|
49
55
|
key_type, value_type = get_args(var_type)
|
50
56
|
if isinstance(key_type, ForwardRef): # Handle forward references on key_type ---
|
51
57
|
forward_name = key_type.__forward_arg__
|
@@ -6,9 +6,10 @@ from osbot_utils.type_safe.Type_Safe import Type_Safe
|
|
6
6
|
from osbot_utils.type_safe.Type_Safe__List import Type_Safe__List
|
7
7
|
from osbot_utils.helpers.Random_Guid import Random_Guid
|
8
8
|
from osbot_utils.helpers.Random_Guid_Short import Random_Guid_Short
|
9
|
+
from osbot_utils.type_safe.Type_Safe__Set import Type_Safe__Set
|
9
10
|
from osbot_utils.type_safe.shared.Type_Safe__Annotations import type_safe_annotations
|
10
11
|
from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache
|
11
|
-
from osbot_utils.type_safe.shared.Type_Safe__Convert
|
12
|
+
from osbot_utils.type_safe.shared.Type_Safe__Convert import type_safe_convert
|
12
13
|
from osbot_utils.utils.Objects import enum_from_value
|
13
14
|
from osbot_utils.helpers.Safe_Id import Safe_Id
|
14
15
|
from osbot_utils.helpers.Timestamp_Now import Timestamp_Now
|
@@ -54,6 +55,19 @@ class Type_Safe__Step__From_Json:
|
|
54
55
|
value = self.deserialize_type__using_value(value)
|
55
56
|
elif type_safe_annotations.obj_is_attribute_annotation_of_type(_self, key, dict): # handle the case when the value is a dict
|
56
57
|
value = self.deserialize_dict__using_key_value_annotations(_self, key, value)
|
58
|
+
elif type_safe_annotations.obj_is_attribute_annotation_of_type(_self, key, set): # handle the case when the value is a list
|
59
|
+
attribute_annotation = type_safe_annotations.obj_attribute_annotation(_self, key) # get the annotation for this variable
|
60
|
+
attribute_annotation_args = get_args(attribute_annotation)
|
61
|
+
if attribute_annotation_args:
|
62
|
+
expected_type = get_args(attribute_annotation)[0] # get the first arg (which is the type)
|
63
|
+
type_safe_set = Type_Safe__Set(expected_type) # create a new instance of Type_Safe__List
|
64
|
+
for item in value: # next we need to convert all items (to make sure they all match the type)
|
65
|
+
if type(item) is dict:
|
66
|
+
new_item = expected_type(**item) # create new object
|
67
|
+
else:
|
68
|
+
new_item = expected_type(item)
|
69
|
+
type_safe_set.add(new_item) # and add it to the new type_safe_list obejct
|
70
|
+
value = type_safe_set # todo: refactor out this create list code, maybe to an deserialize_from_list method
|
57
71
|
elif type_safe_annotations.obj_is_attribute_annotation_of_type(_self, key, list): # handle the case when the value is a list
|
58
72
|
attribute_annotation = type_safe_annotations.obj_attribute_annotation(_self, key) # get the annotation for this variable
|
59
73
|
attribute_annotation_args = get_args(attribute_annotation)
|
@@ -123,7 +137,7 @@ class Type_Safe__Step__From_Json:
|
|
123
137
|
|
124
138
|
if type(dict_value) == value_class: # if the value is already the target, then just use it
|
125
139
|
new__dict_value = dict_value
|
126
|
-
elif issubclass(value_class, Type_Safe):
|
140
|
+
elif isinstance(value_class, type) and issubclass(value_class, Type_Safe):
|
127
141
|
if 'node_type' in dict_value:
|
128
142
|
value_class = type_safe_convert.get_class_from_class_name(dict_value['node_type'])
|
129
143
|
|
osbot_utils/utils/Objects.py
CHANGED
@@ -281,6 +281,8 @@ def serialize_to_dict(obj):
|
|
281
281
|
return f"{obj.__module__}.{obj.__name__}" # save the full type name
|
282
282
|
elif isinstance(obj, list) or isinstance(obj, List):
|
283
283
|
return [serialize_to_dict(item) for item in obj]
|
284
|
+
elif isinstance(obj, set):
|
285
|
+
return [serialize_to_dict(item) for item in obj]
|
284
286
|
elif isinstance(obj, dict):
|
285
287
|
return {key: serialize_to_dict(value) for key, value in obj.items()}
|
286
288
|
elif hasattr(obj, "__dict__"):
|
osbot_utils/version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
v2.
|
1
|
+
v2.15.0
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: osbot_utils
|
3
|
-
Version: 2.
|
3
|
+
Version: 2.15.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
|
-

|
27
27
|
[](https://codecov.io/gh/owasp-sbot/OSBot-Utils)
|
28
28
|
|
29
29
|
|
@@ -246,7 +246,7 @@ osbot_utils/helpers/trace/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJW
|
|
246
246
|
osbot_utils/helpers/xml/Xml__Attribute.py,sha256=_dIVyp0WHfdv306vAj5bpEtiqKa83MLKRH925rjKa94,145
|
247
247
|
osbot_utils/helpers/xml/Xml__Element.py,sha256=NLRdiTsRhqRf0I0ScAdN-tHuSh2qNuKP_tldx7iiSv4,868
|
248
248
|
osbot_utils/helpers/xml/Xml__File.py,sha256=ECR4WD57ePyA88uioKVt5GVbWXddM_Y1OsWAJNzAg74,420
|
249
|
-
osbot_utils/helpers/xml/Xml__File__Load.py,sha256=
|
249
|
+
osbot_utils/helpers/xml/Xml__File__Load.py,sha256=7eiDOxoMhwJVioJdc3Mh4o99lhXy_gjFKQttBjvrk94,3625
|
250
250
|
osbot_utils/helpers/xml/Xml__File__To_Dict.py,sha256=BPkO7dnXHSPcgwNocsMb6za7jf4hVWsyvC6OWf9DwVg,2061
|
251
251
|
osbot_utils/helpers/xml/Xml__File__To_Xml.py,sha256=kJEatLUw3-PQ4qZWQ74AE8GwFpwPnVpznluiKtMJwtA,2884
|
252
252
|
osbot_utils/helpers/xml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -284,10 +284,11 @@ 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=
|
287
|
+
osbot_utils/type_safe/Type_Safe__Base.py,sha256=MD9Peg_nI1X0Z_eaEYfxUkCLgu_jFNAMq3KoUvI_wrw,4945
|
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
|
+
osbot_utils/type_safe/Type_Safe__Set.py,sha256=hNpAokK9nldwAXVDarJ3DXdf8sDrxog7GOMwciPLxg0,1216
|
291
292
|
osbot_utils/type_safe/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
292
293
|
osbot_utils/type_safe/decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
293
294
|
osbot_utils/type_safe/decorators/type_safe.py,sha256=ERFfJuAIo5qLp03YEDu2zu5wxu65OhR7hOybwuTfLlc,1006
|
@@ -299,12 +300,12 @@ osbot_utils/type_safe/shared/Type_Safe__Convert.py,sha256=mS92_sKjKM_aNSB3ERMEgv
|
|
299
300
|
osbot_utils/type_safe/shared/Type_Safe__Not_Cached.py,sha256=25FAl6SOLxdStco_rm9tgOYLfuKyBWheGdl7vVa56UU,800
|
300
301
|
osbot_utils/type_safe/shared/Type_Safe__Raise_Exception.py,sha256=pbru8k8CTQMNUfmFBndiJhg2KkqEYzFvJAPcNZHeHfQ,829
|
301
302
|
osbot_utils/type_safe/shared/Type_Safe__Shared__Variables.py,sha256=SuZGl9LryQX6IpOE0I_lbzClT-h17UNylC__-M8ltTY,129
|
302
|
-
osbot_utils/type_safe/shared/Type_Safe__Validation.py,sha256=
|
303
|
+
osbot_utils/type_safe/shared/Type_Safe__Validation.py,sha256=LW0vqaW1ugOMuMmuofstC_DRr2QCtRvhtaK2IC2N7KA,16301
|
303
304
|
osbot_utils/type_safe/shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
304
305
|
osbot_utils/type_safe/steps/Type_Safe__Step__Class_Kwargs.py,sha256=snoyJKvZ1crgF2fp0zexwNPnV_E63RfyRIsMAZdrKNY,6995
|
305
306
|
osbot_utils/type_safe/steps/Type_Safe__Step__Default_Kwargs.py,sha256=tzKXDUc0HVP5QvCWsmcPuuZodNvQZ9FeMDNI2x00Ngw,1943
|
306
|
-
osbot_utils/type_safe/steps/Type_Safe__Step__Default_Value.py,sha256=
|
307
|
-
osbot_utils/type_safe/steps/Type_Safe__Step__From_Json.py,sha256=
|
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
309
|
osbot_utils/type_safe/steps/Type_Safe__Step__Init.py,sha256=v4FD7zxQiOFLiOF1Ma8wZMP8aLgRlXwJZnsIfBu2zeg,1266
|
309
310
|
osbot_utils/type_safe/steps/Type_Safe__Step__Set_Attr.py,sha256=VuKHH9QEYlbAL9R4zwQ5dwexx2sFY6wMx52QmF7eqcg,5219
|
310
311
|
osbot_utils/type_safe/steps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -328,7 +329,7 @@ osbot_utils/utils/Json.py,sha256=0t7Hwefx8bg4JiZVr-xIbWP3BAk6_ZsnY7iV5pnRLDQ,713
|
|
328
329
|
osbot_utils/utils/Json_Cache.py,sha256=mLPkkDZN-3ZVJiDvV1KBJXILtKkTZ4OepzOsDoBPhWg,2006
|
329
330
|
osbot_utils/utils/Lists.py,sha256=tPz5x5s3sRO97WZ_nsxREBPC5cwaHrhgaYBhsrffTT8,5599
|
330
331
|
osbot_utils/utils/Misc.py,sha256=H_xexJgiTxB3jDeDiW8efGQbO0Zuy8MM0iQ7qXC92JI,17363
|
331
|
-
osbot_utils/utils/Objects.py,sha256=
|
332
|
+
osbot_utils/utils/Objects.py,sha256=cLqAcWZDmg0fQ-UzvaGfPJUNTDysjpe9vvnUMdS7uQ8,12845
|
332
333
|
osbot_utils/utils/Png.py,sha256=V1juGp6wkpPigMJ8HcxrPDIP4bSwu51oNkLI8YqP76Y,1172
|
333
334
|
osbot_utils/utils/Process.py,sha256=lr3CTiEkN3EiBx3ZmzYmTKlQoPdkgZBRjPulMxG-zdo,2357
|
334
335
|
osbot_utils/utils/Python_Logger.py,sha256=M9Oi62LxfnRSlCN8GhaiwiBINvcSdGy39FCWjyDD-Xg,12792
|
@@ -340,8 +341,8 @@ osbot_utils/utils/Toml.py,sha256=Rxl8gx7mni5CvBAK-Ai02EKw-GwtJdd3yeHT2kMloik,166
|
|
340
341
|
osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
|
341
342
|
osbot_utils/utils/Zip.py,sha256=pR6sKliUY0KZXmqNzKY2frfW-YVQEVbLKiyqQX_lc-8,14052
|
342
343
|
osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
343
|
-
osbot_utils/version,sha256=
|
344
|
-
osbot_utils-2.
|
345
|
-
osbot_utils-2.
|
346
|
-
osbot_utils-2.
|
347
|
-
osbot_utils-2.
|
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,,
|
File without changes
|
File without changes
|