Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Use `@overload`

0div608ef45fa517
diff --git a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py
index 8d5eb1815..4d5037684 100644
--- a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py
@@ -26,6 +26,6 @@ class EntryInfo:
WriteData = Union[str, bytes, IO]
@dataclass
-class FileWriteData:
+class WriteEntry:
path: str
data: WriteData
diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
index 401f00e01..b7bfca494 100644
--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
@@ -1,6 +1,6 @@
from io import TextIOBase
from typing import Iterator, List, Literal, Optional, overload
-from e2b.sandbox.filesystem.filesystem import WriteData, FileWriteData
+from e2b.sandbox.filesystem.filesystem import WriteData, WriteEntry
import e2b_connect
import httpcore
@@ -92,12 +92,32 @@ class Filesystem:
elif format == "stream":
return r.iter_bytes()
+ @overload
def write(
self,
- path: str | None,
- data: WriteData | List[FileWriteData],
+ path: str,
+ data: WriteData,
user: Username = "user",
request_timeout: Optional[float] = None,
+ ) -> EntryInfo:
+ """Write to file"""
+
+ @overload
+ def write(
+ self,
+ files: List[WriteEntry],
+ user: Optional[Username] = "user",
+ path: Optional[str] = None,
+ request_timeout: Optional[float] = None,
+ ) -> List[EntryInfo]:
+ """Write multiple files"""
+
+ def write(
+ self,
+ path_or_files: str | List[WriteEntry],
+ data_or_user: WriteData | Username = "user",
+ user_or_request_timeout: Optional[float | Username] = None,
+ request_timeout_or_none: Optional[float] = None
) -> EntryInfo | List[EntryInfo]:
"""Write to file(s)
When writing to a file that doesn't exist, the file will get created.
@@ -105,16 +125,16 @@ class Filesystem:
When writing to a file that's in a directory that doesn't exist, you'll get an error.
"""
- write_files = []
- if path is None:
- if not isinstance(data, list):
- raise Exception("Expected data to be a list of FileWriteData")
- write_files = data
- else:
- if isinstance(data, list):
+ path, write_files, user, request_timeout, = None, [], "user", None
+ if isinstance(path_or_files, str):
+ if isinstance(data_or_user, list):
raise Exception("Cannot specify path with array of files")
- write_files = [{"path": path, "data": data}]
-
+ path, write_files, user, request_timeout = \
+ path_or_files, [{"path": path_or_files, "data": data_or_user}], user_or_request_timeout or "user", request_timeout_or_none
+ else:
+ path, write_files, user, request_timeout = \
+ None, path_or_files, data_or_user, user_or_request_timeout
+
if len(write_files) == 0:
raise Exception("Need at least one file to write")
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py
index 83addec27..3ccbd41e0 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py
@@ -16,7 +16,7 @@ def test_write_file(sandbox):
def test_write_multiple_files(sandbox):
# Attempt to write with empty files array
try:
- sandbox.files.write(None, [])
+ sandbox.files.write([])
except Exception as e:
assert "Need at least one file to write" in str(e)
@@ -27,7 +27,7 @@ def test_write_multiple_files(sandbox):
assert "Cannot specify path with array of files" in str(e)
# Attempt to write with one file in array
- info = sandbox.files.write(None, [{ "path": "one_test_file.txt", "data": "This is a test file." }])
+ info = sandbox.files.write([{ "path": "one_test_file.txt", "data": "This is a test file." }])
assert isinstance(info, list)
assert len(info) == 1
info = info[0]
@@ -46,7 +46,7 @@ def test_write_multiple_files(sandbox):
content = f"This is a test file {i}."
files.append({"path": filename, "data": content})
- infos = sandbox.files.write(None, files)
+ infos = sandbox.files.write(files)
assert isinstance(infos, list)
assert len(infos) == len(files)
for i, info in enumerate(infos):