Skip to content

Commit 1cdc31a

Browse files
Merge pull request #81 from a-akimov/python-server-add-ruff
Python weather server - Add the ruff llinter and apply it
2 parents 811d462 + 53d8d21 commit 1cdc31a

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

weather-server-python/pyproject.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@ dependencies = [
1313
requires = [ "hatchling",]
1414
build-backend = "hatchling.build"
1515

16+
[dependency-groups]
17+
dev = [
18+
"ruff>=0.14.8",
19+
]
20+
1621
[project.scripts]
1722
weather = "weather:main"
1823

24+
[tool.ruff]
25+
line-length = 120
26+
target-version = "py310"
27+
extend-exclude = ["README.md"]
28+
29+
[tool.ruff.lint]
30+
select = [
31+
"C4", # flake8-comprehensions
32+
"C90", # mccabe
33+
"E", # pycodestyle
34+
"F", # pyflakes
35+
"I", # isort
36+
"PERF", # Perflint
37+
"PL", # Pylint
38+
"UP", # pyupgrade
39+
]
40+
ignore = ["PERF203", "PLC0415", "PLR0402"]
41+
mccabe.max-complexity = 24 # Default is 10
42+
43+
[tool.ruff.lint.pylint]
44+
allow-magic-value-types = ["bytes", "float", "int", "str"]
45+
max-args = 23 # Default is 5
46+
max-branches = 23 # Default is 12
47+
max-returns = 13 # Default is 6
48+
max-statements = 102 # Default is 50

weather-server-python/uv.lock

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

weather-server-python/weather.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any
2+
23
import httpx
34
from mcp.server.fastmcp import FastMCP
45

@@ -9,12 +10,10 @@
910
NWS_API_BASE = "https://api.weather.gov"
1011
USER_AGENT = "weather-app/1.0"
1112

13+
1214
async def make_nws_request(url: str) -> dict[str, Any] | None:
1315
"""Make a request to the NWS API with proper error handling."""
14-
headers = {
15-
"User-Agent": USER_AGENT,
16-
"Accept": "application/geo+json"
17-
}
16+
headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"}
1817
async with httpx.AsyncClient() as client:
1918
try:
2019
response = await client.get(url, headers=headers, timeout=30.0)
@@ -23,17 +22,19 @@ async def make_nws_request(url: str) -> dict[str, Any] | None:
2322
except Exception:
2423
return None
2524

25+
2626
def format_alert(feature: dict) -> str:
2727
"""Format an alert feature into a readable string."""
2828
props = feature["properties"]
2929
return f"""
30-
Event: {props.get('event', 'Unknown')}
31-
Area: {props.get('areaDesc', 'Unknown')}
32-
Severity: {props.get('severity', 'Unknown')}
33-
Description: {props.get('description', 'No description available')}
34-
Instructions: {props.get('instruction', 'No specific instructions provided')}
30+
Event: {props.get("event", "Unknown")}
31+
Area: {props.get("areaDesc", "Unknown")}
32+
Severity: {props.get("severity", "Unknown")}
33+
Description: {props.get("description", "No description available")}
34+
Instructions: {props.get("instruction", "No specific instructions provided")}
3535
"""
3636

37+
3738
@mcp.tool()
3839
async def get_alerts(state: str) -> str:
3940
"""Get weather alerts for a US state.
@@ -53,6 +54,7 @@ async def get_alerts(state: str) -> str:
5354
alerts = [format_alert(feature) for feature in data["features"]]
5455
return "\n---\n".join(alerts)
5556

57+
5658
@mcp.tool()
5759
async def get_forecast(latitude: float, longitude: float) -> str:
5860
"""Get weather forecast for a location.
@@ -80,18 +82,20 @@ async def get_forecast(latitude: float, longitude: float) -> str:
8082
forecasts = []
8183
for period in periods[:5]: # Only show next 5 periods
8284
forecast = f"""
83-
{period['name']}:
84-
Temperature: {period['temperature']}°{period['temperatureUnit']}
85-
Wind: {period['windSpeed']} {period['windDirection']}
86-
Forecast: {period['detailedForecast']}
85+
{period["name"]}:
86+
Temperature: {period["temperature"]}°{period["temperatureUnit"]}
87+
Wind: {period["windSpeed"]} {period["windDirection"]}
88+
Forecast: {period["detailedForecast"]}
8789
"""
8890
forecasts.append(forecast)
8991

9092
return "\n---\n".join(forecasts)
9193

94+
9295
def main():
9396
# Initialize and run the server
94-
mcp.run(transport='stdio')
97+
mcp.run(transport="stdio")
98+
9599

96100
if __name__ == "__main__":
97101
main()

0 commit comments

Comments
 (0)