Search code examples
pythonhttpexceptionpython-requestspylint

Resolve a raise-missing-from Pylint exception in CQL query loop


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.


Solution

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