11from __future__ import annotations
22from dataclasses import dataclass
3- from typing import Generic , TypeVar , Callable , Any , TypeAlias
3+ from typing import Generic , TypeVar , Callable , Any , TypeAlias , NoReturn , ParamSpec
44import functools
55
66T = TypeVar ("T" )
77E = TypeVar ("E" )
88U = TypeVar ("U" )
99F = TypeVar ("F" )
10+ P = ParamSpec ("P" ) # 用于保留装饰器参数签名
1011
1112
1213class UnwrapError (RuntimeError ):
1314 """统一抛出的解包异常。"""
1415
1516
1617# -------------------- 成功分支 --------------------
17- @dataclass (frozen = True )
18+ @dataclass (frozen = True , slots = True )
1819class Ok (Generic [T , E ]):
1920 value : T
2021
@@ -27,7 +28,7 @@ def is_err(self) -> bool:
2728 def unwrap (self ) -> T :
2829 return self .value
2930
30- def unwrap_err (self ) -> E :
31+ def unwrap_err (self ) -> NoReturn :
3132 raise UnwrapError (f"Called unwrap_err() on an Ok: { self .value !r} " )
3233
3334 def unwrap_or (self , default : T ) -> T :
@@ -61,7 +62,7 @@ def or_else(self, f: Callable[[E], Result[T, F]]) -> Result[T, F]:
6162
6263
6364# -------------------- 失败分支 --------------------
64- @dataclass (frozen = True )
65+ @dataclass (frozen = True , slots = True )
6566class Err (Generic [T , E ]):
6667 error : E
6768
@@ -83,7 +84,7 @@ def unwrap_or(self, default: T) -> T:
8384 def unwrap_or_else (self , op : Callable [[E ], T ]) -> T :
8485 return op (self .error )
8586
86- def expect (self , msg : str ) -> T :
87+ def expect (self , msg : str ) -> NoReturn :
8788 raise UnwrapError (f"{ msg } : { self .error !r} " )
8889
8990 def map (self , f : Callable [[T ], U ]) -> Err [U , E ]:
@@ -111,7 +112,7 @@ def inspect(self, func: Callable[[E], Any]) -> Err[T, E]:
111112Result : TypeAlias = Ok [T , E ] | Err [T , E ]
112113
113114
114- # -------------------- 小写构造器(Rust 风格) --------------------
115+ # -------------------- 辅助构造器 --------------------
115116def ok (value : T ) -> Ok [T , Any ]:
116117 """小写构造器,返回 Ok[T, Any](错误类型留作 Any)。"""
117118 return Ok (value )
@@ -123,13 +124,13 @@ def err(error: E) -> Err[Any, E]:
123124
124125
125126# -------------------- 装饰器:将普通函数变为返回 Result 的函数 --------------------
126- def catch (func : Callable [... , T ]) -> Callable [... , Result [T , Exception ]]:
127+ def catch (func : Callable [P , T ]) -> Callable [P , Result [T , Exception ]]:
127128 """
128- 装饰器。自动捕获函数内部的异常,
129- 将正常返回值包装为 Ok(value),异常包装为 Err(exception) 。
129+ 装饰器。自动捕获异常并返回 Result。
130+ 利用 ParamSpec 完美保留了原函数的参数类型提示 。
130131 """
131132 @functools .wraps (func )
132- def wrapper (* args : Any , ** kwargs : Any ) -> Result [T , Exception ]:
133+ def wrapper (* args : P . args , ** kwargs : P . kwargs ) -> Result [T , Exception ]:
133134 try :
134135 return Ok (func (* args , ** kwargs ))
135136 except Exception as e :
@@ -138,7 +139,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Result[T, Exception]:
138139
139140
140141# -------------------- 函数式调用:立即执行函数并返回 Result --------------------
141- def try_catch (func : Callable [... , T ], * args : Any , ** kwargs : Any ) -> Result [T , Exception ]:
142+ def try_catch (func : Callable [P , T ], * args : P . args , ** kwargs : P . kwargs ) -> Result [T , Exception ]:
142143 """手动调用:立即执行函数并返回 Result。"""
143144 try :
144145 return Ok (func (* args , ** kwargs ))
0 commit comments