Skip to content

Commit

Permalink
fixing mypy issues in client files and lib/hash.py
Browse files Browse the repository at this point in the history
  • Loading branch information
khoaguin committed Dec 16, 2024
1 parent 0feac95 commit 350ec11
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions syftbox/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ClientBase:
def __init__(self, conn: httpx.Client):
self.conn = conn

def raise_for_status(self, response: httpx.Response):
def raise_for_status(self, response: httpx.Response) -> None:
endpoint = response.request.url.path
if response.status_code == 401:
raise SyftAuthenticationError()
Expand All @@ -93,7 +93,7 @@ def raise_for_status(self, response: httpx.Response):
raise SyftServerError(f"[{endpoint}] Server returned {response.status_code}: {response.text}")

@staticmethod
def _make_headers(config: SyftClientConfig) -> dict[str, str]:
def _make_headers(config: SyftClientConfig) -> dict:
headers = {
**SYFTBOX_HEADERS,
HEADER_SYFTBOX_USER: config.email,
Expand All @@ -106,7 +106,7 @@ def _make_headers(config: SyftClientConfig) -> dict[str, str]:
return headers

@classmethod
def from_config(cls, config: SyftClientConfig):
def from_config(cls, config: SyftClientConfig) -> "ClientBase":
conn = httpx.Client(
base_url=str(config.server_url),
follow_redirects=True,
Expand Down
6 changes: 3 additions & 3 deletions syftbox/client/plugins/sync/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def download_all_missing(self, datasite_states: list[DatasiteState]) -> None:

logger.info(f"Downloading {len(missing_files)} files in batch")
received_files = create_local_batch(self.context, missing_files)
for path in received_files:
state = self.get_current_local_metadata(Path(path))
for file_path in received_files:
state = self.get_current_local_metadata(Path(file_path))
self.local_state.insert_synced_file(
path=Path(path),
path=Path(file_path),
state=state,
action=SyncActionType.CREATE_LOCAL,
)
Expand Down
16 changes: 11 additions & 5 deletions syftbox/client/plugins/sync/sync_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def process_rejection(self, context: SyftBoxContextInterface, reason: Optional[s
class ModifyLocalAction(SyncAction):
action_type = SyncActionType.MODIFY_LOCAL

def execute(self, context: SyftBoxContextInterface):
def execute(self, context: SyftBoxContextInterface) -> None:
if self.local_metadata is None:
raise ValueError("Local metadata is required for modify local action")
# Use rsync to update the local file with the remote changes
diff = context.client.sync.get_diff(self.path, self.local_metadata.signature)

Expand Down Expand Up @@ -212,7 +214,7 @@ def process_rejection(self, context: SyftBoxContextInterface, reason: Optional[s
class DeleteLocalAction(SyncAction):
action_type = SyncActionType.DELETE_LOCAL

def execute(self, context: SyftBoxContextInterface):
def execute(self, context: SyftBoxContextInterface) -> None:
abs_path = context.workspace.datasites / self.path
abs_path.unlink()
self.status = SyncStatus.SYNCED
Expand All @@ -225,7 +227,7 @@ def process_rejection(self, context: SyftBoxContextInterface, reason: Optional[s
class CreateRemoteAction(SyncAction):
action_type = SyncActionType.CREATE_REMOTE

def execute(self, context: SyftBoxContextInterface):
def execute(self, context: SyftBoxContextInterface) -> None:
abs_path = context.workspace.datasites / self.path
data = abs_path.read_bytes()
context.client.sync.create(self.path, data)
Expand All @@ -243,10 +245,14 @@ def process_rejection(self, context: SyftBoxContextInterface, reason: Optional[s
class ModifyRemoteAction(SyncAction):
action_type = SyncActionType.MODIFY_REMOTE

def execute(self, context: SyftBoxContextInterface):
def execute(self, context: SyftBoxContextInterface) -> None:
abs_path = context.workspace.datasites / self.path
local_data = abs_path.read_bytes()
if self.remote_metadata is None:
raise ValueError("Remote metadata is required for modify remote action")
diff = py_fast_rsync.diff(self.remote_metadata.signature_bytes, local_data)
if self.local_metadata is None:
raise ValueError("Local metadata is required for modify remote action")
context.client.sync.apply_diff(
relative_path=self.path,
diff=diff,
Expand Down Expand Up @@ -274,7 +280,7 @@ def process_rejection(self, context: SyftBoxContextInterface, reason: Optional[s
class DeleteRemoteAction(SyncAction):
action_type = SyncActionType.DELETE_REMOTE

def execute(self, context: SyftBoxContextInterface):
def execute(self, context: SyftBoxContextInterface) -> None:
context.client.sync.delete(self.path)
self.status = SyncStatus.SYNCED

Expand Down
6 changes: 3 additions & 3 deletions syftbox/client/server_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
from pathlib import Path
from typing import Union
from typing import Any, Union

import httpx

Expand All @@ -27,7 +27,7 @@ def info(self) -> dict:
self.raise_for_status(response)
return response.json()

def log_analytics_event(self, event_name: str, **kwargs) -> None:
def log_analytics_event(self, event_name: str, **kwargs: dict) -> None:
"""Log an event to the server"""
event_data = {
"event_name": event_name,
Expand All @@ -39,7 +39,7 @@ def log_analytics_event(self, event_name: str, **kwargs) -> None:


class AuthClient(ClientBase):
def whoami(self):
def whoami(self) -> Any:
response = self.conn.post("/auth/whoami")
self.raise_for_status(response)
return response.json()
Expand Down
2 changes: 1 addition & 1 deletion syftbox/lib/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def hash_file(file_path: Path, root_dir: Optional[Path] = None) -> Optional[File
try:
if file_path.stat().st_size > 100_000_000:
logger.warning("File too large: %s", file_path)
return str(file_path), None
return None

with open(file_path, "rb") as f:
# not ideal for large files
Expand Down

0 comments on commit 350ec11

Please sign in to comment.