Skip to content

Commit ee19674

Browse files
committed
added fix for type safe edge case related to using Callable variables with complex Type definitions
1 parent 55e3355 commit ee19674

File tree

4 files changed

+433
-5
lines changed

4 files changed

+433
-5
lines changed

osbot_utils/type_safe/type_safe_core/shared/Type_Safe__Validation.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def are_types_compatible_for_assigment(self, source_type, target_type):
2222
import types
2323
import typing
2424

25+
if source_type is None:
26+
return target_type is type(None) or target_type is None or target_type is typing.Any
27+
28+
2529
if isinstance(target_type, str): # If the "target_type" is a forward reference (string), handle it here.
2630
if target_type == source_type.__name__: # Simple check: does the string match the actual class name
2731
return True
@@ -119,6 +123,13 @@ def check_if__type_matches__union_type(self, annotation : Any,
119123
if value_type in args: # Direct match
120124
return True
121125

126+
if value_type is types.FunctionType: # Handle Callable types within Union (e.g., Optional[Callable[[str], None]]), hen value_type is a function, check if any union arg is a compatible Callable
127+
for arg in args:
128+
arg_origin = type_safe_cache.get_origin(arg)
129+
if arg_origin is collections.abc.Callable:
130+
return True # Accept functions for Callable annotations in unions , Full signature validation happens elsewhere
131+
132+
122133
if value_type in TYPE_MAPPINGS: # Check for typing generics equivalence
123134
if TYPE_MAPPINGS[value_type] in args: # If value_type is a built-in, check if its typing equivalent is in args
124135
return True

tests/unit/helpers/html/transformers/test_Html__To__Html_Dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from unittest import TestCase
1+
from unittest import TestCase
22
from osbot_utils.helpers.html.transformers.Html_Dict__To__Html import Html_Dict__To__Html
33
from osbot_utils.helpers.html.transformers.Html__To__Html_Dict import Html__To__Html_Dict, STRING__SCHEMA_TEXT
4-
from tests._test_data.Sample_Test_Files import Sample_Test_Files
4+
from tests._test_data.Sample_Test_Files import Sample_Test_Files
55

66

77
class test_Html__To__Html_Dict(TestCase):

tests/unit/type_safe/type_safe_core/_bugs/test_Type_Safe__bugs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,4 @@ class Extended_Config(Base_Config):
9797
# because Base_Handler is not a subclass of Extended_Handler
9898
error_message = "On Extended_Config, invalid type for attribute 'handler_type'. Expected 'typing.Type["
9999
with pytest.raises(ValueError, match=re.escape(error_message)):
100-
Extended_Config() # BUG: should auto-assign Extended_Handler
101-
102-
100+
Extended_Config() # BUG: should auto-assign Extended_Handler

0 commit comments

Comments
 (0)