From ad28ae57f9423601991a7491e1ffe175476d2c54 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Thu, 30 May 2024 11:45:51 +0930 Subject: [PATCH] parametrized a few ranges of unittests --- pydra/utils/tests/test_typing.py | 234 ++++++++++++------------------- 1 file changed, 89 insertions(+), 145 deletions(-) diff --git a/pydra/utils/tests/test_typing.py b/pydra/utils/tests/test_typing.py index ec6ed7ca66..f83eedbd8c 100644 --- a/pydra/utils/tests/test_typing.py +++ b/pydra/utils/tests/test_typing.py @@ -75,6 +75,9 @@ def test_type_check_basic7(): def test_type_check_basic8(): TypeParser(Path, coercible=[(PathTypes, PathTypes)])(lz(str)) + + +def test_type_check_basic8a(): TypeParser(str, coercible=[(PathTypes, PathTypes)])(lz(Path)) @@ -94,6 +97,9 @@ def test_type_check_basic10(): def test_type_check_basic11(): TypeParser(str, coercible=[(PathTypes, PathTypes)])(lz(File)) + + +def test_type_check_basic11a(): TypeParser(File, coercible=[(PathTypes, PathTypes)])(lz(str)) @@ -655,12 +661,15 @@ def test_contains_type_in_dict(): def test_any_union(): """Check that the superclass auto-cast matches if any of the union args match instead of all""" + # The Json type within the Union matches File as it is a subclass as `match_any_of_union` + # is set to True. Otherwise, all types within the Union would have to match TypeParser(File, match_any_of_union=True).check_type(ty.Union[ty.List[File], Json]) def test_union_superclass_check_type(): """Check that the superclass auto-cast matches if any of the union args match instead of all""" + # In this case, File matches Json due to the `superclass_auto_cast=True` flag being set TypeParser(ty.Union[ty.List[File], Json], superclass_auto_cast=True)(lz(File)) @@ -818,20 +827,42 @@ def test_typing_cast(tmp_path, specific_task, other_specific_task): assert out_file.header.parent != in_file.header.parent -def test_type_is_subclass1(): - assert TypeParser.is_subclass(ty.Type[File], type) - - -def test_type_is_subclass2(): - assert not TypeParser.is_subclass(ty.Type[File], ty.Type[Json]) - - -def test_type_is_subclass3(): - assert TypeParser.is_subclass(ty.Type[Json], ty.Type[File]) +@pytest.mark.parametrize( + ("sub", "super"), + [ + (ty.Type[File], type), + (ty.Type[Json], ty.Type[File]), + (ty.Union[Json, Yaml], ty.Union[Json, Yaml, Xml]), + (Json, ty.Union[Json, Yaml]), + (ty.List[int], list), + (None, ty.Union[int, None]), + (ty.Tuple[int, None], ty.Tuple[int, None]), + (None, None), + (None, type(None)), + (type(None), None), + (type(None), type(None)), + (type(None), type(None)), + ], +) +def test_subclass(sub, super): + assert TypeParser.is_subclass(sub, super) -def test_union_is_subclass1(): - assert TypeParser.is_subclass(ty.Union[Json, Yaml], ty.Union[Json, Yaml, Xml]) +@pytest.mark.parametrize( + ("sub", "super"), + [ + (ty.Type[File], ty.Type[Json]), + (ty.Union[Json, Yaml, Xml], ty.Union[Json, Yaml]), + (ty.Union[Json, Yaml], Json), + (list, ty.List[int]), + (ty.List[float], ty.List[int]), + (None, ty.Union[int, float]), + (None, int), + (int, None), + ], +) +def test_not_subclass(sub, super): + assert not TypeParser.is_subclass(sub, super) @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") @@ -844,18 +875,11 @@ def test_union_is_subclass1b(): assert TypeParser.is_subclass(Json | Yaml, ty.Union[Json, Yaml, Xml]) -## Up to here! - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_union_is_subclass1c(): assert TypeParser.is_subclass(ty.Union[Json, Yaml], Json | Yaml | Xml) -def test_union_is_subclass2(): - assert not TypeParser.is_subclass(ty.Union[Json, Yaml, Xml], ty.Union[Json, Yaml]) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_union_is_subclass2a(): assert not TypeParser.is_subclass(Json | Yaml | Xml, Json | Yaml) @@ -871,86 +895,26 @@ def test_union_is_subclass2c(): assert not TypeParser.is_subclass(Json | Yaml | Xml, ty.Union[Json, Yaml]) -def test_union_is_subclass3(): - assert TypeParser.is_subclass(Json, ty.Union[Json, Yaml]) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_union_is_subclass3a(): assert TypeParser.is_subclass(Json, Json | Yaml) -def test_union_is_subclass4(): - assert not TypeParser.is_subclass(ty.Union[Json, Yaml], Json) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_union_is_subclass4a(): assert not TypeParser.is_subclass(Json | Yaml, Json) -def test_generic_is_subclass1(): - assert TypeParser.is_subclass(ty.List[int], list) - - -def test_generic_is_subclass2(): - assert not TypeParser.is_subclass(list, ty.List[int]) - - -def test_generic_is_subclass3(): - assert not TypeParser.is_subclass(ty.List[float], ty.List[int]) - - -def test_none_is_subclass1(): - assert TypeParser.is_subclass(None, ty.Union[int, None]) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_none_is_subclass1a(): assert TypeParser.is_subclass(None, int | None) -def test_none_is_subclass2(): - assert not TypeParser.is_subclass(None, ty.Union[int, float]) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_none_is_subclass2a(): assert not TypeParser.is_subclass(None, int | float) -def test_none_is_subclass3(): - assert TypeParser.is_subclass(ty.Tuple[int, None], ty.Tuple[int, None]) - - -def test_none_is_subclass4(): - assert TypeParser.is_subclass(None, None) - - -def test_none_is_subclass5(): - assert not TypeParser.is_subclass(None, int) - - -def test_none_is_subclass6(): - assert not TypeParser.is_subclass(int, None) - - -def test_none_is_subclass7(): - assert TypeParser.is_subclass(None, type(None)) - - -def test_none_is_subclass8(): - assert TypeParser.is_subclass(type(None), None) - - -def test_none_is_subclass9(): - assert TypeParser.is_subclass(type(None), type(None)) - - -def test_none_is_subclass10(): - assert TypeParser.is_subclass(type(None), type(None)) - - @pytest.mark.skipif( sys.version_info < (3, 9), reason="Cannot subscript tuple in < Py3.9" ) @@ -980,40 +944,33 @@ class B(A): assert not TypeParser.is_subclass(MyTuple[B], ty.Tuple[A, int]) -def test_type_is_instance1(): - assert TypeParser.is_instance(File, ty.Type[File]) - - -def test_type_is_instance2(): - assert not TypeParser.is_instance(File, ty.Type[Json]) - - -def test_type_is_instance3(): - assert TypeParser.is_instance(Json, ty.Type[File]) - - -def test_type_is_instance4(): - assert TypeParser.is_instance(Json, type) - - -def test_type_is_instance5(): - assert TypeParser.is_instance(None, None) - - -def test_type_is_instance6(): - assert TypeParser.is_instance(None, type(None)) - - -def test_type_is_instance7(): - assert not TypeParser.is_instance(None, int) - - -def test_type_is_instance8(): - assert not TypeParser.is_instance(1, None) +@pytest.mark.parametrize( + ("tp", "obj"), + [ + (File, ty.Type[File]), + (Json, ty.Type[File]), + (Json, type), + (None, None), + (None, type(None)), + (None, ty.Union[int, None]), + (1, ty.Union[int, None]), + ], +) +def test_type_is_instance(tp, obj): + assert TypeParser.is_instance(tp, obj) -def test_type_is_instance9(): - assert TypeParser.is_instance(None, ty.Union[int, None]) +@pytest.mark.parametrize( + ("tp", "obj"), + [ + (File, ty.Type[Json]), + (None, int), + (1, None), + (None, ty.Union[int, str]), + ], +) +def test_type_is_not_instance(tp, obj): + assert not TypeParser.is_instance(tp, obj) @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") @@ -1021,19 +978,11 @@ def test_type_is_instance9a(): assert TypeParser.is_instance(None, int | None) -def test_type_is_instance10(): - assert TypeParser.is_instance(1, ty.Union[int, None]) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_type_is_instance10a(): assert TypeParser.is_instance(1, int | None) -def test_type_is_instance11(): - assert not TypeParser.is_instance(None, ty.Union[int, str]) - - @pytest.mark.skipif(sys.version_info < (3, 10), reason="No UnionType < Py3.10") def test_type_is_instance11a(): assert not TypeParser.is_instance(None, int | str) @@ -1059,32 +1008,27 @@ def test_multi_input_obj_coerce4a(): TypeParser(MultiInputObj[ty.Union[int, ty.List[str]]])([[1]]) -def test_multi_input_obj_check_type1(): - TypeParser(MultiInputObj[str])(lz(str)) - - -def test_multi_input_obj_check_type2(): - TypeParser(MultiInputObj[str])(lz(ty.List[str])) - - -def test_multi_input_obj_check_type3(): - TypeParser(MultiInputObj[ty.List[str]])(lz(ty.List[str])) - - -def test_multi_input_obj_check_type3a(): - TypeParser(MultiInputObj[ty.Union[int, ty.List[str]]])(lz(ty.List[str])) - - -def test_multi_input_obj_check_type3b(): - TypeParser(MultiInputObj[ty.Union[int, ty.List[str]]])(lz(ty.List[ty.List[str]])) - - -def test_multi_input_obj_check_type4(): - TypeParser(MultiInputObj[ty.Union[int, ty.List[str]]])(lz(ty.List[int])) +@pytest.mark.parametrize( + ("reference", "to_be_checked"), + [ + (MultiInputObj[str], str), + (MultiInputObj[str], ty.List[str]), + (MultiInputObj[ty.List[str]], ty.List[str]), + (MultiInputObj[ty.Union[int, ty.List[str]]], ty.List[str]), + (MultiInputObj[ty.Union[int, ty.List[str]]], ty.List[ty.List[str]]), + (MultiInputObj[ty.Union[int, ty.List[str]]], ty.List[int]), + ], +) +def test_multi_input_obj_check_type(reference, to_be_checked): + TypeParser(reference)(lz(to_be_checked)) -def test_multi_input_obj_check_type4a(): +@pytest.mark.parametrize( + ("reference", "to_be_checked"), + [ + (MultiInputObj[ty.Union[int, ty.List[str]]], ty.List[ty.List[int]]), + ], +) +def test_multi_input_obj_check_type_fail(reference, to_be_checked): with pytest.raises(TypeError): - TypeParser(MultiInputObj[ty.Union[int, ty.List[str]]])( - lz(ty.List[ty.List[int]]) - ) + TypeParser(reference)(lz(to_be_checked))