11

I'm trying to implement a retry function in http_requests, but I'm running into problems with a 'needed' return statement, although I cant figure out where this should be

def _http_request(method: _HttpMethod, url: str, **kwargs) -> Optional[requests.models.Response]:
    _body: Any = None
    _retries: Any = 3
    if "json" in kwargs:
        _body = kwargs.get("json")
    elif "data" in kwargs:
        _body = kwargs.get("data")
    elif "retries" in kwargs:
        _retries = kwargs.get("retries")
    elif "timeout" in kwargs:
        _timeout = kwargs.get("timeout")

    for _retry in range(1, _retries):
        try:
            logging.debug(f"{method.value} {url} body: {_body}")
            _response = requests.request(method.value, url, **kwargs)
            logging.debug(f"{_response.status_code} body: {_response.text}")
            return _response
        except NewConnectionError:
            logging.error("Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time.")
            return None
        except requests.exceptions.ConnectionError:
            logging.error("Failed to establish a new connection: [Errno 113] No route to host.")
            return None
        except requests.exceptions.Timeout:
            if _retry <= 3:
                logging.warning(f"Connection to Jira timed out after {_timeout}, trying to connect again({_retry} of {_retries} retries)")
            else:
                logging.error(f"Failed to connect to jira server efter {_retries}")
                return None
3
  • What is the full text of the error message.
    – Andy G
    Commented Feb 14, 2019 at 9:34
  • Possibly related to stackoverflow.com/questions/47149154/…
    – match
    Commented Feb 14, 2019 at 9:35
  • Andy G -The error message is: Missing return statement _mypy(error)_ displayed at method definition level, with no indication on where it would need a return statement
    – Xoferif
    Commented Feb 14, 2019 at 9:44

1 Answer 1

16

The return statements are within the for loop, but not after it, creating an inconsistency.

Add return None outside of (after) the for loop.

The block if _retry <= 3: is also inconsistent in that it does not have a return statement, but return None after the loop may resolve the warning.

2
  • 2
    Although I cannot comprehend why MyPy cannot see the return statements in the loop, this seems to have fixed my problem, though I don't like the way it looks, but it works. (:
    – Xoferif
    Commented Feb 14, 2019 at 9:56
  • 1
    If the loop were never entered then the method would not encounter a return statement. We can see that the loop will always be entered, because _retries is given the value 3, but the parser cannot (or will not) determine this.
    – Andy G
    Commented Feb 14, 2019 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.