Skip to content

Commit 53e531c

Browse files
committed
refactor: rename the covariant lifespan TypeVar to LifespanT_co
context.py and runner.py both declared a TypeVar named LifespanT with different variance. The covariant one (Context's) is now LifespanT_co, and both context.py declarations carry a one-line variance rationale.
1 parent fda5736 commit 53e531c

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/mcp/server/context.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from mcp.shared.transport_context import TransportContext
1616
from mcp.types import LoggingLevel, RequestId, RequestParamsMeta
1717

18+
# Invariant: parameterizes a mutable dataclass field; dict default matches the default lifespan.
1819
LifespanContextT = TypeVar("LifespanContextT", default=dict[str, Any])
1920
RequestT = TypeVar("RequestT", default=Any)
2021

@@ -38,10 +39,11 @@ class ServerRequestContext(Generic[LifespanContextT, RequestT]):
3839
close_standalone_sse_stream: CloseSSEStreamCallback | None = None
3940

4041

41-
LifespanT = TypeVar("LifespanT", default=Any, covariant=True)
42+
# Covariant: `lifespan` is exposed read-only, so a `Context[AppState]` passes as `Context[object]`.
43+
LifespanT_co = TypeVar("LifespanT_co", default=Any, covariant=True)
4244

4345

44-
class Context(BaseContext[TransportContext], ClientPeerMixin, TypedServerRequestMixin, Generic[LifespanT]):
46+
class Context(BaseContext[TransportContext], ClientPeerMixin, TypedServerRequestMixin, Generic[LifespanT_co]):
4547
"""Server-side per-request context.
4648
4749
Composes `BaseContext` (forwards to `DispatchContext`, satisfies `Outbound`),
@@ -57,7 +59,7 @@ def __init__(
5759
self,
5860
dctx: DispatchContext[TransportContext],
5961
*,
60-
lifespan: LifespanT,
62+
lifespan: LifespanT_co,
6163
connection: Connection,
6264
meta: RequestParamsMeta | None = None,
6365
) -> None:
@@ -66,7 +68,7 @@ def __init__(
6668
self._connection = connection
6769

6870
@property
69-
def lifespan(self) -> LifespanT:
71+
def lifespan(self) -> LifespanT_co:
7072
"""The server-wide lifespan output (what `Server(..., lifespan=...)` yielded)."""
7173
return self._lifespan
7274

0 commit comments

Comments
 (0)