Skip to content

Commit 3bc5f31

Browse files
committed
fixes for seed_users, tracing/utils
1 parent b5e40c1 commit 3bc5f31

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

backend/app/core/tracing/utils.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import asyncio
22
import functools
3+
from collections.abc import Callable, Generator
34
from contextlib import contextmanager
4-
from typing import Any, Callable, Dict, Generator
5+
from typing import Any, ParamSpec, TypeVar
56

67
from opentelemetry import context, propagate, trace
78
from opentelemetry.trace import SpanKind, Status, StatusCode
89

10+
P = ParamSpec("P")
11+
R = TypeVar("R")
12+
913

1014
def get_tracer() -> trace.Tracer:
1115
"""Get a tracer for the current module."""
@@ -16,7 +20,7 @@ def get_tracer() -> trace.Tracer:
1620
def trace_span(
1721
name: str,
1822
kind: SpanKind = SpanKind.INTERNAL,
19-
attributes: Dict[str, Any] | None = None,
23+
attributes: dict[str, Any] | None = None,
2024
set_status_on_exception: bool = True,
2125
tracer: trace.Tracer | None = None
2226
) -> Generator[trace.Span, None, None]:
@@ -53,38 +57,40 @@ def trace_span(
5357
def trace_method(
5458
name: str | None = None,
5559
kind: SpanKind = SpanKind.INTERNAL,
56-
attributes: Dict[str, Any] | None = None
57-
) -> Callable:
60+
attributes: dict[str, Any] | None = None
61+
) -> Callable[[Callable[P, R]], Callable[P, R]]:
5862
"""
5963
Decorator for tracing method calls.
60-
64+
6165
Args:
6266
name: Custom span name, defaults to module.method_name
6367
kind: Kind of span (INTERNAL, CLIENT, SERVER, etc.)
6468
attributes: Additional attributes to set on the span
65-
69+
6670
Returns:
6771
Decorated function with tracing
6872
"""
69-
def decorator(func: Callable) -> Callable:
73+
def decorator(func: Callable[P, R]) -> Callable[P, R]:
7074
span_name = name or f"{func.__module__}.{func.__name__}"
7175

7276
@functools.wraps(func)
73-
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
77+
async def async_wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
7478
with trace_span(span_name, kind=kind, attributes=attributes):
75-
return await func(*args, **kwargs)
79+
return await func(*args, **kwargs) # type: ignore[misc, no-any-return]
7680

7781
@functools.wraps(func)
78-
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
82+
def sync_wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
7983
with trace_span(span_name, kind=kind, attributes=attributes):
8084
return func(*args, **kwargs)
8185

82-
return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper
86+
if asyncio.iscoroutinefunction(func):
87+
return async_wrapper # type: ignore[return-value]
88+
return sync_wrapper
8389

8490
return decorator
8591

8692

87-
def inject_trace_context(headers: Dict[str, str]) -> Dict[str, str]:
93+
def inject_trace_context(headers: dict[str, str]) -> dict[str, str]:
8894
"""
8995
Inject current trace context into headers for propagation.
9096
@@ -99,7 +105,7 @@ def inject_trace_context(headers: Dict[str, str]) -> Dict[str, str]:
99105
return propagation_headers
100106

101107

102-
def extract_trace_context(headers: Dict[str, str]) -> context.Context:
108+
def extract_trace_context(headers: dict[str, str]) -> context.Context:
103109
"""
104110
Extract trace context from headers.
105111
@@ -126,7 +132,7 @@ def add_span_attributes(**attributes: Any) -> None:
126132
span.set_attribute(key, value)
127133

128134

129-
def add_span_event(name: str, attributes: Dict[str, Any] | None = None) -> None:
135+
def add_span_event(name: str, attributes: dict[str, Any] | None = None) -> None:
130136
"""
131137
Add an event to the current span.
132138

backend/scripts/seed_users.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import asyncio
1616
import os
1717
from datetime import datetime, timezone
18+
from typing import Any
1819

1920
from bson import ObjectId
2021
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase
@@ -24,7 +25,7 @@
2425

2526

2627
async def upsert_user(
27-
db: AsyncIOMotorDatabase,
28+
db: AsyncIOMotorDatabase[dict[str, Any]],
2829
username: str,
2930
email: str,
3031
password: str,
@@ -68,7 +69,7 @@ async def seed_users() -> None:
6869

6970
print(f"Connecting to MongoDB (database: {db_name})...")
7071

71-
client: AsyncIOMotorClient = AsyncIOMotorClient(mongodb_url)
72+
client: AsyncIOMotorClient[dict[str, Any]] = AsyncIOMotorClient(mongodb_url)
7273
db = client[db_name]
7374

7475
# Default user

0 commit comments

Comments
 (0)