Skip to content

Commit 9b62ddb

Browse files
committed
mypy fixes
1 parent bea78dd commit 9b62ddb

13 files changed

Lines changed: 102 additions & 89 deletions

File tree

build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class CustomBuildHook(BuildHookInterface):
9-
def initialize(self, version, build_data):
9+
def initialize(self, version, build_data): # noqa: ARG002
1010
build_data["ext_modules"] = [
1111
Extension(
1212
"telemetric._stats_wrapper",

examples/test_scipy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
# test()
2222

2323
# How many calls (errors, and how often something was odd with the args)
24-
print("counts: ", stats.norm.pdf._get_counts())
24+
print("counts: ", stats.norm.pdf._get_counts()) # noqa: T201
2525

2626
# Detailed statistics for each parameter. How often was it passed
2727
# and how often were "a" and 3 passed (or something equal to them,
2828
# I do not consider types right now, although one could)?
29-
print("param stats: ", stats.norm.pdf._get_param_stats())
29+
print("param stats: ", stats.norm.pdf._get_param_stats()) # noqa: T201

examples/test_span.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88

99
@span
1010
def foo(hello="world", delay=1):
11-
print(hello)
11+
print(hello) # noqa: T201
1212
sleep(delay)
1313

1414

1515
@span
1616
def bar(spam="eggs", delay=1):
17-
print(spam)
17+
print(spam) # noqa: T201
1818
sleep(delay)
1919

2020

2121
@span
2222
def baz(apple="orange", delay=1):
23-
print(apple)
23+
print(apple) # noqa: T201
2424
sleep(delay)
2525

2626

examples/test_stats_uploader.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,42 @@ def another_func(x=1, y=True):
3434

3535

3636
# Print current statistics
37-
print("=== Current Statistics ===")
38-
print("test_func counts:", test_func._get_counts())
39-
print("test_func param stats:", test_func._get_param_stats())
40-
print()
41-
print("another_func counts:", another_func._get_counts())
42-
print("another_func param stats:", another_func._get_param_stats())
43-
print()
37+
print("=== Current Statistics ===") # noqa: T201
38+
print("test_func counts:", test_func._get_counts()) # noqa: T201
39+
print("test_func param stats:", test_func._get_param_stats()) # noqa: T201
40+
print() # noqa: T201
41+
print("another_func counts:", another_func._get_counts()) # noqa: T201
42+
print("another_func param stats:", another_func._get_param_stats()) # noqa: T201
43+
print() # noqa: T201
4444

4545

4646
# Test the stats uploader (this will try to upload to GA4)
47-
print("=== Testing Stats Uploader ===")
47+
print("=== Testing Stats Uploader ===") # noqa: T201
4848

4949
# Use a dummy proxy URL for testing (won't actually send data unless configured)
5050
uploader = StatsUploader(
5151
proxy_url="https://analytics-proxy-production-665e.up.railway.app"
5252
)
5353

54-
print("Analytics client enabled:", uploader.analytics.enabled)
54+
print("Analytics client enabled:", uploader.analytics.enabled) # noqa: T201
5555

5656
# Test uploading individual function stats
57-
print("Uploading test_func stats...")
57+
print("Uploading test_func stats...") # noqa: T201
5858
result1 = uploader.upload_function_stats(test_func, package_name="test_package")
59-
print("Upload result:", result1)
59+
print("Upload result:", result1) # noqa: T201
6060

61-
print("Uploading another_func stats...")
61+
print("Uploading another_func stats...") # noqa: T201
6262
result2 = uploader.upload_function_stats(another_func, package_name="test_package")
63-
print("Upload result:", result2)
63+
print("Upload result:", result2) # noqa: T201
6464

6565
# Test uploading all stats
66-
print("\nUploading all stats...")
66+
print("\nUploading all stats...") # noqa: T201
6767
summary = uploader.upload_all_stats(package_name="test_package")
68-
print("Upload summary:", summary)
68+
print("Upload summary:", summary) # noqa: T201
6969

7070
# Test custom stats upload
71-
print("\nUploading custom stats...")
71+
print("\nUploading custom stats...") # noqa: T201
7272
custom_result = uploader.upload_custom_stats(
7373
"custom_metric", {"metric_name": "test_metric", "value": 42, "category": "testing"}
7474
)
75-
print("Custom upload result:", custom_result)
75+
print("Custom upload result:", custom_result) # noqa: T201

examples/test_statswrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def test_func(a, b=None, c=None):
1212
test_func(1, 3)
1313

1414
# How many calls (errors, and how often something was odd with the args)
15-
print("counts: ", test_func._get_counts())
15+
print("counts: ", test_func._get_counts()) # noqa: T201
1616

1717
# Detailed statistics for each parameter. How often was it passed
1818
# and how often were "a" and 3 passed (or something equal to them,
1919
# I do not consider types right now, although one could)?
20-
print("param stats: ", test_func._get_param_stats())
20+
print("param stats: ", test_func._get_param_stats()) # noqa: T201

src/telemetric/console.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
import os
44

5-
from opentelemetry import trace
6-
from opentelemetry.sdk.resources import Resource
7-
from opentelemetry.sdk.trace import TracerProvider
8-
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
5+
from opentelemetry import trace # type: ignore[import-not-found]
6+
from opentelemetry.sdk.resources import Resource # type: ignore[import-not-found]
7+
from opentelemetry.sdk.trace import TracerProvider # type: ignore[import-not-found]
8+
from opentelemetry.sdk.trace.export import ( # type: ignore[import-not-found]
9+
BatchSpanProcessor,
10+
ConsoleSpanExporter,
11+
)
912

1013
__all__ = ["setup_console"]
1114

1215

13-
def setup_console(service_name: str | None = None):
16+
def setup_console(service_name: str | None = None) -> None:
1417
if service_name is None:
15-
attributes = os.environ.get("OTEL_RESOURCE_ATTRIBUTES")
16-
attributes = dict(k.split("=") for k in attributes.split(","))
18+
attributes_str = os.environ.get("OTEL_RESOURCE_ATTRIBUTES")
19+
if attributes_str:
20+
attributes = dict(k.split("=") for k in attributes_str.split(","))
21+
else:
22+
attributes = {}
1723
else:
1824
attributes = {"service.name": service_name}
1925

src/telemetric/entrypoint.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3-
from opentelemetry.instrumentation.auto_instrumentation import initialize
3+
from opentelemetry.instrumentation.auto_instrumentation import ( # type: ignore[import-not-found]
4+
initialize,
5+
)
46

57
from telemetric import install
68
from telemetric.console import setup_console

src/telemetric/ga4/analytics.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import platform
88
import uuid
99

10-
import requests
10+
import requests # type: ignore[import-untyped]
1111

1212
_log = logging.getLogger()
1313

@@ -24,19 +24,20 @@ class AnalyticsClient:
2424
})
2525
"""
2626

27-
def __init__(self, proxy_url):
27+
def __init__(self, proxy_url: str) -> None:
2828
self.proxy_url = proxy_url
2929
self.client_id = str(uuid.uuid4())
3030
self.enabled = not self._is_disabled()
3131

32-
def _is_disabled(self):
33-
if os.environ.get("DO_NOT_TRACK", "").lower() in ("1", "true"):
34-
return True
35-
if os.environ.get("CI", "").lower() in ("1", "true"):
36-
return True
37-
return False
32+
def _is_disabled(self) -> bool:
33+
return os.environ.get("DO_NOT_TRACK", "").lower() in (
34+
"1",
35+
"true",
36+
) or os.environ.get("CI", "").lower() in ("1", "true")
3837

39-
def track_event(self, event_name, params=None):
38+
def track_event(
39+
self, event_name: str, params: dict[str, str] | None = None
40+
) -> None:
4041
if not self.enabled:
4142
return
4243

@@ -60,7 +61,7 @@ def track_event(self, event_name, params=None):
6061
_log.debug(f"POST request to: {url}")
6162
_log.debug(payload)
6263

63-
try:
64+
try: # noqa: SIM105
6465
requests.post(f"{self.proxy_url}/track", json=payload, timeout=2)
6566
except Exception:
6667
pass

src/telemetric/ga4/ga4_proxy.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import os
88

9-
import requests
10-
from fastapi import FastAPI, Request
11-
from fastapi.middleware.cors import CORSMiddleware
12-
from fastapi.responses import JSONResponse
9+
import requests # type: ignore[import-untyped]
10+
from fastapi import FastAPI, Request # type: ignore[import-not-found]
11+
from fastapi.middleware.cors import CORSMiddleware # type: ignore[import-not-found]
12+
from fastapi.responses import JSONResponse # type: ignore[import-not-found]
1313

1414
app = FastAPI()
1515

@@ -28,7 +28,7 @@
2828

2929

3030
@app.get("/debug")
31-
async def debug():
31+
async def debug(): # type: ignore[no-untyped-def]
3232
"""Debug endpoint to check if env vars are loaded"""
3333
return {
3434
"GA4_MEASUREMENT_ID_set": bool(GA4_MEASUREMENT_ID),
@@ -40,13 +40,13 @@ async def debug():
4040

4141

4242
@app.get("/")
43-
async def root():
43+
async def root(): # type: ignore[no-untyped-def]
4444
"""Health check endpoint"""
4545
return {"status": "ok", "service": "GA4 Analytics Proxy"}
4646

4747

48-
@app.post("/track")
49-
async def track_event(request: Request):
48+
@app.post("/track") # type: ignore[misc]
49+
async def track_event(request: Request): # type: ignore[no-untyped-def]
5050
"""
5151
Proxy endpoint that forwards events to GA4
5252
@@ -104,7 +104,7 @@ async def track_event(request: Request):
104104

