@@ -75,6 +75,9 @@ def test_type_check_basic7():
75
75
76
76
def test_type_check_basic8 ():
77
77
TypeParser (Path , coercible = [(PathTypes , PathTypes )])(lz (str ))
78
+
79
+
80
+ def test_type_check_basic8a ():
78
81
TypeParser (str , coercible = [(PathTypes , PathTypes )])(lz (Path ))
79
82
80
83
@@ -94,6 +97,9 @@ def test_type_check_basic10():
94
97
95
98
def test_type_check_basic11 ():
96
99
TypeParser (str , coercible = [(PathTypes , PathTypes )])(lz (File ))
100
+
101
+
102
+ def test_type_check_basic11a ():
97
103
TypeParser (File , coercible = [(PathTypes , PathTypes )])(lz (str ))
98
104
99
105
@@ -655,12 +661,15 @@ def test_contains_type_in_dict():
655
661
def test_any_union ():
656
662
"""Check that the superclass auto-cast matches if any of the union args match instead
657
663
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
658
666
TypeParser (File , match_any_of_union = True ).check_type (ty .Union [ty .List [File ], Json ])
659
667
660
668
661
669
def test_union_superclass_check_type ():
662
670
"""Check that the superclass auto-cast matches if any of the union args match instead
663
671
of all"""
672
+ # In this case, File matches Json due to the `superclass_auto_cast=True` flag being set
664
673
TypeParser (ty .Union [ty .List [File ], Json ], superclass_auto_cast = True )(lz (File ))
665
674
666
675
@@ -818,20 +827,42 @@ def test_typing_cast(tmp_path, specific_task, other_specific_task):
818
827
assert out_file .header .parent != in_file .header .parent
819
828
820
829
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 )
831
849
832
850
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 )
835
866
836
867
837
868
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
@@ -844,18 +875,11 @@ def test_union_is_subclass1b():
844
875
assert TypeParser .is_subclass (Json | Yaml , ty .Union [Json , Yaml , Xml ])
845
876
846
877
847
- ## Up to here!
848
-
849
-
850
878
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
851
879
def test_union_is_subclass1c ():
852
880
assert TypeParser .is_subclass (ty .Union [Json , Yaml ], Json | Yaml | Xml )
853
881
854
882
855
- def test_union_is_subclass2 ():
856
- assert not TypeParser .is_subclass (ty .Union [Json , Yaml , Xml ], ty .Union [Json , Yaml ])
857
-
858
-
859
883
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
860
884
def test_union_is_subclass2a ():
861
885
assert not TypeParser .is_subclass (Json | Yaml | Xml , Json | Yaml )
@@ -871,86 +895,26 @@ def test_union_is_subclass2c():
871
895
assert not TypeParser .is_subclass (Json | Yaml | Xml , ty .Union [Json , Yaml ])
872
896
873
897
874
- def test_union_is_subclass3 ():
875
- assert TypeParser .is_subclass (Json , ty .Union [Json , Yaml ])
876
-
877
-
878
898
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
879
899
def test_union_is_subclass3a ():
880
900
assert TypeParser .is_subclass (Json , Json | Yaml )
881
901
882
902
883
- def test_union_is_subclass4 ():
884
- assert not TypeParser .is_subclass (ty .Union [Json , Yaml ], Json )
885
-
886
-
887
903
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
888
904
def test_union_is_subclass4a ():
889
905
assert not TypeParser .is_subclass (Json | Yaml , Json )
890
906
891
907
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
-
908
908
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
909
909
def test_none_is_subclass1a ():
910
910
assert TypeParser .is_subclass (None , int | None )
911
911
912
912
913
- def test_none_is_subclass2 ():
914
- assert not TypeParser .is_subclass (None , ty .Union [int , float ])
915
-
916
-
917
913
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
918
914
def test_none_is_subclass2a ():
919
915
assert not TypeParser .is_subclass (None , int | float )
920
916
921
917
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
-
954
918
@pytest .mark .skipif (
955
919
sys .version_info < (3 , 9 ), reason = "Cannot subscript tuple in < Py3.9"
956
920
)
@@ -980,60 +944,45 @@ class B(A):
980
944
assert not TypeParser .is_subclass (MyTuple [B ], ty .Tuple [A , int ])
981
945
982
946
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 )
1013
961
1014
962
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 )
1017
974
1018
975
1019
976
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
1020
977
def test_type_is_instance9a ():
1021
978
assert TypeParser .is_instance (None , int | None )
1022
979
1023
980
1024
- def test_type_is_instance10 ():
1025
- assert TypeParser .is_instance (1 , ty .Union [int , None ])
1026
-
1027
-
1028
981
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
1029
982
def test_type_is_instance10a ():
1030
983
assert TypeParser .is_instance (1 , int | None )
1031
984
1032
985
1033
- def test_type_is_instance11 ():
1034
- assert not TypeParser .is_instance (None , ty .Union [int , str ])
1035
-
1036
-
1037
986
@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
1038
987
def test_type_is_instance11a ():
1039
988
assert not TypeParser .is_instance (None , int | str )
@@ -1059,32 +1008,27 @@ def test_multi_input_obj_coerce4a():
1059
1008
TypeParser (MultiInputObj [ty .Union [int , ty .List [str ]]])([[1 ]])
1060
1009
1061
1010
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 ))
1084
1024
1085
1025
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 ):
1087
1033
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