Skip to content
Draft
Prev Previous commit
Next Next commit
fix: Address pylint warnings in _local_server.py
- Use 'with' statements for resource management where appropriate
- Add explicit exception chaining with 'from e'
- Replace broad Exception catches with specific exceptions (OSError, URLError)
- Add pylint disable comment for subprocess.Popen usage where 'with' is not suitable

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
  • Loading branch information
tpayet and claude committed May 26, 2025
commit 9dc2959d26e4bf4688ce826fb0b671ce5edbf5ff
23 changes: 13 additions & 10 deletions meilisearch/_local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import time
from pathlib import Path
from typing import Optional, Tuple
from urllib.error import URLError
from urllib.parse import urlparse
from urllib.request import urlopen

Expand Down Expand Up @@ -104,14 +105,14 @@ def _download_meilisearch(self) -> str:
if not binary_path.exists():
print(f"Downloading Meilisearch binary from {download_url}...")
try:
response = urlopen(download_url, timeout=300)
with open(binary_path, "wb") as f:
f.write(response.read())
with urlopen(download_url, timeout=300) as response:
with open(binary_path, "wb") as f:
f.write(response.read())
# Make it executable
binary_path.chmod(0o755)
print(f"Downloaded Meilisearch to {binary_path}")
except Exception as e:
raise RuntimeError(f"Failed to download Meilisearch: {e}")
raise RuntimeError(f"Failed to download Meilisearch: {e}") from e

return str(binary_path)

Expand Down Expand Up @@ -146,6 +147,8 @@ def start(self) -> None:

# Start the process
try:
# pylint: disable=consider-using-with
# We don't use 'with' here because we want the process to run in the background
self.process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
Expand All @@ -161,17 +164,17 @@ def start(self) -> None:

except Exception as e:
self.stop()
raise RuntimeError(f"Failed to start Meilisearch: {e}")
raise RuntimeError(f"Failed to start Meilisearch: {e}") from e

def _wait_for_server(self, timeout: int = 30) -> None:
"""Wait for the server to be ready."""
start_time = time.time()
while time.time() - start_time < timeout:
try:
response = urlopen(f"{self.url}/health", timeout=1)
if response.status == 200:
return
except Exception:
with urlopen(f"{self.url}/health", timeout=1) as response:
if response.status == 200:
return
except (OSError, URLError):
pass

# Check if process has died
Expand Down Expand Up @@ -207,7 +210,7 @@ def stop(self) -> None:
if self._temp_data_dir and os.path.exists(self.data_path):
try:
shutil.rmtree(self.data_path)
except Exception:
except OSError:
pass # Ignore cleanup errors

def __del__(self) -> None:
Expand Down
Loading