A flake8 plugin that checks for missing timeout parameters in network calls.
requests and urllib.request.urlopen do not set timeouts by default unlike httpx and aiohttp.
flake8-timeout checks requests and urllib.request.urlopen calls by default but can be configured to track any function that accepts a timeout parameter.
pip install flake8-timeout| Code | Description |
|---|---|
| TIM100 | timeout missing for request call |
The plugin tracks these functions by default:
requests.getrequests.postrequests.putrequests.deleterequests.headrequests.patchrequests.optionsrequests.requesturllib.request.urlopen(timeout at positional index 2)
See pre-commit for instructions
Sample .pre-commit-config.yaml:
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-timeout==2.0.0]Use --timeout-extend-funcs to add custom functions while keeping the defaults:
Command line:
flake8 --timeout-extend-funcs=my_http_lib.request,custom.api.call:1Pre-commit:
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-timeout==2.0.0]
args: [--timeout-extend-funcs=my_http_lib.request,custom.api.call:1]This will check the defaults plus your custom functions.
Use --timeout-funcs to replace the defaults entirely:
Command line:
flake8 --timeout-funcs=custom.http.get,custom.http.post:2Pre-commit:
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-timeout==2.0.0]
args: [--timeout-funcs=custom.http.get,custom.http.post:2]This will only check the functions you specify, ignoring the defaults.
Some functions accept timeout as a positional argument. Specify the 0-based index after a colon:
my_lib.fetch:2 # timeout is at index 2 (3rd argument)
other.call:0 # timeout is at index 0 (1st argument)
Example with positional timeout:
# my_lib.fetch(url, data, timeout)
my_lib.fetch('https://api.example.com', None, 30) # OK - timeout at index 2
my_lib.fetch('https://api.example.com', None) # TIM100 - missing timeout