@@ -41,13 +41,13 @@ class Thunk(Generic[T, Unpack[TS]]):
41
41
state : Resolved [T ] | Unresolved [T , Unpack [TS ]] | Resolving | Error
42
42
43
43
@classmethod
44
- def fn (cls , fn : Callable [[Unpack [TS ]], T ], * args : Unpack [TS ]) -> Thunk [T , Unpack [TS ]]:
44
+ def fn (cls , fn : Callable [[Unpack [TS ]], T ], * args : Unpack [TS ], context : str | None = None ) -> Thunk [T , Unpack [TS ]]:
45
45
"""
46
46
Create a thunk based on some functions and some partial args.
47
47
48
48
If the function is called while it is being resolved recursively it will raise an exception.
49
49
"""
50
- return cls (Unresolved (fn , args ))
50
+ return cls (Unresolved (fn , args , context ))
51
51
52
52
@classmethod
53
53
def value (cls , value : T ) -> Thunk [T ]:
@@ -57,12 +57,12 @@ def __call__(self) -> T:
57
57
match self .state :
58
58
case Resolved (value ):
59
59
return value
60
- case Unresolved (fn , args ):
60
+ case Unresolved (fn , args , context ):
61
61
self .state = Resolving ()
62
62
try :
63
63
res = fn (* args )
64
64
except Exception as e :
65
- self .state = Error (e )
65
+ self .state = Error (e , context )
66
66
raise e from None
67
67
else :
68
68
self .state = Resolved (res )
@@ -83,6 +83,7 @@ class Resolved(Generic[T]):
83
83
class Unresolved (Generic [T , Unpack [TS ]]):
84
84
fn : Callable [[Unpack [TS ]], T ]
85
85
args : tuple [Unpack [TS ]]
86
+ context : str | None
86
87
87
88
88
89
@dataclass
@@ -93,3 +94,4 @@ class Resolving:
93
94
@dataclass
94
95
class Error :
95
96
e : Exception
97
+ context : str | None
0 commit comments