confusing error for bad timeout value
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 54.3k
- Forks
- 10.4k
- Avg merge
- 16h 43m
- Merged PRs (30d)
- 3
Description
Calling requests.get(...., timeout=X) with a bad value for X causes urllib3 to throw an exception.
If X is a tuple, then requests raises its own exception too (good!).
If not, then requests doesn't say that it got a bad timeout value.
#5017 suggested a fix, but was closed
Expected Result
These two lines should both raise an exception in requests:
get("https://random.org", timeout=(True, True))
get("https://random.org", timeout=True)
Actual Result
The first line outputs:
Traceback (most recent call last):
File "..../requests/adapters.py", line 425, in send
timeout = TimeoutSauce(connect=connect, read=read)
File "..../urllib3/util/timeout.py", line 94, in __init__
self._connect = self._validate_timeout(connect, 'connect')
File "..../urllib3/util/timeout.py", line 121, in _validate_timeout
raise ValueError("Timeout cannot be a boolean value. It must "
ValueError: Timeout cannot be a boolean value. It must be an int, float or None.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 3, in <module>
get("https://random.org", timeout=(True, True))
File "..../requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "..../requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "..../requests/sessions.py", line 537, in request
resp = self.send(prep, **send_kwargs)
File "..../requests/sessions.py", line 650, in send
r = adapter.send(request, **kwargs)
File "..../requests/adapters.py", line 431, in send
raise ValueError(err)
ValueError: Invalid timeout (True, True). Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same value
The second outputs:
Traceback (most recent call last):
File "test.py", line 3, in <module>
get("https://random.org", timeout=True)
File "..../requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "..../requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "..../requests/sessions.py", line 537, in request
resp = self.send(prep, **send_kwargs)
File "..../requests/sessions.py", line 650, in send
r = adapter.send(request, **kwargs)
File "..../requests/adapters.py", line 435, in send
timeout = TimeoutSauce(connect=timeout, read=timeout)
File "..../urllib3/util/timeout.py", line 94, in __init__
self._connect = self._validate_timeout(connect, 'connect')
File "..../urllib3/util/timeout.py", line 121, in _validate_timeout
raise ValueError("Timeout cannot be a boolean value. It must "
ValueError: Timeout cannot be a boolean value. It must be an int, float or None.
Reproduction Steps
import requests
get("https://random.org", timeout=True)
System Information
$ python -m requests.help
{
"chardet": {
"version": "3.0.4"
},
"cryptography": {
"version": "1.3.2"
},
"idna": {
"version": "2.6"
},
"implementation": {
"name": "CPython",
"version": "3.5.1"
},
"platform": {
"release": "15.0.0",
"system": "Darwin"
},
"pyOpenSSL": {
"openssl_version": "1000208f",
"version": "16.0.0"
},
"requests": {
"version": "2.22.0"
},
"system_ssl": {
"version": "1000212f"
},
"urllib3": {
"version": "1.22"
},
"using_pyopenssl": true
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in requests/adapters.py, following the timeout handling shown in the traceback, and review the related request path in sessions.py. Reproduce the two timeout calls and add regression coverage so both invalid values raise the intended requests error rather than exposing urllib3 validation directly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100