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

add class methods, unit tests for flytefile and flytedirectory #2852

Merged
merged 1 commit into from
Oct 23, 2024
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
24 changes: 24 additions & 0 deletions flytekit/types/directory/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@
remote_path = ctx.file_access.generate_new_custom_path(alt=alt, stem=stem)
return cls(path=remote_path)

@classmethod
def new(cls, dirname: str | os.PathLike) -> FlyteFile:
"""
Create a new FlyteDirectory object in current Flyte working directory.
"""

if os.path.isabs(dirname):
raise ValueError("Path should be relative.")

Check warning on line 216 in flytekit/types/directory/types.py

View check run for this annotation

Codecov / codecov/patch

flytekit/types/directory/types.py#L216

Added line #L216 was not covered by tests

ctx = FlyteContextManager.current_context()

path = os.path.join(ctx.user_space_params.working_directory, dirname)

os.makedirs(path, exist_ok=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the exist_ok=False.


return cls(path=path)

def __class_getitem__(cls, item: typing.Union[typing.Type, str]) -> typing.Type[FlyteDirectory]:
if item is None:
return cls
Expand Down Expand Up @@ -406,6 +423,13 @@
def __str__(self):
return str(self.path)

def __truediv__(self, other: str | os.PathLike) -> Path:
"""
This is a convenience method to allow for easy concatenation of paths.
"""

return Path(self.path) / other


class FlyteDirToMultipartBlobTransformer(AsyncTypeTransformer[FlyteDirectory]):
"""
Expand Down
15 changes: 15 additions & 0 deletions flytekit/types/file/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@
t = FlyteFilePathTransformer()
return t.to_python_value(ctx, lit, cls)

@classmethod
def new(cls, filename: str | os.PathLike) -> FlyteFile:
"""
Create a new FlyteFile object in the current Flyte working directory
"""

if os.path.isabs(filename):
raise ValueError("Path should be relative.")

Check warning on line 224 in flytekit/types/file/file.py

View check run for this annotation

Codecov / codecov/patch

flytekit/types/file/file.py#L224

Added line #L224 was not covered by tests

ctx = FlyteContextManager.current_context()

path = os.path.join(ctx.user_space_params.working_directory, filename)

return cls(path=path)

def __class_getitem__(cls, item: typing.Union[str, typing.Type]) -> typing.Type[FlyteFile]:
from flytekit.types.file import FileExt

Expand Down
12 changes: 11 additions & 1 deletion tests/flytekit/unit/types/directory/test_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mock

from flytekit import FlyteContext
from flytekit import FlyteContext, FlyteContextManager
from flytekit.types.directory import FlyteDirectory
from flytekit.types.file import FlyteFile

Expand All @@ -27,6 +27,16 @@ def test_new_remote_dir_alt():
assert "my-alt-bucket" in ff.path
assert "my-stem" in ff.path

def test_new_auto_new_dir():
fd = FlyteDirectory.new("my_dir")
assert FlyteContextManager.current_context().user_space_params.working_directory in fd.path

def test_add_path_to_dir():
fd = FlyteDirectory.new("my_other_dir")
cwd = FlyteContextManager.current_context().user_space_params.working_directory
assert cwd in str(fd / "myfile.txt")


@mock.patch("flytekit.types.directory.types.os.name", "nt")
def test_sep_nt():
fd = FlyteDirectory(path="file://mypath")
Expand Down
5 changes: 5 additions & 0 deletions tests/flytekit/unit/types/file/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ def test_new_remote_alt():
ff = FlyteFile.new_remote_file(alt="my-alt-prefix", name="my-file.txt")
assert "my-alt-prefix" in ff.path
assert "my-file.txt" in ff.path

def test_new_auto_file():
ff = FlyteFile.new("my-file.txt")
cwd = FlyteContextManager.current_context().user_space_params.working_directory
assert cwd in ff.path
Loading