Skip to content

Commit 9dc2959

Browse files
tpayetclaude
andcommitted
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]>
1 parent 392ad0e commit 9dc2959

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

meilisearch/_local_server.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import time
1414
from pathlib import Path
1515
from typing import Optional, Tuple
16+
from urllib.error import URLError
1617
from urllib.parse import urlparse
1718
from urllib.request import urlopen
1819

@@ -104,14 +105,14 @@ def _download_meilisearch(self) -> str:
104105
if not binary_path.exists():
105106
print(f"Downloading Meilisearch binary from {download_url}...")
106107
try:
107-
response = urlopen(download_url, timeout=300)
108-
with open(binary_path, "wb") as f:
109-
f.write(response.read())
108+
with urlopen(download_url, timeout=300) as response:
109+
with open(binary_path, "wb") as f:
110+
f.write(response.read())
110111
# Make it executable
111112
binary_path.chmod(0o755)
112113
print(f"Downloaded Meilisearch to {binary_path}")
113114
except Exception as e:
114-
raise RuntimeError(f"Failed to download Meilisearch: {e}")
115+
raise RuntimeError(f"Failed to download Meilisearch: {e}") from e
115116

116117
return str(binary_path)
117118

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

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

162165
except Exception as e:
163166
self.stop()
164-
raise RuntimeError(f"Failed to start Meilisearch: {e}")
167+
raise RuntimeError(f"Failed to start Meilisearch: {e}") from e
165168

166169
def _wait_for_server(self, timeout: int = 30) -> None:
167170
"""Wait for the server to be ready."""
168171
start_time = time.time()
169172
while time.time() - start_time < timeout:
170173
try:
171-
response = urlopen(f"{self.url}/health", timeout=1)
172-
if response.status == 200:
173-
return
174-
except Exception:
174+
with urlopen(f"{self.url}/health", timeout=1) as response:
175+
if response.status == 200:
176+
return
177+
except (OSError, URLError):
175178
pass
176179

177180
# Check if process has died
@@ -207,7 +210,7 @@ def stop(self) -> None:
207210
if self._temp_data_dir and os.path.exists(self.data_path):
208211
try:
209212
shutil.rmtree(self.data_path)
210-
except Exception:
213+
except OSError:
211214
pass # Ignore cleanup errors
212215

213216
def __del__(self) -> None:

0 commit comments

Comments
 (0)