@@ -120,13 +120,26 @@ def test_type_check_basic15():
120
120
TypeParser (ty .Union [Path , File , float ])(lz (int ))
121
121
122
122
123
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
124
+ def test_type_check_basic15a ():
125
+ TypeParser (Path | File | float )(lz (int ))
126
+
127
+
123
128
def test_type_check_basic16 ():
124
129
with pytest .raises (
125
130
TypeError , match = "Cannot coerce <class 'float'> to any of the union types"
126
131
):
127
132
TypeParser (ty .Union [Path , File , bool , int ])(lz (float ))
128
133
129
134
135
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
136
+ def test_type_check_basic16a ():
137
+ with pytest .raises (
138
+ TypeError , match = "Cannot coerce <class 'float'> to any of the union types"
139
+ ):
140
+ TypeParser (Path | File | bool | int )(lz (float ))
141
+
142
+
130
143
def test_type_check_basic17 ():
131
144
TypeParser (ty .Sequence )(lz (ty .Tuple [int , ...]))
132
145
@@ -194,6 +207,12 @@ def test_type_check_fail2():
194
207
TypeParser (ty .Union [Path , File ])(lz (int ))
195
208
196
209
210
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
211
+ def test_type_check_fail2a ():
212
+ with pytest .raises (TypeError , match = "to any of the union types" ):
213
+ TypeParser (Path | File )(lz (int ))
214
+
215
+
197
216
def test_type_check_fail3 ():
198
217
with pytest .raises (TypeError , match = "doesn't match any of the explicit inclusion" ):
199
218
TypeParser (ty .Sequence , coercible = [(ty .Sequence , ty .Sequence )])(
@@ -312,13 +331,32 @@ def test_type_coercion_basic12():
312
331
assert TypeParser (ty .Union [Path , File , int ], coercible = [(ty .Any , ty .Any )])(1.0 ) == 1
313
332
314
333
334
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
335
+ def test_type_coercion_basic12a ():
336
+ with pytest .raises (TypeError , match = "explicitly excluded" ):
337
+ TypeParser (
338
+ list ,
339
+ coercible = [(ty .Sequence , ty .Sequence )],
340
+ not_coercible = [(str , ty .Sequence )],
341
+ )("a-string" )
342
+
343
+ assert TypeParser (Path | File | int , coercible = [(ty .Any , ty .Any )])(1.0 ) == 1
344
+
345
+
315
346
def test_type_coercion_basic13 ():
316
347
assert (
317
348
TypeParser (ty .Union [Path , File , bool , int ], coercible = [(ty .Any , ty .Any )])(1.0 )
318
349
is True
319
350
)
320
351
321
352
353
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
354
+ def test_type_coercion_basic13a ():
355
+ assert (
356
+ TypeParser (Path | File | bool | int , coercible = [(ty .Any , ty .Any )])(1.0 ) is True
357
+ )
358
+
359
+
322
360
def test_type_coercion_basic14 ():
323
361
assert TypeParser (ty .Sequence , coercible = [(ty .Any , ty .Any )])((1 , 2 , 3 )) == (
324
362
1 ,
@@ -404,6 +442,12 @@ def test_type_coercion_fail2():
404
442
TypeParser (ty .Union [Path , File ], coercible = [(ty .Any , ty .Any )])(1 )
405
443
406
444
445
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
446
+ def test_type_coercion_fail2a ():
447
+ with pytest .raises (TypeError , match = "to any of the union types" ):
448
+ TypeParser (Path | File , coercible = [(ty .Any , ty .Any )])(1 )
449
+
450
+
407
451
def test_type_coercion_fail3 ():
408
452
with pytest .raises (TypeError , match = "doesn't match any of the explicit inclusion" ):
409
453
TypeParser (ty .Sequence , coercible = [(ty .Sequence , ty .Sequence )])(
@@ -446,7 +490,7 @@ def f(x: ty.List[File], y: ty.Dict[str, ty.List[File]]):
446
490
TypeParser (ty .List [str ])(task .lzout .a ) # pylint: disable=no-member
447
491
with pytest .raises (
448
492
TypeError ,
449
- match = "Cannot coerce <class 'fileformats.generic.File'> into <class 'int'>" ,
493
+ match = "Cannot coerce <class 'fileformats\ .generic.*\ .File'> into <class 'int'>" ,
450
494
):
451
495
TypeParser (ty .List [int ])(task .lzout .a ) # pylint: disable=no-member
452
496
@@ -469,6 +513,27 @@ def test_matches_type_union():
469
513
assert not TypeParser .matches_type (ty .Union [int , bool , str ], ty .Union [int , bool ])
470
514
471
515
516
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
517
+ def test_matches_type_union_a ():
518
+ assert TypeParser .matches_type (int | bool | str , int | bool | str )
519
+ assert TypeParser .matches_type (int | bool , int | bool | str )
520
+ assert not TypeParser .matches_type (int | bool | str , int | bool )
521
+
522
+
523
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
524
+ def test_matches_type_union_b ():
525
+ assert TypeParser .matches_type (int | bool | str , ty .Union [int , bool , str ])
526
+ assert TypeParser .matches_type (int | bool , ty .Union [int , bool , str ])
527
+ assert not TypeParser .matches_type (int | bool | str , ty .Union [int , bool ])
528
+
529
+
530
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
531
+ def test_matches_type_union_c ():
532
+ assert TypeParser .matches_type (ty .Union [int , bool , str ], int | bool | str )
533
+ assert TypeParser .matches_type (ty .Union [int , bool ], int | bool | str )
534
+ assert not TypeParser .matches_type (ty .Union [int , bool , str ], int | bool )
535
+
536
+
472
537
def test_matches_type_dict ():
473
538
COERCIBLE = [(str , Path ), (Path , str ), (int , float )]
474
539
@@ -713,18 +778,61 @@ def test_union_is_subclass1():
713
778
assert TypeParser .is_subclass (ty .Union [Json , Yaml ], ty .Union [Json , Yaml , Xml ])
714
779
715
780
781
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
782
+ def test_union_is_subclass1a ():
783
+ assert TypeParser .is_subclass (Json | Yaml , Json | Yaml | Xml )
784
+
785
+
786
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
787
+ def test_union_is_subclass1b ():
788
+ assert TypeParser .is_subclass (Json | Yaml , ty .Union [Json , Yaml , Xml ])
789
+
790
+
791
+ ## Up to here!
792
+
793
+
794
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
795
+ def test_union_is_subclass1c ():
796
+ assert TypeParser .is_subclass (ty .Union [Json , Yaml ], Json | Yaml | Xml )
797
+
798
+
716
799
def test_union_is_subclass2 ():
717
800
assert not TypeParser .is_subclass (ty .Union [Json , Yaml , Xml ], ty .Union [Json , Yaml ])
718
801
719
802
803
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
804
+ def test_union_is_subclass2a ():
805
+ assert not TypeParser .is_subclass (Json | Yaml | Xml , Json | Yaml )
806
+
807
+
808
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
809
+ def test_union_is_subclass2b ():
810
+ assert not TypeParser .is_subclass (ty .Union [Json , Yaml , Xml ], Json | Yaml )
811
+
812
+
813
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
814
+ def test_union_is_subclass2c ():
815
+ assert not TypeParser .is_subclass (Json | Yaml | Xml , ty .Union [Json , Yaml ])
816
+
817
+
720
818
def test_union_is_subclass3 ():
721
819
assert TypeParser .is_subclass (Json , ty .Union [Json , Yaml ])
722
820
723
821
822
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
823
+ def test_union_is_subclass3a ():
824
+ assert TypeParser .is_subclass (Json , Json | Yaml )
825
+
826
+
724
827
def test_union_is_subclass4 ():
725
828
assert not TypeParser .is_subclass (ty .Union [Json , Yaml ], Json )
726
829
727
830
831
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
832
+ def test_union_is_subclass4a ():
833
+ assert not TypeParser .is_subclass (Json | Yaml , Json )
834
+
835
+
728
836
def test_generic_is_subclass1 ():
729
837
assert TypeParser .is_subclass (ty .List [int ], list )
730
838
@@ -737,6 +845,56 @@ def test_generic_is_subclass3():
737
845
assert not TypeParser .is_subclass (ty .List [float ], ty .List [int ])
738
846
739
847
848
+ def test_none_is_subclass1 ():
849
+ assert TypeParser .is_subclass (None , ty .Union [int , None ])
850
+
851
+
852
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
853
+ def test_none_is_subclass1a ():
854
+ assert TypeParser .is_subclass (None , int | None )
855
+
856
+
857
+ def test_none_is_subclass2 ():
858
+ assert not TypeParser .is_subclass (None , ty .Union [int , float ])
859
+
860
+
861
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
862
+ def test_none_is_subclass2a ():
863
+ assert not TypeParser .is_subclass (None , int | float )
864
+
865
+
866
+ def test_none_is_subclass3 ():
867
+ assert TypeParser .is_subclass (ty .Tuple [int , None ], ty .Tuple [int , None ])
868
+
869
+
870
+ def test_none_is_subclass4 ():
871
+ assert TypeParser .is_subclass (None , None )
872
+
873
+
874
+ def test_none_is_subclass5 ():
875
+ assert not TypeParser .is_subclass (None , int )
876
+
877
+
878
+ def test_none_is_subclass6 ():
879
+ assert not TypeParser .is_subclass (int , None )
880
+
881
+
882
+ def test_none_is_subclass7 ():
883
+ assert TypeParser .is_subclass (None , type (None ))
884
+
885
+
886
+ def test_none_is_subclass8 ():
887
+ assert TypeParser .is_subclass (type (None ), None )
888
+
889
+
890
+ def test_none_is_subclass9 ():
891
+ assert TypeParser .is_subclass (type (None ), type (None ))
892
+
893
+
894
+ def test_none_is_subclass10 ():
895
+ assert TypeParser .is_subclass (type (None ), type (None ))
896
+
897
+
740
898
@pytest .mark .skipif (
741
899
sys .version_info < (3 , 9 ), reason = "Cannot subscript tuple in < Py3.9"
742
900
)
@@ -780,3 +938,46 @@ def test_type_is_instance3():
780
938
781
939
def test_type_is_instance4 ():
782
940
assert TypeParser .is_instance (Json , type )
941
+
942
+
943
+ def test_type_is_instance5 ():
944
+ assert TypeParser .is_instance (None , None )
945
+
946
+
947
+ def test_type_is_instance6 ():
948
+ assert TypeParser .is_instance (None , type (None ))
949
+
950
+
951
+ def test_type_is_instance7 ():
952
+ assert not TypeParser .is_instance (None , int )
953
+
954
+
955
+ def test_type_is_instance8 ():
956
+ assert not TypeParser .is_instance (1 , None )
957
+
958
+
959
+ def test_type_is_instance9 ():
960
+ assert TypeParser .is_instance (None , ty .Union [int , None ])
961
+
962
+
963
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
964
+ def test_type_is_instance9a ():
965
+ assert TypeParser .is_instance (None , int | None )
966
+
967
+
968
+ def test_type_is_instance10 ():
969
+ assert TypeParser .is_instance (1 , ty .Union [int , None ])
970
+
971
+
972
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
973
+ def test_type_is_instance10a ():
974
+ assert TypeParser .is_instance (1 , int | None )
975
+
976
+
977
+ def test_type_is_instance11 ():
978
+ assert not TypeParser .is_instance (None , ty .Union [int , str ])
979
+
980
+
981
+ @pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "No UnionType < Py3.10" )
982
+ def test_type_is_instance11a ():
983
+ assert not TypeParser .is_instance (None , int | str )
0 commit comments