use default for getattr instead of catching error

This commit is contained in:
Sean Breckenridge 2021-12-19 11:22:35 -08:00 committed by karlicoss
parent 83725e49dd
commit 01dfbbd58e

View file

@ -54,15 +54,11 @@ def locate_function(module_name: str, function_name: str) -> Callable[[], Iterab
for (fname, func) in inspect.getmembers(mod, inspect.isfunction): for (fname, func) in inspect.getmembers(mod, inspect.isfunction):
if fname == function_name: if fname == function_name:
return func return func
try: # incase the function is defined dynamically,
# incase the function is defined dynamically, # like with a globals().setdefault(...) or a module-level __getattr__ function
# like with a globals().setdefault(...) or a module-level __getattr__ function func = getattr(mod, function_name, None)
func = getattr(mod, function_name) if func is not None and callable(func):
if callable(func): return func
return func
except AttributeError:
# shouldn't raise an error here, just fall through
pass
except Exception as e: except Exception as e:
raise QueryException(str(e)) raise QueryException(str(e))
raise QueryException(f"Could not find function '{function_name}' in '{module_name}'") raise QueryException(f"Could not find function '{function_name}' in '{module_name}'")