Skip to content

Commit

Permalink
fixed bug in Type_Safe_Method where it didn't check for Enums in Type…
Browse files Browse the repository at this point in the history
… Safe conversions
  • Loading branch information
DinisCruz committed Dec 6, 2024
1 parent 2726c05 commit dfb786b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
33 changes: 20 additions & 13 deletions osbot_utils/helpers/Type_Safe_Method.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect # For function introspection
from typing import get_args, get_origin, Optional, Union, List, Any, Dict # For type hinting utilities
import inspect # For function introspection
from enum import Enum
from typing import get_args, get_origin, Optional, Union, List, Any, Dict # For type hinting utilities


class Type_Safe_Method: # Class to handle method type safety validation
Expand Down Expand Up @@ -86,17 +87,23 @@ def validate_union_type(self, param_name: str,
if not any(isinstance(param_value, arg_type) for arg_type in args_types): # Check if value matches any type
raise ValueError(f"Parameter '{param_name}' expected one of types {args_types}, but got {type(param_value)}") # Raise error if no match

def try_basic_type_conversion(self, param_value: Any, # Try to convert basic types
expected_type: Any, param_name: str, # Conversion parameters
bound_args) -> bool: # Return success flag
if type(param_value) in [int, str]: # Check if basic type
try: # Attempt conversion
converted_value = expected_type(param_value) # Convert value
bound_args.arguments[param_name] = converted_value # Update bound arguments
return True # Return success
except Exception: # Handle conversion failure
pass # Continue without conversion
return False # Return failure
def try_basic_type_conversion(self, param_value: Any, expected_type: Any, param_name: str,bound_args) -> bool: # Try to convert basic types
if type(param_value) in [int, str]: # Check if basic type
try: # Attempt conversion
converted_value = expected_type(param_value) # Convert value
bound_args.arguments[param_name] = converted_value # Update bound arguments
return True # Return success
except Exception: # Handle conversion failure
pass # Continue without conversion
elif isinstance(param_value, Enum): # Check if value is an Enum
try:
if issubclass(expected_type, str): # If expecting string type
bound_args.arguments[param_name] = param_value.value # Use enum's value
return True # Return success
except Exception: # Handle conversion failure
pass # Continue without conversion
return False # Return failure
# Return failure

def validate_direct_type(self, param_name: str, # Validate direct type match
param_value: Any, expected_type: Any): # Type parameters
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/helpers/test_Type_Safe_Method.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from unittest import TestCase
from typing import Optional, Union, List
from osbot_utils.helpers.Safe_Id import Safe_Id
Expand Down Expand Up @@ -119,4 +120,20 @@ def test_validate_direct_type(self):
self.type_checker.validate_direct_type('param_a', Safe_Id('test'), Safe_Id) # Valid type

with self.assertRaises(ValueError): # Invalid type
self.type_checker.validate_direct_type('param_a', 'not_safe', Safe_Id)
self.type_checker.validate_direct_type('param_a', 'not_safe', Safe_Id)


def test_try_basic_type_conversion_enum(self): # Test enum conversion
class TestEnum(Enum): # Define test enum
TEST = 'test-value' # With test value

bound_args = self.type_checker.bind_args( # Create bound args
('self', Safe_Id('test'), None, Random_Guid(),
[Safe_Id('d')], None), {})

self.assertTrue(self.type_checker.try_basic_type_conversion( # Test valid enum conversion
TestEnum.TEST, Safe_Id, 'param_a', bound_args))

self.assertEqual( # Verify converted value
bound_args.arguments['param_a'],
Safe_Id('test-value'))

0 comments on commit dfb786b

Please sign in to comment.