Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance generic empty case #2802

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions flytekit/core/type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
29 changes: 29 additions & 0 deletions tests/flytekit/unit/core/test_type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading