osbot-utils 2.81.0__py3-none-any.whl → 2.83.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.
@@ -2,6 +2,7 @@ import collections
2
2
  import inspect # For function introspection
3
3
  from enum import Enum
4
4
  from typing import get_args, get_origin, Union, List, Any, Dict # For type hinting utilities
5
+ from osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive
5
6
  from osbot_utils.type_safe.type_safe_core.shared.Type_Safe__Shared__Variables import IMMUTABLE_TYPES
6
7
 
7
8
 
@@ -18,24 +19,53 @@ class Type_Safe__Method:
18
19
  raise ValueError(f"Parameter '{param_name}' uses lowercase 'any' instead of 'Any' from typing module. "
19
20
  f"Please use 'from typing import Any' and annotate as '{param_name}: Any'")
20
21
 
21
- def handle_type_safety(self, args: tuple, kwargs: dict): # Main method to handle type safety
22
+ def convert_primitive_parameters(self, bound_args): # Convert parameters to Type_Safe__Primitive types where applicable
23
+
24
+ for param_name, param_value in bound_args.arguments.items():
25
+ if param_name == 'self' or param_name not in self.annotations:
26
+ continue
27
+
28
+ expected_type = self.annotations[param_name]
29
+
30
+ if not (isinstance(expected_type, type) and issubclass(expected_type, Type_Safe__Primitive)): # Check if expected type is a Type_Safe__Primitive subclass
31
+ continue
32
+
33
+ primitive_base = expected_type.__primitive_base__ # Try direct conversion for common cases
34
+
35
+ if primitive_base in (int, float) and isinstance(param_value, str): # Handle string to int/float conversion
36
+ try:
37
+ bound_args.arguments[param_name] = expected_type(param_value)
38
+ continue
39
+ except (ValueError, TypeError):
40
+ pass # Let normal validation handle the error
41
+
42
+ if primitive_base and isinstance(param_value, primitive_base): # Handle values that match the primitive base type
43
+ try:
44
+ bound_args.arguments[param_name] = expected_type(param_value)
45
+ except (ValueError, TypeError):
46
+ pass # Let normal validation handle the error
47
+
48
+ def handle_type_safety(self, args: tuple, kwargs: dict): # Main method to handle type safety
22
49
  self.check_for_any_use()
23
- bound_args = self.bind_args(args, kwargs) # Bind arguments to parameters
24
- for param_name, param_value in bound_args.arguments.items(): # Iterate through arguments
25
- if param_name != 'self': # Skip self parameter
26
- self.validate_parameter(param_name, param_value, bound_args) # Validate each parameter
27
- return bound_args # Return bound arguments
28
-
29
- def bind_args(self, args: tuple, kwargs: dict): # Bind args to parameters
30
- bound_args = self.sig.bind(*args, **kwargs) # Bind arguments to signature
31
- bound_args.apply_defaults() # Apply default values
32
- return bound_args # Return bound arguments
33
-
34
- def validate_parameter(self, param_name: str, param_value: Any, bound_args): # Validate a single parameter
35
- self.validate_immutable_parameter(param_name, param_value) # Validata the param_value (make sure if it set it is on of IMMUTABLE_TYPES)
36
- if param_name in self.annotations: # Check if parameter is annotated
37
- expected_type = self.annotations[param_name] # Get expected type
38
- self.check_parameter_value(param_name, param_value, expected_type, bound_args)# Check value against type
50
+ bound_args = self.bind_args(args, kwargs) # Bind arguments to parameters
51
+
52
+ self.convert_primitive_parameters(bound_args) # Pre-process primitive type conversions
53
+
54
+ for param_name, param_value in bound_args.arguments.items(): # Iterate through arguments
55
+ if param_name != 'self': # Skip self parameter
56
+ self.validate_parameter(param_name, param_value, bound_args) # Validate each parameter
57
+ return bound_args # Return bound arguments
58
+
59
+ def bind_args(self, args: tuple, kwargs: dict): # Bind args to parameters
60
+ bound_args = self.sig.bind(*args, **kwargs) # Bind arguments to signature
61
+ bound_args.apply_defaults() # Apply default values
62
+ return bound_args # Return bound arguments
63
+
64
+ def validate_parameter(self, param_name: str, param_value: Any, bound_args): # Validate a single parameter
65
+ self.validate_immutable_parameter(param_name, param_value) # Validata the param_value (make sure if it set it is on of IMMUTABLE_TYPES)
66
+ if param_name in self.annotations: # Check if parameter is annotated
67
+ expected_type = self.annotations[param_name] # Get expected type
68
+ self.check_parameter_value(param_name, param_value, expected_type, bound_args) # Check value against type
39
69
 
40
70
  def validate_immutable_parameter(self, param_name, param_value):
41
71
  param = self.sig.parameters.get(param_name) # Check if this is a default value from a mutable type
@@ -466,8 +466,9 @@ class Files:
466
466
 
467
467
  @staticmethod
468
468
  def write(path = None,contents=None, extension=None, mode='w'):
469
- path = path or temp_file(extension)
470
- contents = contents or ''
469
+ path = path or temp_file(extension)
470
+ if contents is None:
471
+ contents = b'' if 'b' in mode else ''
471
472
  with open(file=path, mode=mode) as file:
472
473
  file.write(contents)
473
474
  return path
