Mypy 抛出错误 'Missing return statement',但我看不到我遗漏的地方

Mypy throws and error 'Missing return statement', but i can't see where I'm missing it

我正在尝试在 http_requests 中实现重试功能,但我 运行 遇到了 'needed' return 语句的问题,尽管我无法理解这应该是

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

return 语句在 for 循环内,但不在循环之后,造成不一致。

在 for 循环之外(之后)添加 return None

if _retry <= 3:也不一致,因为它没有return语句,但循环后return None可能会解决警告。