Skip to content

Commit adc471f

Browse files
committed
Fix lint and type errors for CI
- Replace Optional[X] with X | None (UP045) - Restructure static region data to fit line length limit (E501) - Use datetime.UTC alias (UP017) - Fix boto3 Session init and CloudWatch client typing
1 parent 3a6e64b commit adc471f

3 files changed

Lines changed: 49 additions & 39 deletions

File tree

canopy/cli/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Canopy CLI entry point."""
22

3-
from typing import Annotated, Optional
3+
from typing import Annotated
44

55
import typer
66
from rich.console import Console
@@ -25,7 +25,7 @@ def version_callback(value: bool) -> None:
2525
@app.callback()
2626
def main(
2727
version: Annotated[
28-
Optional[bool],
28+
bool | None,
2929
typer.Option("--version", "-v", callback=version_callback, is_eager=True),
3030
] = None,
3131
) -> None:
@@ -35,7 +35,7 @@ def main(
3535
@app.command()
3636
def audit(
3737
provider: Annotated[str, typer.Option(help="Cloud provider (aws, gcp)")] = "aws",
38-
region: Annotated[Optional[str], typer.Option(help="Filter by region")] = None,
38+
region: Annotated[str | None, typer.Option(help="Filter by region")] = None,
3939
output: Annotated[str, typer.Option(help="Output format (table, json)")] = "table",
4040
) -> None:
4141
"""Scan running infrastructure and compute EcoWeight scores."""

canopy/engine/carbon/client.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,46 @@
44

55
from canopy.models.core import Region
66

7+
78
# Static fallback data based on publicly available sources.
89
# Used when no API key is configured or when offline.
910
# Sources: Google Cloud CFE%, Electricity Maps, WattTime
11+
def _r(
12+
provider: str, name: str, location: str, cfe: int, intensity: int
13+
) -> dict[str, object]:
14+
return {
15+
"provider": provider, "name": name,
16+
"location": location, "cfe": cfe, "intensity": intensity,
17+
}
18+
19+
1020
STATIC_REGIONS: list[dict[str, object]] = [
1121
# GCP
12-
{"provider": "gcp", "name": "europe-north1", "location": "Finland", "cfe": 94, "intensity": 8},
13-
{"provider": "gcp", "name": "europe-north2", "location": "Stockholm, SE", "cfe": 100, "intensity": 3},
14-
{"provider": "gcp", "name": "europe-west9", "location": "Paris, FR", "cfe": 96, "intensity": 16},
15-
{"provider": "gcp", "name": "northamerica-northeast1", "location": "Montréal, CA", "cfe": 99, "intensity": 5},
16-
{"provider": "gcp", "name": "us-west1", "location": "Oregon, US", "cfe": 87, "intensity": 79},
17-
{"provider": "gcp", "name": "us-central1", "location": "Iowa, US", "cfe": 97, "intensity": 39},
18-
{"provider": "gcp", "name": "us-east1", "location": "South Carolina, US", "cfe": 31, "intensity": 576},
19-
{"provider": "gcp", "name": "us-east4", "location": "Virginia, US", "cfe": 28, "intensity": 312},
20-
{"provider": "gcp", "name": "asia-south1", "location": "Mumbai, IN", "cfe": 9, "intensity": 679},
21-
{"provider": "gcp", "name": "asia-east1", "location": "Taiwan", "cfe": 10, "intensity": 541},
22-
{"provider": "gcp", "name": "asia-northeast1", "location": "Tokyo, JP", "cfe": 19, "intensity": 463},
23-
{"provider": "gcp", "name": "europe-west1", "location": "Belgium", "cfe": 74, "intensity": 110},
24-
# AWS (estimated from grid data — AWS does not publish per-region CFE%)
25-
{"provider": "aws", "name": "eu-north-1", "location": "Stockholm, SE", "cfe": 98, "intensity": 8},
26-
{"provider": "aws", "name": "ca-central-1", "location": "Montréal, CA", "cfe": 95, "intensity": 12},
27-
{"provider": "aws", "name": "eu-west-1", "location": "Ireland", "cfe": 75, "intensity": 98},
28-
{"provider": "aws", "name": "eu-west-3", "location": "Paris, FR", "cfe": 93, "intensity": 22},
29-
{"provider": "aws", "name": "eu-central-1", "location": "Frankfurt, DE", "cfe": 55, "intensity": 252},
30-
{"provider": "aws", "name": "us-west-2", "location": "Oregon, US", "cfe": 85, "intensity": 79},
31-
{"provider": "aws", "name": "us-east-1", "location": "N. Virginia, US", "cfe": 30, "intensity": 312},
32-
{"provider": "aws", "name": "us-east-2", "location": "Ohio, US", "cfe": 25, "intensity": 410},
33-
{"provider": "aws", "name": "ap-south-1", "location": "Mumbai, IN", "cfe": 9, "intensity": 679},
34-
{"provider": "aws", "name": "ap-southeast-1", "location": "Singapore", "cfe": 3, "intensity": 408},
35-
{"provider": "aws", "name": "ap-northeast-1", "location": "Tokyo, JP", "cfe": 19, "intensity": 463},
36-
{"provider": "aws", "name": "sa-east-1", "location": "São Paulo, BR", "cfe": 80, "intensity": 61},
22+
_r("gcp", "europe-north1", "Finland", 94, 8),
23+
_r("gcp", "europe-north2", "Stockholm, SE", 100, 3),
24+
_r("gcp", "europe-west9", "Paris, FR", 96, 16),
25+
_r("gcp", "northamerica-northeast1", "Montréal, CA", 99, 5),
26+
_r("gcp", "us-west1", "Oregon, US", 87, 79),
27+
_r("gcp", "us-central1", "Iowa, US", 97, 39),
28+
_r("gcp", "us-east1", "S. Carolina, US", 31, 576),
29+
_r("gcp", "us-east4", "Virginia, US", 28, 312),
30+
_r("gcp", "asia-south1", "Mumbai, IN", 9, 679),
31+
_r("gcp", "asia-east1", "Taiwan", 10, 541),
32+
_r("gcp", "asia-northeast1", "Tokyo, JP", 19, 463),
33+
_r("gcp", "europe-west1", "Belgium", 74, 110),
34+
# AWS (estimated — AWS does not publish per-region CFE%)
35+
_r("aws", "eu-north-1", "Stockholm, SE", 98, 8),
36+
_r("aws", "ca-central-1", "Montréal, CA", 95, 12),
37+
_r("aws", "eu-west-1", "Ireland", 75, 98),
38+
_r("aws", "eu-west-3", "Paris, FR", 93, 22),
39+
_r("aws", "eu-central-1", "Frankfurt, DE", 55, 252),
40+
_r("aws", "us-west-2", "Oregon, US", 85, 79),
41+
_r("aws", "us-east-1", "N. Virginia, US", 30, 312),
42+
_r("aws", "us-east-2", "Ohio, US", 25, 410),
43+
_r("aws", "ap-south-1", "Mumbai, IN", 9, 679),
44+
_r("aws", "ap-southeast-1", "Singapore", 3, 408),
45+
_r("aws", "ap-northeast-1", "Tokyo, JP", 19, 463),
46+
_r("aws", "sa-east-1", "São Paulo, BR", 80, 61),
3747
]
3848

3949

canopy/engine/providers/aws.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""AWS cloud provider implementation."""
22

