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

1"""A tagged-union ``Result`` the type checker can actually narrow. 

2 

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. 

8 

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. 

12 

13``is_ok`` / ``is_error`` are runtime predicates that also narrow via their ``Literal`` 

14returns; inside strictly typed code, discriminate with ``match`` or ``isinstance``. 

15 

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""" 

20 

21from __future__ import annotations 

22 

23from dataclasses import dataclass 

24from typing import Generic, Literal, TypeAlias 

25 

26from typing_extensions import Never, TypeVar 

27 

28_TOk_co = TypeVar("_TOk_co", covariant=True, default=Never) 

29_TError_co = TypeVar("_TError_co", covariant=True, default=Never) 

30 

31 

32@dataclass(frozen=True) 

33class Ok(Generic[_TOk_co, _TError_co]): 

34 ok: _TOk_co 

35 

36 def is_ok(self) -> Literal[True]: 

37 return True 

38 

39 def is_error(self) -> Literal[False]: 

40 return False 

41 

42 

43@dataclass(frozen=True) 

44class Error(Generic[_TOk_co, _TError_co]): 

45 error: _TError_co 

46 

47 def is_ok(self) -> Literal[False]: 

48 return False 

49 

50 def is_error(self) -> Literal[True]: 

51 return True 

52 

53 

54Result: TypeAlias = Ok[_TOk_co, _TError_co] | Error[_TOk_co, _TError_co]