From eaa5cfe5cae44e67c0b8af8973ecf02a41bb5cac Mon Sep 17 00:00:00 2001 From: Yee Hing Tong Date: Thu, 10 Oct 2024 18:39:16 -0700 Subject: [PATCH] Instance generic empty case (#2802) --- flytekit/core/type_engine.py | 8 ++--- flytekit/tools/script_mode.py | 1 + .../unit/cli/pyflyte/test_script_mode.py | 2 +- tests/flytekit/unit/core/test_type_engine.py | 29 +++++++++++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/flytekit/core/type_engine.py b/flytekit/core/type_engine.py index 2d9d21f0ff..d7a6aca75d 100644 --- a/flytekit/core/type_engine.py +++ b/flytekit/core/type_engine.py @@ -159,18 +159,14 @@ def isinstance_generic(self, obj, generic_alias): if origin in {list, tuple, set}: for item in obj: self.assert_type(args[0], item) - return - raise TypeTransformerFailedError(f"Not all items in '{obj}' are of type {args[0]}") + return if origin is dict: key_type, value_type = args for k, v in obj.items(): self.assert_type(key_type, k) self.assert_type(value_type, v) - return - raise TypeTransformerFailedError(f"Not all values in '{obj}' are of type {value_type}") - - return + return def assert_type(self, t: Type[T], v: T): if sys.version_info >= (3, 10): diff --git a/flytekit/tools/script_mode.py b/flytekit/tools/script_mode.py index 7188b5b90d..fa8634361d 100644 --- a/flytekit/tools/script_mode.py +++ b/flytekit/tools/script_mode.py @@ -118,6 +118,7 @@ def ls_files( else: all_files = list_all_files(source_path, deref_symlinks, ignore_group) + all_files.sort() hasher = hashlib.md5() for abspath in all_files: relpath = os.path.relpath(abspath, source_path) diff --git a/tests/flytekit/unit/cli/pyflyte/test_script_mode.py b/tests/flytekit/unit/cli/pyflyte/test_script_mode.py index 74d8aeab73..c588eb36f8 100644 --- a/tests/flytekit/unit/cli/pyflyte/test_script_mode.py +++ b/tests/flytekit/unit/cli/pyflyte/test_script_mode.py @@ -39,7 +39,7 @@ def test_list_dir(dummy_dir_structure): files, d = ls_files(str(dummy_dir_structure), CopyFileDetection.ALL) assert len(files) == 5 if os.name != "nt": - assert d == "c092f1b85f7c6b2a71881a946c00a855" + assert d == "b6907fd823a45e26c780a4ba62111243" def test_list_filtered_on_modules(dummy_dir_structure): diff --git a/tests/flytekit/unit/core/test_type_engine.py b/tests/flytekit/unit/core/test_type_engine.py index a8e4cd31a8..e6b4acd485 100644 --- a/tests/flytekit/unit/core/test_type_engine.py +++ b/tests/flytekit/unit/core/test_type_engine.py @@ -3466,3 +3466,32 @@ def test_option_list_with_pipe_2(): with pytest.raises(TypeTransformerFailedError): TypeEngine.to_literal(ctx, [[{"a": "one"}], None, [{"b": 3}]], pt, lt) + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP604 requires >=3.10, 585 requires >=3.9") +def test_generic_errors_and_empty(): + # Test dictionaries + pt = dict[str, str] + lt = TypeEngine.to_literal_type(pt) + + ctx = FlyteContextManager.current_context() + lit = TypeEngine.to_literal(ctx, {}, pt, lt) + lit = TypeEngine.to_literal(ctx, {"a": "b"}, pt, lt) + + with pytest.raises(TypeTransformerFailedError): + TypeEngine.to_literal(ctx, {"a": 3}, pt, lt) + + with pytest.raises(TypeTransformerFailedError): + TypeEngine.to_literal(ctx, {3: "a"}, pt, lt) + + # Test lists + pt = list[str] + lt = TypeEngine.to_literal_type(pt) + lit = TypeEngine.to_literal(ctx, [], pt, lt) + lit = TypeEngine.to_literal(ctx, ["a"], pt, lt) + + with pytest.raises(TypeTransformerFailedError): + TypeEngine.to_literal(ctx, {"a": 3}, pt, lt) + + with pytest.raises(TypeTransformerFailedError): + TypeEngine.to_literal(ctx, [3], pt, lt)