小编Xof*_*rif的帖子

Mypy 抛出错误“缺少返回语句”,但我看不出我在哪里缺少它

我正在尝试在 http_requests 中实现重试功能,但我遇到了“需要”返回语句的问题,尽管我无法弄清楚它应该在哪里

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 …
Run Code Online (Sandbox Code Playgroud)

python python-requests mypy

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

mypy ×1

python ×1

python-requests ×1