@@ -570,4 +570,85 @@ def method_with_kwargs(self, name: str, **kwargs): # The bug is caused
570570 # assert result != expected # BUG: This is what we expect
571571 # assert result == current # BUG: This is what we get
572572 assert result == expected
573- assert result != with_bug
573+ assert result != with_bug
574+
575+ def test__regression__list_of_type_T__not_supported (self ): # Document bug where List[Type[SomeClass]] fails validation in @type_safe decorator
576+
577+ # Define a base class that we want to use in Type[T]
578+ class Base_Handler (Type_Safe ):
579+ name : str = "base"
580+
581+ class Handler_A (Base_Handler ):
582+ name : str = "handler_a"
583+
584+ class Handler_B (Base_Handler ):
585+ name : str = "handler_b"
586+
587+ # Define a class with a @type_safe decorated method that takes List[Type[Base_Handler]]
588+ class Executor (Type_Safe ):
589+
590+ @type_safe
591+ def execute (self ,
592+ handler_classes : List [Type [Base_Handler ]], # This is the problematic type
593+ name : str = "test"
594+ ) -> int :
595+ return len (handler_classes )
596+
597+ # BUG: This should work but raises NotImplementedError
598+ with Executor () as executor :
599+ # Empty list should work
600+ #with pytest.raises(NotImplementedError, match="Validation for list items with subscripted type"):
601+ # executor.execute(handler_classes=[]) # BUG: fails even with empty list
602+ assert executor .execute (handler_classes = []) == 0 # FIXED
603+
604+ # List with valid classes should work
605+ # with pytest.raises(NotImplementedError, match="Validation for list items with subscripted type"):
606+ # executor.execute(handler_classes=[Handler_A, Handler_B]) # BUG: fails
607+ assert executor .execute (handler_classes = [Handler_A , Handler_B ]) == 2 # FIXED
608+
609+ def test__regression__list_of_type_T__expected_behavior (self ): # Document what SHOULD happen when bug is fixed
610+
611+ class Base_Handler (Type_Safe ):
612+ name : str = "base"
613+
614+ class Handler_A (Base_Handler ):
615+ name : str = "handler_a"
616+
617+ class Handler_B (Base_Handler ):
618+ name : str = "handler_b"
619+
620+ class Unrelated_Class (Type_Safe ):
621+ value : int = 0
622+
623+ class Executor (Type_Safe ):
624+
625+ @type_safe
626+ def execute (self ,
627+ handler_classes : List [Type [Base_Handler ]],
628+ name : str = "test"
629+ ) -> int :
630+ return len (handler_classes )
631+
632+ executor = Executor ()
633+
634+ # When fixed, these should be the expected behaviors:
635+ #
636+ # 1. Empty list should work:
637+ assert executor .execute (handler_classes = []) == 0 # Should return 0
638+ #
639+ # 2. List with valid subclasses should work:
640+ assert executor .execute (handler_classes = [Handler_A , Handler_B ]) == 2 # Should return 2
641+
642+ # 3. List with base class itself should work:
643+ assert executor .execute (handler_classes = [Base_Handler ]) == 1 # Should return 1
644+
645+ #
646+ # 4. List with non-subclass should raise ValueError:
647+ error_message_1 = "List item at index 0 expected subclass of Base_Handler, but got Unrelated_Class"
648+ with pytest .raises (ValueError ,match = error_message_1 ):
649+ executor .execute (handler_classes = [Unrelated_Class ])
650+
651+ # 5. List with non-type should raise ValueError:
652+ error_message_2 = "List item at index 0 expected a type/class but got Handler_A"
653+ with pytest .raises (ValueError ,match = error_message_2 ):
654+ executor .execute (handler_classes = [Handler_A ()]) # instance, not class
0 commit comments