Skip to content

Commit b158fa4

Browse files
committed
Replace requests with httpx for type safety (Fixes mypy check)
2 parents 83469f9 + c79034c commit b158fa4

4 files changed

Lines changed: 15 additions & 16 deletions

File tree

computer_vision/vision_transformer_demo.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- torch
1313
- transformers
1414
- Pillow (PIL)
15-
- requests
15+
- httpx (already in repo dependencies)
1616
1717
Resources:
1818
- Paper: https://arxiv.org/abs/2010.11929
@@ -40,13 +40,13 @@
4040
from typing import Any
4141

4242
try:
43-
import requests
43+
import httpx
4444
import torch
4545
from PIL import Image
4646
from transformers import ViTForImageClassification, ViTImageProcessor
4747
except ImportError as e:
4848
print(f"Error: Missing required dependency: {e.name}")
49-
print("Install dependencies: pip install torch transformers pillow requests")
49+
print("Install dependencies: pip install torch transformers pillow httpx")
5050
sys.exit(1)
5151

5252

@@ -62,8 +62,8 @@ def load_image(image_source: str | Path, timeout: int = 10) -> Image.Image:
6262
PIL Image object
6363
6464
Raises:
65-
TimeoutError: If request times out
66-
ConnectionError: If URL is unreachable
65+
TimeoutError: If request times out
66+
ConnectionError: If URL is unreachable
6767
FileNotFoundError: If local file doesn't exist
6868
IOError: If image cannot be opened
6969
@@ -79,16 +79,17 @@ def load_image(image_source: str | Path, timeout: int = 10) -> Image.Image:
7979
("http://", "https://")
8080
):
8181
try:
82-
response = requests.get(str(image_source), timeout=timeout)
83-
response.raise_for_status()
84-
return Image.open(BytesIO(response.content)).convert("RGB")
85-
except requests.exceptions.Timeout:
82+
with httpx.Client(timeout=timeout) as client:
83+
response = client.get(str(image_source))
84+
response.raise_for_status()
85+
return Image.open(BytesIO(response.content)).convert("RGB")
86+
except httpx.TimeoutException:
8687
msg = (
8788
f"Request timed out after {timeout} seconds. "
8889
"Try increasing the timeout parameter."
8990
)
9091
raise TimeoutError(msg)
91-
except requests.exceptions.RequestException as e:
92+
except httpx.HTTPError as e:
9293
msg = f"Failed to download image from URL: {e}"
9394
raise ConnectionError(msg) from e
9495
else:

machine_learning/decision_tree.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,13 @@ def predict(self, x):
146146
"""
147147
if self.prediction is not None:
148148
return self.prediction
149-
elif self.left or self.right is not None:
149+
elif self.left is not None and self.right is not None:
150150
if x >= self.decision_boundary:
151151
return self.right.predict(x)
152152
else:
153153
return self.left.predict(x)
154154
else:
155-
print("Error: Decision tree not yet trained")
156-
return None
155+
raise ValueError("Decision tree not yet trained")
157156

158157

159158
class TestDecisionTree:
@@ -201,4 +200,4 @@ def main():
201200
main()
202201
import doctest
203202

204-
doctest.testmod(name="mean_squarred_error", verbose=True)
203+
doctest.testmod(name="mean_squared_error", verbose=True)

maths/monte_carlo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from statistics import mean
99

1010

11-
def pi_estimator(iterations: int):
11+
def pi_estimator(iterations: int) -> None:
1212
"""
1313
An implementation of the Monte Carlo method used to find pi.
1414
1. Draw a 2x2 square centred at (0,0).

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ dependencies = [
2020
"opencv-python>=4.10.0.84",
2121
"pandas>=2.2.3",
2222
"pillow>=11.3",
23-
"requests>=2.31.0",
2423
"rich>=13.9.4",
2524
"scikit-learn>=1.5.2",
2625
"scipy>=1.16.2",

0 commit comments

Comments
 (0)