105105

106106
if __name__ == "__main__":
107-
import uvicorn
107+
import uvicorn # type: ignore[import-not-found]
108108

109-
port = int(os.environ.get("PORT", 8000))
109+
port = int(os.environ.get("PORT", "8000"))
110110
uvicorn.run(app, host="0.0.0.0", port=port)

src/telemetric/ga4/stats_uploader.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import Any, Dict, Optional
5+
from typing import Any
66

77
from telemetric.ga4.analytics import AnalyticsClient
88
from telemetric.statswrapper import _wrapped
@@ -26,8 +26,8 @@ def __init__(self, proxy_url: str):
2626
"""
2727
self.analytics = AnalyticsClient(proxy_url)
2828

29-
def upload_function_stats(
30-
self, wrapped_func, package_name: Optional[str] = None
29+
def upload_function_stats( # type: ignore[no-untyped-def]
30+
self, wrapped_func, package_name: str | None = None
3131
) -> bool:
3232
"""
3333
Upload statistics for a single wrapped function to GA4.
@@ -98,8 +98,8 @@ def upload_function_stats(
9898
return True
9999

100100
def upload_all_stats(
101-
self, package_name: Optional[str] = None, skip_uncalled: bool = True
102-
) -> Dict[str, Any]:
101+
self, package_name: str | None = None, skip_uncalled: bool = True
102+
) -> dict[str, Any]:
103103
"""
104104
Upload statistics for all wrapped functions to GA4.
105105
@@ -152,7 +152,7 @@ def upload_all_stats(
152152
"status": "completed",
153153
}
154154

155-
def upload_custom_stats(self, event_name: str, stats_data: Dict[str, Any]) -> bool:
155+
def upload_custom_stats(self, event_name: str, stats_data: dict[str, Any]) -> bool:
156156
"""
157157
Upload custom statistics data to GA4.
158158

0 commit comments

Comments
 (0)