@@ -75,6 +75,9 @@ def test_type_check_basic7():
7575
7676def test_type_check_basic8 ():
7777 TypeParser (Path , coercible = [(PathTypes , PathTypes )])(lz (str ))
78+
79+
80+ def test_type_check_basic8a ():
7881 TypeParser (str , coercible = [(PathTypes , PathTypes )])(lz (Path ))
7982
8083
@@ -94,6 +97,9 @@ def test_type_check_basic10():
9497
9598def test_type_check_basic11 ():
9699 TypeParser (str , coercible = [(PathTypes , PathTypes )])(lz (File ))
100+
101+
102+ def test_type_check_basic11a ():
97103 TypeParser (File , coercible = [(PathTypes , PathTypes )])(lz (str ))
98104
99105
@@ -655,12 +661,15 @@ def test_contains_type_in_dict():
655661def test_any_union ():
656662 """Check that the superclass auto-cast matches if any of the union args match instead
657663 of all"""
664+ # The Json type within the Union matches File as it is a subclass as `match_any_of_union`
665+ # is set to True. Otherwise, all types within the Union would have to match
658666 TypeParser (File , match_any_of_union = True ).check_type (ty .Union [ty .List [File ], Json ])
659667
660668
661669def test_union_superclass_check_type ():
662670 """Check that the superclass auto-cast matches if any of the union args match instead
663671 of all"""
672+ # In this case, File matches Json due to the `superclass_auto_cast=True` flag being set
664673 TypeParser (ty .Union [ty .List [File ], Json ], superclass_auto_cast = True )(lz (File ))
665674
666675
@@ -818,20 +827,42 @@ def test_typing_cast(tmp_path, specific_task, other_specific_task):
818827 assert out_file .header .parent != in_file .header .parent
819828
820829
821- def test_type_is_subclass1 ():
822- assert TypeParser .is_subclass (ty .Type [File ], type )
823-
824-
825- def test_type_is_subclass2 ():
826- assert not TypeParser .is_subclass (ty .Type [File ], ty .Type [Json ])
827-
828-
829- def test_type_is_subclass3 ():
830- assert TypeParser .is_subclass (ty .Type [Json ], ty .Type [File ])
830+ @pytest .mark .parametrize (
831+ ("sub" , "super" ),
832+ [
833+ (ty .Type [File ], type ),
834+ (ty .Type [Json ], ty .Type [File ]),
835+ (ty .Union [Json , Yaml ], ty .Union [Json , Yaml , Xml ]),
836+ (Json , ty .Union [Json , Yaml ]),
837+ (ty .List [int ], list ),
838+ (None , ty .Union [int , None ]),
839+ (ty .Tuple [int , None ], ty .Tuple [int , None ]),
840+ (None , None ),
841+ (None , type (None )),
842+ (type (None ), None ),
843+ (type (None ), type (None )),
844+ (type (None ), type (None )),
845+ ],
846+ )
847+ def test_subclass (sub , super ):
848+ assert TypeParser .is_subclass (sub , super )
831849
832850
833- def test_union_is_subclass1 ():
834- assert TypeParser .is_subclass (ty .Union [Json , Yaml ], ty .Union [Json , Yaml , Xml ])
851+ @pytest .mark .parametrize (
852+ ("sub" , "super" ),
853+ [
854+ (ty .Type [File ], ty .Type [Json ]),
855+ (ty .Union [Json , Yaml , Xml ], ty .Union [Json , Yaml ]),
856+ (ty .Union [Json , Yaml ], Json ),
857+ (list , ty .List [int ]),
858+ (ty .List [float ], ty .List [int ]),
859+ (None , ty .Union [int , float ]),
860+ (None , int ),
861+ (int , None ),
862+ ],
863+ )
864+ def test_not_subclass (sub , super ):
865+ assert not TypeParser .is_subclass (sub , super )
835866
836867
837868@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
@@ -844,18 +875,11 @@ def test_union_is_subclass1b():
844875 assert TypeParser .is_subclass (Json | Yaml , ty .Union [Json , Yaml , Xml ])
845876
846877
847- ## Up to here!
848-
849-
850878@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
851879def test_union_is_subclass1c ():
852880 assert TypeParser .is_subclass (ty .Union [Json , Yaml ], Json | Yaml | Xml )
853881
854882
855- def test_union_is_subclass2 ():
856- assert not TypeParser .is_subclass (ty .Union [Json , Yaml , Xml ], ty .Union [Json , Yaml ])
857-
858-
859883@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
860884def test_union_is_subclass2a ():
861885 assert not TypeParser .is_subclass (Json | Yaml | Xml , Json | Yaml )
@@ -871,86 +895,26 @@ def test_union_is_subclass2c():
871895 assert not TypeParser .is_subclass (Json | Yaml | Xml , ty .Union [Json , Yaml ])
872896
873897
874- def test_union_is_subclass3 ():
875- assert TypeParser .is_subclass (Json , ty .Union [Json , Yaml ])
876-
877-
878898@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
879899def test_union_is_subclass3a ():
880900 assert TypeParser .is_subclass (Json , Json | Yaml )
881901
882902
883- def test_union_is_subclass4 ():
884- assert not TypeParser .is_subclass (ty .Union [Json , Yaml ], Json )
885-
886-
887903@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
888904def test_union_is_subclass4a ():
889905 assert not TypeParser .is_subclass (Json | Yaml , Json )
890906
891907
892- def test_generic_is_subclass1 ():
893- assert TypeParser .is_subclass (ty .List [int ], list )
894-
895-
896- def test_generic_is_subclass2 ():
897- assert not TypeParser .is_subclass (list , ty .List [int ])
898-
899-
900- def test_generic_is_subclass3 ():
901- assert not TypeParser .is_subclass (ty .List [float ], ty .List [int ])
902-
903-
904- def test_none_is_subclass1 ():
905- assert TypeParser .is_subclass (None , ty .Union [int , None ])
906-
907-
908908@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
909909def test_none_is_subclass1a ():
910910 assert TypeParser .is_subclass (None , int | None )
911911
912912
913- def test_none_is_subclass2 ():
914- assert not TypeParser .is_subclass (None , ty .Union [int , float ])
915-
916-
917913@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
918914def test_none_is_subclass2a ():
919915 assert not TypeParser .is_subclass (None , int | float )
920916
921917
922- def test_none_is_subclass3 ():
923- assert TypeParser .is_subclass (ty .Tuple [int , None ], ty .Tuple [int , None ])
924-
925-
926- def test_none_is_subclass4 ():
927- assert TypeParser .is_subclass (None , None )
928-
929-
930- def test_none_is_subclass5 ():
931- assert not TypeParser .is_subclass (None , int )
932-
933-
934- def test_none_is_subclass6 ():
935- assert not TypeParser .is_subclass (int , None )
936-
937-
938- def test_none_is_subclass7 ():
939- assert TypeParser .is_subclass (None , type (None ))
940-
941-
942- def test_none_is_subclass8 ():
943- assert TypeParser .is_subclass (type (None ), None )
944-
945-
946- def test_none_is_subclass9 ():
947- assert TypeParser .is_subclass (type (None ), type (None ))
948-
949-
950- def test_none_is_subclass10 ():
951- assert TypeParser .is_subclass (type (None ), type (None ))
952-
953-
954918@pytest .mark .skipif (
955919 sys .version_info < (3 , 9 ), reason = "Cannot subscript tuple in < Py3.9"
956920)
@@ -980,60 +944,45 @@ class B(A):
980944 assert not TypeParser .is_subclass (MyTuple [B ], ty .Tuple [A , int ])
981945
982946
983- def test_type_is_instance1 ():
984- assert TypeParser .is_instance (File , ty .Type [File ])
985-
986-
987- def test_type_is_instance2 ():
988- assert not TypeParser .is_instance (File , ty .Type [Json ])
989-
990-
991- def test_type_is_instance3 ():
992- assert TypeParser .is_instance (Json , ty .Type [File ])
993-
994-
995- def test_type_is_instance4 ():
996- assert TypeParser .is_instance (Json , type )
997-
998-
999- def test_type_is_instance5 ():
1000- assert TypeParser .is_instance (None , None )
1001-
1002-
1003- def test_type_is_instance6 ():
1004- assert TypeParser .is_instance (None , type (None ))
1005-
1006-
1007- def test_type_is_instance7 ():
1008- assert not TypeParser .is_instance (None , int )
1009-
1010-
1011- def test_type_is_instance8 ():
1012- assert not TypeParser .is_instance (1 , None )
947+ @pytest .mark .parametrize (
948+ ("tp" , "obj" ),
949+ [
950+ (File , ty .Type [File ]),
951+ (Json , ty .Type [File ]),
952+ (Json , type ),
953+ (None , None ),
954+ (None , type (None )),
955+ (None , ty .Union [int , None ]),
956+ (1 , ty .Union [int , None ]),
957+ ],
958+ )
959+ def test_type_is_instance (tp , obj ):
960+ assert TypeParser .is_instance (tp , obj )
1013961
1014962
1015- def test_type_is_instance9 ():
1016- assert TypeParser .is_instance (None , ty .Union [int , None ])
963+ @pytest .mark .parametrize (
964+ ("tp" , "obj" ),
965+ [
966+ (File , ty .Type [Json ]),
967+ (None , int ),
968+ (1 , None ),
969+ (None , ty .Union [int , str ]),
970+ ],
971+ )
972+ def test_type_is_not_instance (tp , obj ):
973+ assert not TypeParser .is_instance (tp , obj )
1017974
1018975
1019976@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
1020977def test_type_is_instance9a ():
1021978 assert TypeParser .is_instance (None , int | None )
1022979
1023980
1024- def test_type_is_instance10 ():
1025- assert TypeParser .is_instance (1 , ty .Union [int , None ])
1026-
1027-
1028981@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
1029982def test_type_is_instance10a ():
1030983 assert TypeParser .is_instance (1 , int | None )
1031984
1032985
1033- def test_type_is_instance11 ():
1034- assert not TypeParser .is_instance (None , ty .Union [int , str ])
1035-
1036-
1037986@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
1038987def test_type_is_instance11a ():
1039988 assert not TypeParser .is_instance (None , int | str )
@@ -1059,32 +1008,27 @@ def test_multi_input_obj_coerce4a():
10591008 TypeParser (MultiInputObj [ty .Union [int , ty .List [str ]]])([[1 ]])
10601009
10611010
1062- def test_multi_input_obj_check_type1 ():
1063- TypeParser (MultiInputObj [str ])(lz (str ))
1064-
1065-
1066- def test_multi_input_obj_check_type2 ():
1067- TypeParser (MultiInputObj [str ])(lz (ty .List [str ]))
1068-
1069-
1070- def test_multi_input_obj_check_type3 ():
1071- TypeParser (MultiInputObj [ty .List [str ]])(lz (ty .List [str ]))
1072-
1073-
1074- def test_multi_input_obj_check_type3a ():
1075- TypeParser (MultiInputObj [ty .Union [int , ty .List [str ]]])(lz (ty .List [str ]))
1076-
1077-
1078- def test_multi_input_obj_check_type3b ():
1079- TypeParser (MultiInputObj [ty .Union [int , ty .List [str ]]])(lz (ty .List [ty .List [str ]]))
1080-
1081-
1082- def test_multi_input_obj_check_type4 ():
1083- TypeParser (MultiInputObj [ty .Union [int , ty .List [str ]]])(lz (ty .List [int ]))
1011+ @pytest .mark .parametrize (
1012+ ("reference" , "to_be_checked" ),
1013+ [
1014+ (MultiInputObj [str ], str ),
1015+ (MultiInputObj [str ], ty .List [str ]),
1016+ (MultiInputObj [ty .List [str ]], ty .List [str ]),
1017+ (MultiInputObj [ty .Union [int , ty .List [str ]]], ty .List [str ]),
1018+ (MultiInputObj [ty .Union [int , ty .List [str ]]], ty .List [ty .List [str ]]),
1019+ (MultiInputObj [ty .Union [int , ty .List [str ]]], ty .List [int ]),
1020+ ],
1021+ )
1022+ def test_multi_input_obj_check_type (reference , to_be_checked ):
1023+ TypeParser (reference )(lz (to_be_checked ))
10841024
10851025
1086- def test_multi_input_obj_check_type4a ():
1026+ @pytest .mark .parametrize (
1027+ ("reference" , "to_be_checked" ),
1028+ [
1029+ (MultiInputObj [ty .Union [int , ty .List [str ]]], ty .List [ty .List [int ]]),
1030+ ],
1031+ )
1032+ def test_multi_input_obj_check_type_fail (reference , to_be_checked ):
10871033 with pytest .raises (TypeError ):
1088- TypeParser (MultiInputObj [ty .Union [int , ty .List [str ]]])(
1089- lz (ty .List [ty .List [int ]])
1090- )
1034+ TypeParser (reference )(lz (to_be_checked ))
0 commit comments