|
22 | 22 |
|
23 | 23 | class test_Type_Safe__regression(TestCase):
|
24 | 24 |
|
| 25 | + def test__regression__forward_ref_type(self): |
| 26 | + class Base__Type(Type_Safe): |
| 27 | + ref_type: Type['Base__Type'] |
| 28 | + |
| 29 | + |
| 30 | + class Type__With__Forward__Ref(Base__Type): |
| 31 | + pass |
| 32 | + |
| 33 | + target = Type__With__Forward__Ref() |
| 34 | + |
| 35 | + json_data = target.json() # This serializes ref_type as string |
| 36 | + #error_message = "Invalid type for attribute 'ref_type'. Expected 'typing.Type[ForwardRef('Base__Type')]' but got '<class 'str'>'" |
| 37 | + error_message = "Could not reconstruct type from 'test_Type_Safe__regression.Type__With__Forward__Ref': module 'test_Type_Safe__regression' has no attribute 'Type__With__Forward__Ref'" |
| 38 | + # |
| 39 | + assert json_data == {'ref_type': 'test_Type_Safe__regression.Type__With__Forward__Ref'} |
| 40 | + |
| 41 | + with pytest.raises(ValueError, match=re.escape(error_message)): |
| 42 | + Type__With__Forward__Ref.from_json(json_data) # Fixed we are now raising the correct exception BUG: exception should have not been raised |
| 43 | + |
| 44 | + |
25 | 45 | def test__regression__property_descriptor_handling(self):
|
26 | 46 |
|
27 | 47 | class Regular_Class: # First case: Normal Python class without Type_Safe
|
@@ -119,7 +139,7 @@ class Other_Class: pass
|
119 | 139 | with self.assertRaises(ValueError) as context:
|
120 | 140 | custom_node.node_type = Other_Class
|
121 | 141 |
|
122 |
| - assert str(context.exception) == "Invalid type for attribute 'node_type'. Expected 'typing.Type[ForwardRef('Base_Node')]' but got '<class 'type'>'" |
| 142 | + assert str(context.exception) == "Invalid type for attribute 'node_type'. Expected 'typing.Type[ForwardRef('Base_Node')]' but got '<class 'test_Type_Safe__regression.test_Type_Safe__regression.test__regression__forward_ref_handling_in_type_matches.<locals>.Other_Class'>'" |
123 | 143 |
|
124 | 144 | # Test with more complex case (like Schema__MGraph__Node)
|
125 | 145 | from typing import Dict, Any
|
@@ -243,14 +263,14 @@ class An_Class_1(Type_Safe):
|
243 | 263 |
|
244 | 264 | an_class.an_type__str = str
|
245 | 265 | an_class.an_type__str = Random_Guid
|
246 |
| - with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type__str'. Expected 'typing.Type[str]' but got '<class 'type'>'")) : |
| 266 | + with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type__str'. Expected 'typing.Type[str]' but got '<class 'int'>'")) : |
247 | 267 | an_class.an_type__str = int
|
248 | 268 |
|
249 | 269 | #with pytest.raises(TypeError, match=re.escape("issubclass() arg 2 must be a class, a tuple of classes, or a union")):
|
250 | 270 | # an_class.an_type__forward_ref = An_Class_1 # Fixed; BUG: this should have worked
|
251 | 271 |
|
252 | 272 | an_class.an_type__forward_ref = An_Class_1
|
253 |
| - with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type__forward_ref'. Expected 'typing.Type[ForwardRef('An_Class_1')]' but got '<class 'type'>'")): |
| 273 | + with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type__forward_ref'. Expected 'typing.Type[ForwardRef('An_Class_1')]' but got '<class 'str'>'")): |
254 | 274 | an_class.an_type__forward_ref = str
|
255 | 275 |
|
256 | 276 | class An_Class_2(An_Class_1):
|
@@ -347,13 +367,13 @@ class An_Class(Type_Safe):
|
347 | 367 | an_class.an_type_str = str
|
348 | 368 | an_class.an_type_int = int
|
349 | 369 |
|
350 |
| - with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type_str'. Expected 'typing.Type[str]' but got '<class 'type'>")): |
| 370 | + with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type_str'. Expected 'typing.Type[str]' but got '<class 'int'>")): |
351 | 371 | an_class.an_type_str = int # Fixed: BUG: should have raised exception
|
352 | 372 |
|
353 |
| - with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type_int'. Expected 'typing.Type[int]' but got '<class 'type'>")): |
| 373 | + with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type_int'. Expected 'typing.Type[int]' but got '<class 'str'>")): |
354 | 374 | an_class.an_type_int = str # Fixed: BUG: should have raised exception
|
355 | 375 |
|
356 |
| - with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type_str'. Expected 'typing.Type[str]' but got '<class 'str'>'")): |
| 376 | + with pytest.raises(ValueError, match=re.escape("Invalid type for attribute 'an_type_str'. Expected 'typing.Type[str]' but got '<class 'NoneType'>'")): |
357 | 377 | an_class.an_type_str = 'a'
|
358 | 378 |
|
359 | 379 |
|
|
0 commit comments