osbot_utils/version CHANGED
@@ -1 +1 @@
1
- v2.81.0
1
+ v2.83.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: osbot_utils
3
- Version: 2.81.0
3
+ Version: 2.83.0
4
4
  Summary: OWASP Security Bot - Utils
5
5
  License: MIT
6
6
  Author: Dinis Cruz
@@ -21,7 +21,7 @@ Description-Content-Type: text/markdown
21
21
 
22
22
  # OSBot-Utils
23
23
 
24
- ![Current Release](https://img.shields.io/badge/release-v2.81.0-blue)
24
+ ![Current Release](https://img.shields.io/badge/release-v2.83.0-blue)
25
25
  ![Python](https://img.shields.io/badge/python-3.8+-green)
26
26
  ![Type-Safe](https://img.shields.io/badge/Type--Safe-✓-brightgreen)
27
27
  ![Caching](https://img.shields.io/badge/Caching-Built--In-orange)
@@ -417,7 +417,7 @@ osbot_utils/type_safe/type_safe_core/collections/Type_Safe__Tuple.py,sha256=Kx7C
417
417
  osbot_utils/type_safe/type_safe_core/collections/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
418
418
  osbot_utils/type_safe/type_safe_core/decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
419
419
  osbot_utils/type_safe/type_safe_core/decorators/type_safe.py,sha256=wOTMvZBl5hWMz-HuIRZpPBGN0oSbXWd9wH5xxZkfLlA,1796
420
- osbot_utils/type_safe/type_safe_core/methods/Type_Safe__Method.py,sha256=U2YW4qT7T_w14ohLekRYGYXti1ygm9JfUhayeoG6g10,20192
420
+ osbot_utils/type_safe/type_safe_core/methods/Type_Safe__Method.py,sha256=2xW4x3Hy_g3-Fs5rqyOWPHsmb8pBIqMG1iRe0qjjKW0,22154
421
421
  osbot_utils/type_safe/type_safe_core/methods/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
422
422
  osbot_utils/type_safe/type_safe_core/methods/type_safe_property.py,sha256=DcJkOIs6swJtkglsZVKLyFSczCGSJISOVwAmvjCOQvo,1425
423
423
  osbot_utils/type_safe/type_safe_core/shared/Type_Safe__Annotations.py,sha256=kabSiRPYPjpMBJxfjDB5AFRTx-hX17tOznZAd_qQID4,1147
@@ -449,7 +449,7 @@ osbot_utils/utils/Csv.py,sha256=oHLVpjRJqrLMz9lubMCNEoThXWju5rNTprcwHc1zq2c,1012
449
449
  osbot_utils/utils/Dev.py,sha256=eaQ87ZcMRRcxgzA-f7OO8HjjWhbE6L_edwvXiwFZvIQ,1291
450
450
  osbot_utils/utils/Env.py,sha256=rBksAy6k-J5oAJp-S_JedVlcj1b2VK8V3zsQbacopMc,6076
451
451
  osbot_utils/utils/Exceptions.py,sha256=KyOUHkXQ_6jDTq04Xm261dbEZuRidtsM4dgzNwSG8-8,389
452
- osbot_utils/utils/Files.py,sha256=W6bac42w3fYBwpxPdPK_eKfcq1M7yiF2deVHjx2hDGE,23539
452
+ osbot_utils/utils/Files.py,sha256=ODo5QtmhQW0l1Le9qSK231MSIHqzN8TqqN1SgNvQimM,23580
453
453
  osbot_utils/utils/Functions.py,sha256=VoTrAbCHt6hulz6hVz3co8w2xoOS8wE04wyHc5_cC1c,3671
454
454
  osbot_utils/utils/Http.py,sha256=cbymd0YJjflI0K0gfq_ie4Speq-Ugko753VCSlYZuf4,8101
455
455
  osbot_utils/utils/Int.py,sha256=PmlUdU4lSwf4gJdmTVdqclulkEp7KPCVUDO6AcISMF4,116
@@ -469,8 +469,8 @@ osbot_utils/utils/Toml.py,sha256=grjWkVPIMVkawJ499FVIJKxQp8FJ2wcsd0Z3YIR4drM,114
469
469
  osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
470
470
  osbot_utils/utils/Zip.py,sha256=mG42lgTY0tnm14T3P1-DSAIZKkTiYoO3odZ1aOUdc1I,14394
471
471
  osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
472
- osbot_utils/version,sha256=mbIGfCYdUBYBX5IKj_GwRFeqsLHQ9YVfPa2A46aaruM,8
473
- osbot_utils-2.81.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
474
- osbot_utils-2.81.0.dist-info/METADATA,sha256=SxdVFb1ciwxBQdtsaYUtzLmGKHJAsJR7rSD9cRA4neM,7918
475
- osbot_utils-2.81.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
476
- osbot_utils-2.81.0.dist-info/RECORD,,
472
+ osbot_utils/version,sha256=qwiNjSxD3OqsUSFY0ezoeNZIUfvD-vn0S4zNzFmoc50,8
473
+ osbot_utils-2.83.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
474
+ osbot_utils-2.83.0.dist-info/METADATA,sha256=sKYHVqnqbtD8rlM8ZOCZTXN5ddMjIOM1TtneLGfAlcg,7918
475
+ osbot_utils-2.83.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
476
+ osbot_utils-2.83.0.dist-info/RECORD,,