解决在CQL查询循环中出现的Pylint异常”raise-missing-from”。

huangapple go评论65阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2023年5月22日 23:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定