3-
from datetime import datetime, timedelta, timezone
3+
from datetime import UTC, datetime, timedelta
4+
from typing import Any
45

56
import boto3
67
from botocore.exceptions import ClientError
@@ -62,11 +63,10 @@ class AWSProvider(CloudProvider):
6263
"""AWS cloud provider using boto3."""
6364

6465
def __init__(self, profile: str | None = None, default_region: str = "us-east-1") -> None:
65-
session_kwargs: dict[str, str] = {}
66-
if profile:
67-
session_kwargs["profile_name"] = profile
68-
session_kwargs["region_name"] = default_region
69-
self._session = boto3.Session(**session_kwargs)
66+
self._session = boto3.Session(
67+
profile_name=profile,
68+
region_name=default_region,
69+
)
7070
self._default_region = default_region
7171

7272
@property
@@ -143,12 +143,12 @@ def _list_ec2_instances(self, region: str) -> list[Workload]:
143143

144144
return workloads
145145

146-
def _get_avg_cpu(self, cw_client: object, instance_id: str) -> float:
146+
def _get_avg_cpu(self, cw_client: Any, instance_id: str) -> float:
147147
"""Get average CPU utilization over the last 7 days."""
148148
try:
149-
end = datetime.now(timezone.utc)
149+
end = datetime.now(UTC)
150150
start = end - timedelta(days=7)
151-
response = cw_client.get_metric_statistics( # type: ignore[union-attr]
151+
response = cw_client.get_metric_statistics(
152152
Namespace="AWS/EC2",
153153
MetricName="CPUUtilization",
154154
Dimensions=[{"Name": "InstanceId", "Value": instance_id}],
@@ -157,10 +157,10 @@ def _get_avg_cpu(self, cw_client: object, instance_id: str) -> float:
157157
Period=86400,
158158
Statistics=["Average"],
159159
)
160-
datapoints = response.get("Datapoints", [])
160+
datapoints: list[dict[str, Any]] = response.get("Datapoints", [])
161161
if not datapoints:
162162
return 0.0
163-
return sum(d["Average"] for d in datapoints) / len(datapoints)
163+
return float(sum(d["Average"] for d in datapoints) / len(datapoints))
164164
except Exception:
165165
return 0.0
166166

0 commit comments

Comments
 (0)