Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/_experimental/mcp_server/outbound_credentials/result.py: 81%
21 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
1"""A tagged-union ``Result`` the type checker can actually narrow.
3``Ok`` and ``Error`` are separate frozen classes joined by a ``Union`` alias, so
4reaching for ``result.ok`` before eliminating the ``Error`` arm (via ``isinstance``
5or a ``match`` pattern) is a type error rather than a runtime ``AttributeError``. A
6single class carrying both payload fields would make that unguarded access invisible
7to the type checker.
9Both variants are covariant and frozen; the absent side defaults to ``Never`` so a
10bare ``Ok(value)`` or ``Error(err)`` infers fully and is assignable to any ``Result``
11whose matching side fits.
13``is_ok`` / ``is_error`` are runtime predicates that also narrow via their ``Literal``
14returns; inside strictly typed code, discriminate with ``match`` or ``isinstance``.
16This is the shared ``Result`` shape for the ``outbound_credentials`` resolver: every
17seam returns ``Result[T, CredError]`` instead of raising, so each failure is a value
18the caller must handle rather than an exception that can slip past the type checker.
19"""
21from __future__ import annotations
23from dataclasses import dataclass
24from typing import Generic, Literal, TypeAlias
26from typing_extensions import Never, TypeVar
28_TOk_co = TypeVar("_TOk_co", covariant=True, default=Never)
29_TError_co = TypeVar("_TError_co", covariant=True, default=Never)
32@dataclass(frozen=True)
33class Ok(Generic[_TOk_co, _TError_co]):
34 ok: _TOk_co
36 def is_ok(self) -> Literal[True]:
37 return True
39 def is_error(self) -> Literal[False]:
40 return False
43@dataclass(frozen=True)
44class Error(Generic[_TOk_co, _TError_co]):
45 error: _TError_co
47 def is_ok(self) -> Literal[False]:
48 return False
50 def is_error(self) -> Literal[True]:
51 return True
54Result: TypeAlias = Ok[_TOk_co, _TError_co] | Error[_TOk_co, _TError_co]