def get_func_in_module(module: str, qualname: str) -> Callable:
"""Return the function specified by qualname in module.
Raises:
NameLookupError if we can't find the named function
InvalidTypeError if we the name isn't a function
"""
func = get_name_in_module(module, qualname)
# TODO: Incorrect typeshed stub, stop arg should be optional
# https://github.com/python/typeshed/blob/master/stdlib/3/inspect.pyi#L213
func = inspect.unwrap(func) # type: ignore
if isinstance(func, types.MethodType):
func = func.__func__
elif isinstance(func, property):
if func.fget is not None:
if (func.fset is None) and (func.fdel is None):
func = func.fget
else:
raise InvalidTypeError(
f"Property {module}.{qualname} has setter or deleter.")
else:
raise InvalidTypeError(
f"Property {module}.{qualname} is missing getter")
elif not isinstance(func, (types.FunctionType, types.BuiltinFunctionType)):
raise InvalidTypeError(
f"{module}.{qualname} is of type '{type(func)}', not function.")
return func
评论列表
文章目录