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

Fix bug in FlyteDirectory.listdir on local files #2926

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions flytekit/types/directory/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,11 @@ def listdir(cls, directory: FlyteDirectory) -> typing.List[typing.Union[FlyteDir
file_access = FlyteContextManager.current_context().file_access
if not file_access.is_remote(final_path):
for p in os.listdir(final_path):
if os.path.isfile(os.path.join(final_path, p)):
paths.append(FlyteFile(p))
joined_path = os.path.join(final_path, p)
if os.path.isfile(joined_path):
paths.append(FlyteFile(joined_path))
else:
paths.append(FlyteDirectory(p))
paths.append(FlyteDirectory(joined_path))
return paths

def create_downloader(_remote_path: str, _local_path: str, is_multipart: bool):
Expand Down
31 changes: 31 additions & 0 deletions tests/flytekit/unit/types/directory/test_listdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import tempfile
from pathlib import Path

from flytekit import FlyteDirectory, FlyteFile, map_task, task, workflow

def test_listdir():
@task
def setup() -> FlyteDirectory:
tmpdir = Path(tempfile.mkdtemp())
(tmpdir / "file.txt").write_text("Hello, World!")
return FlyteDirectory(tmpdir)


@task
def read_file(file: FlyteFile) -> str:
with open(file, "r") as f:
return f.read()


@task
def list_dir(dir: FlyteDirectory) -> list[FlyteFile]:
return FlyteDirectory.listdir(dir)


@workflow
def wf() -> list[str]:
tmpdir = setup()
files = list_dir(dir=tmpdir)
return map_task(read_file)(file=files)

assert wf() == ["Hello, World!"]
Loading