@@ -857,7 +857,7 @@ def __init__(
857857 deepcopy = False ,
858858 * ,
859859 _empty_init = False ,
860- _levels = - 3 ,
860+ _levels = - 4 ,
861861 ):
862862 if isinstance (value , Result ):
863863 self ._success = value ._success
@@ -890,8 +890,8 @@ def as_Ok(cls, value, deepcopy: bool = False):
890890 return cls (value , deepcopy = deepcopy )
891891
892892 @classmethod
893- def as_Err (cls , error_msg , add_traceback : bool = True ):
894- return cls (EMPTY_ERROR_MSG , False , error_msg , add_traceback , _levels = - 4 )
893+ def as_Err (cls , error_msg , add_traceback : bool = True , * , _levels = - 5 ):
894+ return cls (EMPTY_ERROR_MSG , False , error_msg , add_traceback , _levels = _levels )
895895
896896 @classmethod
897897 def _empty_init (cls ):
@@ -917,8 +917,8 @@ def Ok(self):
917917 # Have to operate on new instance for debugpy, otherwise the Locals inspection will convert self to Err.
918918 # old method: self.add_Err_msg("Result.Ok attribute for Err variant")
919919 err = ResultErr (self ._val )
920- err .append ("Result.Ok attribute for Err variant" )
921- raise err
920+ err .append ("Result.Ok attribute for Err variant" , _levels = - 3 )
921+ raise err # Ok attribute accessed while in Err state
922922 return self ._val
923923
924924 @property
@@ -931,7 +931,7 @@ def Err(self) -> ResultErr:
931931 if self ._success :
932932 # Result.Err raises error for Ok variant
933933 # Have to operate on new instance for debugpy, otherwise the Locals inspection will convert self to Err.
934- raise ResultErr ("Result.Err attribute for Ok variant" )
934+ raise ResultErr ("Result.Err attribute for Ok variant" , _levels = - 3 )
935935 return self ._val
936936
937937 @property
@@ -948,27 +948,27 @@ def unwrap(self):
948948 def unwrap_or (self , default ):
949949 return self ._val if self ._success else default
950950
951- def expect (self , error_msg = "" ):
951+ def expect (self , error_msg = "" , * , _levels = - 4 ):
952952 if self ._success :
953953 return self ._val
954- self .add_Err_msg ("Result.expect for Err variant" )
954+ self .add_Err_msg ("Result.expect() for Err variant" , _levels = _levels )
955955 self .raises (False , error_msg )
956956
957- def expect_Err (self , ok_msg = "" ):
957+ def expect_Err (self , ok_msg = "" , * , _levels = - 4 ):
958958 if not self ._success :
959959 return self ._val
960- self .add_Err_msg ("Result.expect_err for Ok variant" )
960+ self .add_Err_msg ("Result.expect_err() for Ok variant" , _levels = _levels )
961961 self .raises (False , ok_msg )
962962
963- def raises (self , add_traceback : bool = True , error_msg = "" , * , _levels = - 5 ):
963+ def raises (self , add_traceback : bool = True , error_msg = "" , * , _levels = - 4 ):
964964 if self ._success :
965965 return self
966966 if error_msg != "" :
967967 self .add_Err_msg (error_msg , add_traceback , _levels = _levels )
968968 else :
969969 self .add_Err_msg ("Result.raises() on Err" , add_traceback , _levels = _levels )
970970 #
971- raise self ._val # Result. Err variant raises an exception
971+ raise self ._val # Err variant raises exception
972972
973973 def is_Ok_and (self , bool_ok_func , * args , ** kwargs ) -> bool :
974974 return self ._success and bool_ok_func (self ._val , * args , ** kwargs )
@@ -978,11 +978,11 @@ def apply(self, ok_func, *args, **kwargs):
978978 try :
979979 return Result (ok_func (self ._val , * args , ** kwargs ))
980980 except Exception as e :
981- err = Result .as_Err ("Result.apply exception" )
981+ err = Result .as_Err ("Result.apply exception" , _levels = - 6 )
982982 err .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
983983 else :
984984 err = Result (self ._val )
985- err .add_Err_msg ("Result.apply on Err" )
985+ err .add_Err_msg ("Result.apply on Err" , _levels = - 4 )
986986 return err
987987
988988 def apply_or (self , default , ok_func , * args , ** kwargs ):
@@ -1012,6 +1012,7 @@ def apply_or_else(self, err_func, ok_func, *args, **kwargs):
10121012 except Exception as e :
10131013 err = Result .as_Err (
10141014 "Result.apply_or_else ok_func(value), err_func(value), and err_func(error) raised exceptions" ,
1015+ _levels = - 6 ,
10151016 )
10161017 err .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
10171018 return err
@@ -1023,7 +1024,7 @@ def apply_Err(self, err_func, *args, **kwargs):
10231024 return Result (err_func (self ._val , * args , ** kwargs ))
10241025 except Exception as e :
10251026 err = self .copy ()
1026- err .add_Err_msg ("Result.apply_err exception" )
1027+ err .add_Err_msg ("Result.apply_err exception" , _levels = - 4 )
10271028 err .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
10281029 return err
10291030
@@ -1036,7 +1037,7 @@ def apply_map(self, ok_func, unwrap: bool = False):
10361037 return Result (list (map (ok_func , self ._val )))
10371038 return Result ([ok_func (self ._val )])
10381039 except Exception as e :
1039- err = Result .as_Err ("Result.apply_map exception" )
1040+ err = Result .as_Err ("Result.apply_map exception" , _levels = - 6 )
10401041 err .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
10411042 else :
10421043 err = Result (self ._val )
@@ -1047,7 +1048,7 @@ def map(self, ok_func):
10471048 if self ._success :
10481049 return Result (ok_func (self ._val ))
10491050 res = Result (self ._val )
1050- res .add_Err_msg ("Result.map on Err" )
1051+ res .add_Err_msg ("Result.map on Err" , _levels = - 4 )
10511052 return res
10521053
10531054 def map_or (self , default , ok_func ):
@@ -1101,7 +1102,7 @@ def Err_msg_contains(self, sub_msg: str) -> bool:
11011102 """
11021103 return False if self ._success else self ._val .contains (sub_msg )
11031104
1104- def add_Err_msg (self , error_msg , add_traceback : bool = True , * , _levels = - 4 ):
1105+ def add_Err_msg (self , error_msg , add_traceback : bool = True , * , _levels = - 3 ):
11051106 """Convert to error status and append error message."""
11061107 if self ._success :
11071108 if error_msg == "" and self ._val == "" :
@@ -1161,7 +1162,7 @@ def str(self, result_repr: bool = False, value_repr: bool = False) -> str:
11611162 return f"Ok({ val } )"
11621163 return f'Err("{ " | " .join (f"{ m } " for m in self ._val .msg if m != "" )} ")'
11631164
1164- def _operator_overload_prep (self , b , operation : str ):
1165+ def _operator_overload_prep (self , b , operation : str , * , _levels = - 5 ):
11651166 # Checks and returns:
11661167 # a.err and b.err -> True and a&b error
11671168 # b.err -> True and b error
@@ -1174,27 +1175,27 @@ def _operator_overload_prep(self, b, operation: str):
11741175 if isinstance (b , Result ):
11751176 if not self ._success and not b ._success :
11761177 err = Result (self )
1177- err .add_Err_msg (f"{ operation } with a and b as Err" )
1178+ err .add_Err_msg (f"{ operation } with a and b as Err" , _levels = _levels )
11781179 return True , err
11791180 if not b ._success :
11801181 err = Result (b )
1181- err .add_Err_msg (f"{ operation } with b as Err" )
1182+ err .add_Err_msg (f"{ operation } with b as Err" , _levels = _levels )
11821183 return True , err
11831184 if self ._success :
11841185 return False , b ._val # no error
11851186
11861187 if not self ._success :
11871188 err = Result (self )
1188- err .add_Err_msg (f"{ operation } with a as Err" )
1189+ err .add_Err_msg (f"{ operation } with a as Err" , _levels = _levels )
11891190 return True , err
11901191 return False , b # no error
11911192
1192- def _operator_overload_error (self , e , operation : str , apply_to_self : bool ):
1193+ def _operator_overload_error (self , e , operation : str , apply_to_self : bool , * , _levels = - 5 ):
11931194 if apply_to_self :
1194- self .add_Err_msg (f"{ operation } resulted in an Exception" )
1195+ self .add_Err_msg (f"{ operation } resulted in an Exception" , _levels = _levels )
11951196 self .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
11961197 return self
1197- err = Result (EMPTY_ERROR_MSG , False , f"{ operation } resulted in an Exception" , _levels = - 5 )
1198+ err = Result (EMPTY_ERROR_MSG , False , f"{ operation } resulted in an Exception" , _levels = _levels )
11981199 err .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
11991200 return err
12001201
@@ -1207,7 +1208,7 @@ def __repr__(self):
12071208 def __len__ (self ):
12081209 if self ._success :
12091210 return len (self ._val )
1210- self .add_Err_msg ("len(Err) not allowed" )
1211+ self .add_Err_msg ("len(Err) not allowed" , _levels = - 4 )
12111212 return 0
12121213
12131214 def __bool__ (self ):
@@ -1269,15 +1270,17 @@ def __getattr__(self, name):
12691270 f"Result.{ name } is a possible case mistake. Did you mean Result.{ ATTRIBUTES_MISTAKES [name ]} instead?"
12701271 f" Or did you forget () on a method or put () on an attrib."
12711272 f" If Ok(x.{ name } ) is what you want, then do Ok(x).expect().{ name } " ,
1273+ _levels = - 4 ,
12721274 )
12731275 elif name in EXCLUDE_ATTRIBUTES :
12741276 self .add_Err_msg (
12751277 f"{ name } is an excluded attribute/method."
12761278 f" Did you forget () on a method or put () on an attrib."
12771279 f" If Ok(x.{ name } ) is what you want, then do Ok(x).expect().{ name } " ,
1280+ _levels = - 4 ,
12781281 )
12791282 elif self .is_Err :
1280- self .add_Err_msg (f"VAR.{ name } with VAR as Err variant" )
1283+ self .add_Err_msg (f"VAR.{ name } with VAR as Err variant" , _levels = - 4 )
12811284
12821285 self .raises ()
12831286
@@ -1292,21 +1295,24 @@ def method(*args, **kwargs):
12921295 try :
12931296 return Result (attr (* args , ** kwargs ))
12941297 except Exception as e :
1295- self .add_Err_msg (f"VAR.{ name } () raises { e } " )
1298+ self .add_Err_msg (f"VAR.{ name } () raises { e } " , _levels = - 4 )
12961299 return self
12971300
12981301 return method
12991302 if isinstance (attr , Result ):
13001303 return attr
13011304 return Result (attr )
13021305 except AttributeError :
1303- self .add_Err_msg (f"VAR.{ name } raises an AttributeError" )
1306+ self .add_Err_msg (f"VAR.{ name } raises an AttributeError" , _levels = - 4 )
13041307 return self
13051308
13061309 def __getitem__ (self , key ): # index return, a[index]
13071310 if not self ._success :
13081311 err = Result (self )
1309- err .add_Err_msg (f"Err()[{ key } ] is not subscriptable" )
1312+ if isinstance (key , str ):
1313+ err .add_Err_msg (f'Err()["{ key } "] is not subscriptable' , _levels = - 4 )
1314+ else :
1315+ err .add_Err_msg (f"Err()[{ key } ] is not subscriptable" , _levels = - 4 )
13101316 return err
13111317 try :
13121318 return Result (self ._val [key ])
@@ -1322,9 +1328,9 @@ def __setitem__(self, key, value): # set from index, a[index] = XYZ
13221328 try :
13231329 self ._val [key ] = value
13241330 except Exception as e :
1325- self .add_Err_msg (f"Ok()[{ key } ]=value raises { e } " )
1331+ self .add_Err_msg (f"Ok()[{ key } ]=value raises { e } " , _levels = - 4 )
13261332 else :
1327- self .add_Err_msg (f"Err()[{ key } ] is not subscriptable" )
1333+ self .add_Err_msg (f"Err()[{ key } ] is not subscriptable" , _levels = - 4 )
13281334
13291335 def __iter__ (self ):
13301336 return self .iter_wrap ()
@@ -1336,7 +1342,7 @@ def __iadd__(self, other): # addition with assignment, a += b
13361342 op = "a += b"
13371343 errored , other = self ._operator_overload_prep (other , op )
13381344 if errored :
1339- self .add_Err_msg (other )
1345+ self .add_Err_msg (other , _levels = - 4 )
13401346 return self
13411347 try :
13421348 self ._val += other
@@ -1368,7 +1374,7 @@ def __isub__(self, other): # subtraction with assignment, a -= b
13681374 op = "a -= b"
13691375 errored , other = self ._operator_overload_prep (other , op )
13701376 if errored :
1371- self .add_Err_msg (other )
1377+ self .add_Err_msg (other , _levels = - 4 )
13721378 return self
13731379 try :
13741380 self ._val -= other
@@ -1400,7 +1406,7 @@ def __imul__(self, other): # multiplication with assignment, a *= b
14001406 op = "a *= b"
14011407 errored , other = self ._operator_overload_prep (other , op )
14021408 if errored :
1403- self .add_Err_msg (other )
1409+ self .add_Err_msg (other , _levels = - 4 )
14041410 return self
14051411 try :
14061412 self ._val *= other
@@ -1432,7 +1438,7 @@ def __itruediv__(self, other): # division with assignment, a /= b
14321438 op = "a /= b"
14331439 errored , other = self ._operator_overload_prep (other , op )
14341440 if errored :
1435- self .add_Err_msg (other )
1441+ self .add_Err_msg (other , _levels = - 4 )
14361442 return self
14371443 try :
14381444 self ._val /= other
@@ -1464,7 +1470,7 @@ def __ifloordiv__(self, other): # floor division with assignment, a //= b
14641470 op = "a //= b"
14651471 errored , other = self ._operator_overload_prep (other , op )
14661472 if errored :
1467- self .add_Err_msg (other )
1473+ self .add_Err_msg (other , _levels = - 4 )
14681474 return self
14691475 try :
14701476 self ._val //= other
@@ -1496,7 +1502,7 @@ def __imod__(self, other): # modulus with assignment, a %= b
14961502 op = "a %= b"
14971503 errored , other = self ._operator_overload_prep (other , op )
14981504 if errored :
1499- self .add_Err_msg (other )
1505+ self .add_Err_msg (other , _levels = - 4 )
15001506 return self
15011507 try :
15021508 self ._val %= other
@@ -1528,7 +1534,7 @@ def __ipow__(self, other): # exponentiation with assignment, a **= b
15281534 op = "a **= b"
15291535 errored , other = self ._operator_overload_prep (other , op )
15301536 if errored :
1531- self .add_Err_msg (other )
1537+ self .add_Err_msg (other , _levels = - 4 )
15321538 return self
15331539 try :
15341540 self ._val **= other
@@ -1560,7 +1566,7 @@ def __imatmul__(self, other): # matrix multiplication with assignment, a @= b
15601566 op = "a @= b"
15611567 errored , other = self ._operator_overload_prep (other , op )
15621568 if errored :
1563- self .add_Err_msg (other )
1569+ self .add_Err_msg (other , _levels = - 4 )
15641570 return self
15651571 try :
15661572 self ._val @= other
@@ -1699,54 +1705,54 @@ def __abs__(self): # To get called by built-in abs() method to for absolute val
16991705 try :
17001706 return Result (abs (self ._val ))
17011707 except Exception as e :
1702- self .add_Err_msg ("Result(abs(a)) resulted in an Exception" )
1708+ self .add_Err_msg ("Result(abs(a)) resulted in an Exception" , _levels = - 4 )
17031709 self .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
17041710 else :
1705- self .add_Err_msg ("int( Result(a)) with a as Err" )
1711+ self .add_Err_msg ("Result(abs( a)) with a as Err" , _levels = - 4 )
17061712 return self
17071713
17081714 def __neg__ (self ): # negation, -a
17091715 if self ._success :
17101716 try :
17111717 return Result (- self ._val )
17121718 except Exception as e :
1713- self .add_Err_msg ("Result(-a) resulted in an Exception" )
1719+ self .add_Err_msg ("Result(-a) resulted in an Exception" , _levels = - 4 )
17141720 self .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
17151721 else :
1716- self .add_Err_msg ("int( Result(a) ) with a as Err" )
1722+ self .add_Err_msg ("Result(-a ) with a as Err" )
17171723 return self
17181724
17191725 def __pos__ (self ): # unary positive, +a
17201726 if self ._success :
17211727 try :
17221728 return Result (+ self ._val )
17231729 except Exception as e :
1724- self .add_Err_msg ("Result(+a) resulted in an Exception" )
1730+ self .add_Err_msg ("Result(+a) resulted in an Exception" , _levels = - 4 )
17251731 self .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
17261732 else :
1727- self .add_Err_msg ("int( Result(a)) with a as Err" )
1733+ self .add_Err_msg ("Result(+a) with a as Err" , _levels = - 4 )
17281734 return self
17291735
17301736 def __int__ (self ): # To get called by built-in int() method to convert a type to an int.
17311737 if self ._success :
17321738 try :
17331739 return Result (int (self ._val ))
17341740 except Exception as e :
1735- self .add_Err_msg ("Result(int(a)) resulted in an Exception" )
1741+ self .add_Err_msg ("Result(int(a)) resulted in an Exception" , _levels = - 4 )
17361742 self .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
17371743 else :
1738- self .add_Err_msg ("int(Result(a)) with a as Err" )
1744+ self .add_Err_msg ("int(Result(a)) with a as Err" , _levels = - 4 )
17391745 return self
17401746
17411747 def __float__ (self ): # To get called by built-in float() method to convert a type to float.
17421748 if self ._success :
17431749 try :
17441750 return Result (float (self ._val ))
17451751 except Exception as e :
1746- self .add_Err_msg ("Result(float(a)) resulted in an Exception" )
1752+ self .add_Err_msg ("Result(float(a)) resulted in an Exception" , _levels = - 4 )
17471753 self .add_Err_msg (f"{ type (e ).__name__ } : { e } " , False )
17481754 else :
1749- self .add_Err_msg ("float(Result(a)) with a as Err" )
1755+ self .add_Err_msg ("float(Result(a)) with a as Err" , _levels = - 4 )
17501756 return self
17511757
17521758 def __lt__ (self , other ): # compare self < other.
@@ -1829,8 +1835,8 @@ class Err:
18291835 >>> print(error.unwrap()) # Outputs: ResultErr("Error message")
18301836 """
18311837
1832- def __new__ (self , error_msg , add_traceback = True ):
1833- return Result (EMPTY_ERROR_MSG , False , error_msg , add_traceback , _levels = - 4 )
1838+ def __new__ (self , error_msg , add_traceback = True , * , _levels = - 5 ):
1839+ return Result (EMPTY_ERROR_MSG , False , error_msg , add_traceback , _levels = _levels )
18341840
18351841
18361842# %% -----------------------------------------------------------------------------------------------
0 commit comments