Skip to content

Commit 0cb7fa2

Browse files
committed
feat: add case attribute error check
1 parent 7819a80 commit 0cb7fa2

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

ResultContainer/ResultContainer.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@
170170
### "django/core/handlers/",
171171
### "unittest/case.py",
172172
### "pytest/",
173+
"_hooks.py",
174+
"_manager.py",
175+
"_callers.py",
173176
}
174177

175178
EXCLUDE_ATTRIBUTES = {
@@ -212,6 +215,21 @@
212215
"_g",
213216
}
214217

218+
ATTRIBUTES_MISTAKES = {
219+
"resulterr": "ResultErr",
220+
"ok": "Ok",
221+
"err": "Err",
222+
"is_ok": "is_Ok",
223+
"is_err": "is_Err",
224+
"err_msg": "Err_msg",
225+
"err_code": "Err_code",
226+
"err_traceback": "Err_traceback",
227+
"expect_err": "expect_Err",
228+
"apply_err": "apply_Err",
229+
"map_err": "map_Err",
230+
"is_ok_and": "is_Ok_and",
231+
"add_err_msg": "add_Err_msg",
232+
}
215233

216234
# %% --------------------------------------------------------------------------
217235

@@ -1318,6 +1336,13 @@ def __getattr__(self, name):
13181336
Returns:
13191337
The result of the attribute wrapped as a Result or modifies underlying value.
13201338
"""
1339+
if name in ATTRIBUTES_MISTAKES:
1340+
self.add_Err_msg(
1341+
f"Result.{name} is a possible case mistake. Did you mean Result.{ATTRIBUTES_MISTAKES[name]} instead? Or did you forget () on a method or put () on an attrib. If Ok(x.{name}) is what you want, then do Ok(x).expect().{name}",
1342+
self.error_code("Attribute"),
1343+
)
1344+
return self
1345+
13211346
if name in EXCLUDE_ATTRIBUTES:
13221347
self.add_Err_msg(
13231348
f"{name} is an excluded attribute/method. Did you forget () on a method or put () on an attrib. If Ok(x.{name}) is what you want, then do Ok(x).expect().{name}",
@@ -1340,7 +1365,7 @@ def method(*args, **kwargs):
13401365
res = attr(*args, **kwargs)
13411366
return Result(res) if res is not None else None
13421367
except Exception as e:
1343-
return Result.Err(f"VAR.{name}() raised {e}", self.error_code("Method"))
1368+
return Result.Err(f"VAR.{name}() raised {e}", self.error_code("Method"), self._g)
13441369

13451370
return method
13461371
if isinstance(attr, Result):

0 commit comments

Comments
 (0)