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

FlyteFilePath works with os.pathlike / str objects #232

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion flytekit/annotated/type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,16 @@ def to_literal(
python_type: Type[flyte_typing.FlyteFilePath],
expected: LiteralType,
) -> Literal:
remote_path = python_val.remote_path if python_val.remote_path else ctx.file_access.get_random_remote_path()
remote_path = ""
if isinstance(python_val, flyte_typing.FlyteFilePath):
remote_path = python_val.remote_path if python_val.remote_path else ""
else:
if not (isinstance(python_val, os.PathLike) or isinstance(python_val, str)):
raise AssertionError(f"Expected FlyteFilePath or os.PathLike object, received {type(python_val)}")

if remote_path == "":
remote_path = ctx.file_access.get_random_remote_path()

ctx.file_access.put_data(f"{python_val}", remote_path, is_multipart=False)
meta = BlobMetadata(type=self._blob_type(format=FlyteFilePathTransformer.get_format(python_type)))
return Literal(scalar=Scalar(blob=Blob(metadata=meta, uri=remote_path)))
Expand Down
5 changes: 3 additions & 2 deletions tests/flytekit/unit/annotated/test_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,10 @@ def t1(a: str) -> tuple:
def test_file_type_in_workflow_with_bad_format():
@task
def t1() -> flytekit_typing.FlyteFilePath["txt"]:
with open("/tmp/flytekit_test", "w") as fh:
fname = "/tmp/flytekit_test"
with open(fname, "w") as fh:
fh.write("Hello World\n")
return flytekit_typing.FlyteFilePath["txt"]("/tmp/flytekit_test")
return fname

@workflow
def my_wf() -> flytekit_typing.FlyteFilePath["txt"]:
Expand Down