Skip to content

Commit b33cee5

Browse files
committed
doc: add docstr to getitem and setitem
1 parent 4742c20 commit b33cee5

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

ResultContainer/__init__.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,17 @@ class Result:
705705
If Ok variant, then raises ResultErr(ok_msg);
706706
If Err variant, then returns e in Err(e), which is type ResultErr.
707707
708+
getitem(key, default):
709+
Returns the item stored in value from Ok(value) for a given key.
710+
That is: `Ok(value)[key]` returns `Ok(value[key])`.
711+
All other errors return `Ok(default)`.
712+
713+
setitem(key, value, error_raises_exception=False):
714+
Sets the value in Ok(value) at a given key to item and return a Result.
715+
That is: `Ok(value)[key] = 99` sets `value[key] = 99` and returns the updated instance.
716+
Any error returns a new Err(e) instance, or if error_raises_exception is True
717+
raises a ResultErr exception.
718+
708719
is_Ok_and(bool_ok_func, *args, **kwargs):
709720
True if Ok(value) variant and ok_func(value, *args, **kwargs) returns True,
710721
otherwise False.
@@ -981,6 +992,19 @@ def raises(self, add_traceback: bool = True, error_msg="", *, _levels=-4):
981992
raise self._val # Err variant raises exception
982993

983994
def getitem(self, key, default):
995+
"""
996+
Returns the item stored in value from Ok(value) for a given key.
997+
That is:
998+
`Ok(value)[key]` returns `Ok(value[key])`
999+
If `Err(e)` variant or a key error, then returns `Ok(default)` instead.
1000+
1001+
Args:
1002+
key (Any): The key to retrieve the value for.
1003+
default (Any): The default value to use for the error case.
1004+
1005+
Returns:
1006+
Ok(value[key]) or Ok(default)
1007+
"""
9841008
if self._success:
9851009
try:
9861010
return Result(self._val[key])
@@ -989,6 +1013,21 @@ def getitem(self, key, default):
9891013
return Result(default)
9901014

9911015
def setitem(self, key, value, error_raises_exception=False):
1016+
"""
1017+
Sets the value in Ok(value) at a given key to item and return a Result.
1018+
That is:
1019+
`Ok(value)[key] = 99` sets `value[key] = 99` and returns the updated `Ok(value)`.
1020+
If `Err(e)` variant, then adds not subscriptable error and returns the updated `Err(e)`.
1021+
If a key error results, then returns a new instance of `Err(e)`.
1022+
1023+
Args:
1024+
key (Any): The key to retrieve the value for.
1025+
value (Any): The value to set for the given key.
1026+
error_raises_exception (bool): If true, then raises a ResultErr if `Ok(value)[key]` is invalid.
1027+
1028+
Returns:
1029+
Result: Either the original instance updated with the item, or a new `Err(e)` instance.
1030+
"""
9921031
if self._success:
9931032
try:
9941033
self._val[key] = value
@@ -1343,6 +1382,19 @@ def method(*args, **kwargs):
13431382
return self
13441383

13451384
def __getitem__(self, key): # index return, a[index]
1385+
"""
1386+
Returns the item stored in value from Ok(value) for a given key.
1387+
That is:
1388+
`Ok(value)[key]` returns `Ok(value[key])`
1389+
If `Err(e)` variant, then adds not subscriptable error and returns the updated `Err(e)`.
1390+
All other exceptions return a new instance of Err(e).
1391+
1392+
Args:
1393+
key (Any): The key to retrieve the value for.
1394+
1395+
Returns:
1396+
Ok(value[key]) or Err(e)
1397+
"""
13461398
if not self._success:
13471399
err = Result(self)
13481400
if isinstance(key, str):
@@ -1362,6 +1414,17 @@ def __getitem__(self, key): # index return, a[index]
13621414
)
13631415

13641416
def __setitem__(self, key, value): # set from index, a[index] = XYZ
1417+
"""
1418+
Sets the value in Ok(value) at a given key to item.
1419+
That is:
1420+
`Ok(value)[key] = 99` sets `value[key] = 99`.
1421+
If `Err(e)` variant, then adds a not subscriptable error.
1422+
If a key error results, then returns raises a ResultErr exception.
1423+
1424+
Args:
1425+
key (Any): The key to retrieve the value for.
1426+
value (Any): The value to set for the given key.
1427+
"""
13651428
if self._success:
13661429
try:
13671430
self._val[key] = value

0 commit comments

Comments
 (0)