Skip to content
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
5 changes: 2 additions & 3 deletions src/dmaf/gcs_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ def download_gcs_blob(gcs_path: str) -> Path:
blob = bucket.blob(blob_name)

suffix = Path(blob_name).suffix
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix, prefix="dmaf_gcs_")
blob.download_to_filename(tmp.name)
tmp.close()
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix, prefix="dmaf_gcs_") as tmp:
blob.download_to_filename(tmp.name)

logger.debug(f"Downloaded {gcs_path} -> {tmp.name}")
return Path(tmp.name)
Comment on lines +100 to 103
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The variable tmp is referenced outside its scope. After the context manager exits (line 100), tmp.name is accessed on line 102, but tmp is only defined within the with block. To fix this, capture tmp.name in a variable before the context exits, similar to the pattern used in tests/test_gcs_watcher.py lines 72-74.

Suggested change
blob.download_to_filename(tmp.name)
logger.debug(f"Downloaded {gcs_path} -> {tmp.name}")
return Path(tmp.name)
tmp_name = tmp.name
blob.download_to_filename(tmp_name)
logger.debug(f"Downloaded {gcs_path} -> {tmp_name}")
return Path(tmp_name)

Copilot uses AI. Check for mistakes.
Expand Down
15 changes: 8 additions & 7 deletions tests/test_gcs_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ def test_dedup_key_is_gcs_path(self, mock_get_client):
from PIL import Image

test_img = Image.fromarray(np.zeros((100, 100, 3), dtype=np.uint8))
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
test_img.save(tmp.name)
tmp.close()
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
test_img.save(tmp.name)
temp_image_path = Path(tmp.name)

# Mock blob download to copy our test image
def fake_download(filename):
import shutil
shutil.copy(tmp.name, filename)

shutil.copy(temp_image_path, filename)

mock_bucket.blob.return_value.download_to_filename = fake_download

Expand All @@ -103,15 +104,15 @@ def fake_download(filename):
assert result.processed == 1

# Clean up our test image
Path(tmp.name).unlink(missing_ok=True)
temp_image_path.unlink(missing_ok=True)

def test_local_dirs_unchanged(self, tmp_path):
"""Local directory scanning should work exactly as before."""
from dmaf.watcher import scan_and_process_once

import numpy as np
from PIL import Image

from dmaf.watcher import scan_and_process_once

# Create a test image in local dir
img_path = tmp_path / "test.jpg"
test_img = Image.fromarray(np.zeros((100, 100, 3), dtype=np.uint8))
Expand Down
6 changes: 3 additions & 3 deletions tests/test_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def test_delete_source_enabled_batch_mode(self, mock_sha256, mock_image_open, te
mock_img = Mock(spec=Image.Image)
mock_img_rgb = Mock()
mock_img.convert.return_value = mock_img_rgb
mock_image_open.return_value = mock_img
mock_image_open.return_value.__enter__.return_value = mock_img

mock_sha256.return_value = "test_hash"

Expand Down Expand Up @@ -656,7 +656,7 @@ def test_delete_source_disabled_batch_mode(self, mock_sha256, mock_image_open, t
mock_img = Mock(spec=Image.Image)
mock_img_rgb = Mock()
mock_img.convert.return_value = mock_img_rgb
mock_image_open.return_value = mock_img
mock_image_open.return_value.__enter__.return_value = mock_img

mock_sha256.return_value = "test_hash"

Expand Down Expand Up @@ -698,7 +698,7 @@ def test_delete_source_upload_failure_batch_mode(
mock_img = Mock(spec=Image.Image)
mock_img_rgb = Mock()
mock_img.convert.return_value = mock_img_rgb
mock_image_open.return_value = mock_img
mock_image_open.return_value.__enter__.return_value = mock_img

mock_sha256.return_value = "test_hash"

Expand Down
Loading