Python IDE未在动态生成的类中检测方法
我正在为RESTAPI构建一个连接器,这个api有10多个端点,它们需要完全相同类型的函数。我的目标是构建一个类,在这个类中,我可以通过它的一个实例访问所有这些方法,如果我只创建了这样一个函数,这将很容易:Python IDE未在动态生成的类中检测方法,python,visual-studio-code,pycharm,Python,Visual Studio Code,Pycharm,我正在为RESTAPI构建一个连接器,这个api有10多个端点,它们需要完全相同类型的函数。我的目标是构建一个类,在这个类中,我可以通过它的一个实例访问所有这些方法,如果我只创建了这样一个函数,这将很容易: pairs = {"getCategory","/rest/catalog/category"} def query_type2(query_type,id): assert isinstance(id, int), "Invalid
pairs = {"getCategory","/rest/catalog/category"}
def query_type2(query_type,id):
assert isinstance(id, int), "Invalid ID, must be an integer"
assert query_type in pairs, "Invalid query type"
return requests.get(
self.chosen_endpoint + pairs[query_type] + "/" + str(id),
params={"_format": "json"},
headers={"Authorization": self.api_key},
)
manager = Connector()
id = 5000
manager.getCategory(id)
但我的目标是构建一个类,我可以像这样访问它:
pairs = {"getCategory","/rest/catalog/category"}
def query_type2(query_type,id):
assert isinstance(id, int), "Invalid ID, must be an integer"
assert query_type in pairs, "Invalid query type"
return requests.get(
self.chosen_endpoint + pairs[query_type] + "/" + str(id),
params={"_format": "json"},
headers={"Authorization": self.api_key},
)
manager = Connector()
id = 5000
manager.getCategory(id)
我已经动态地将这些方法归于类,但问题是pycharm和visualstudio代码都无法检测到这些方法,即使它们可以工作
我对这个问题的看法是:
def createTemplateFunctionType2(endpoint):
def function_template(self, id: int):
assert isinstance(id, int), "Invalid ID, must be an integer"
return requests.get(
self.chosen_endpoint + endpoint + "/" + str(id),
params={"_format": "json"},
headers={"Authorization": self.api_key},
)
return function_template
setattr(CatalogMixin, 'getCategory', createTemplateFunctionType2('/rest/catalog/category'))