From 01dfbbd58e5a3cc5ae2b8faf5388b44c128a22bc Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Sun, 19 Dec 2021 11:22:35 -0800 Subject: [PATCH] use default for getattr instead of catching error --- my/core/query.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/my/core/query.py b/my/core/query.py index 1496e74..68a88b6 100644 --- a/my/core/query.py +++ b/my/core/query.py @@ -54,15 +54,11 @@ def locate_function(module_name: str, function_name: str) -> Callable[[], Iterab for (fname, func) in inspect.getmembers(mod, inspect.isfunction): if fname == function_name: return func - try: - # incase the function is defined dynamically, - # like with a globals().setdefault(...) or a module-level __getattr__ function - func = getattr(mod, function_name) - if callable(func): - return func - except AttributeError: - # shouldn't raise an error here, just fall through - pass + # incase the function is defined dynamically, + # like with a globals().setdefault(...) or a module-level __getattr__ function + func = getattr(mod, function_name, None) + if func is not None and callable(func): + return func except Exception as e: raise QueryException(str(e)) raise QueryException(f"Could not find function '{function_name}' in '{module_name}'")