英文:
Resolve a raise-missing-from Pylint exception in CQL query loop
问题
如何解决以下Pylint
的linting错误:
pylint:W0707:考虑明确使用 'raise SystemExit(http_error) from http_error'
这是我的代码:
try:
for label in labels:
# CQL查询以将标签名称附加到URL
self.query = {"cql": f"type=page and label='{label}'"}
self.response = requests.request(
"GET",
self.rest_url,
auth=BearerAuth(self._token),
params=self.query,
timeout=100,
)
self.response.raise_for_status()
# 一些代码
return self.page_details
except requests.exceptions.HTTPError as http_error:
raise SystemExit(http_error)
我尝试简单地打印错误,但这将会显示另一个lint错误。我不确定如何最好地处理异常。
英文:
How to resolve the following Pylint
linting error:
pylint:W0707:Consider explicitly re-raising using 'raise SystemExit(http_error) from http_error'
Here is my code:
try:
for label in labels:
# CQL query to append label name to url
self.query = {"cql": f"type=page and label='{label}'"}
self.response = requests.request(
"GET",
self.rest_url,
auth=BearerAuth(self._token),
params=self.query,
timeout=100,
)
self.response.raise_for_status()
#some Code
return self.page_details
except requests.exceptions.HTTPError as http_error:
raise SystemExit(http_error)`
I tried simply printing error instead but it will show another lint error because of it.
I am not sure what is the best way to handle exceptions.
答案1
得分: 0
The message is telling you what to do: "考虑明确地使用 'raise SystemExit(http_error) from http_error' 重新引发异常。" 即,执行以下操作:
try:
for label in labels:
# CQL query to append label name to url
self.query = {"cql": f"type=page and label='{label}'"}
self.response = requests.request(
"GET",
self.rest_url,
auth=BearerAuth(self._token),
params=self.query,
timeout=100,
)
self.response.raise_for_status()
# some Code
return self.page_details
except requests.exceptions.HTTPError as http_error:
raise SystemExit(http_error) from http_error
Checking the doc could be helpful in general too: 查看文档可能也有帮助
请让我知道如何让这个消息更清晰(我是提出这个消息的工具的维护者)。
英文:
The message is telling you what to do: Consider explicitly re-raising using 'raise SystemExit(http_error) from http_error'
. i.e. do this:
try:
for label in labels:
# CQL query to append label name to url
self.query = {"cql": f"type=page and label='{label}'"}
self.response = requests.request(
"GET",
self.rest_url,
auth=BearerAuth(self._token),
params=self.query,
timeout=100,
)
self.response.raise_for_status()
#some Code
return self.page_details
except requests.exceptions.HTTPError as http_error:
raise SystemExit(http_error) from http_error
Checking the doc could be helpful in general too: https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/raise-missing-from.html
Please let me know how I can make the message clearer (I'm the maintainer of the tool raising this message).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论