const std::vector<std::string>& IMethod::getArgumentNames() const
{
- // TODO(jwtan): Deal with empty parameter list.
- if (!argumentNames_.empty()) {
+ if (isArgumentNamesInitialized_) {
return argumentNames_;
}
+ isArgumentNamesInitialized_ = true;
setArgumentNames(argumentNames_);
return argumentNames_;
}
}
// Pre-registered modules.
+ // Since torch::deploy::Obj.toIValue cannot infer empty list, we hack it to
+ // return None for empty list.
// TODO(jwtan): Make the discovery of these modules easier.
register_module_source(
"GetArgumentNamesModule",
"from inspect import signature\n"
- "def getArgumentNames(function): return list(signature(function).parameters.keys())\n");
+ "from typing import Callable, Optional\n"
+ "def getArgumentNames(function: Callable) -> Optional[list]:\n"
+ " names = list(signature(function).parameters.keys())\n"
+ " if len(names) == 0:\n"
+ " return None\n"
+ " return names\n");
TORCH_DEPLOY_SAFE_CATCH_RETHROW
}
auto iArgumentNames =
session.global("GetArgumentNamesModule", "getArgumentNames")({method})
.toIValue();
+ if (iArgumentNames.isNone()) {
+ return;
+ }
+
TORCH_INTERNAL_ASSERT(iArgumentNames.isList());
auto argumentNames = iArgumentNames.toListRef();
// Try to infer the type of a Python object
// The type cannot be inferred if:
-// input is a None
// input is an empty container (list, dict)
// input is an list with element types that cannot be unified
// input is an dict with key or value types that cannot be unified