Added new feature for request timeout plugin #9525
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
The purpose of this feature is to enhance Pylint's capabilities to detect missing timeouts for requests.Session() calls in Python code. Currently, Pylint's missing-timeout warning (W3101) is triggered for requests.get() calls but not for requests.Session(). This enhancement ensures consistent timeout handling across different usage patterns of the requests library.
Motivation
Timeout settings are crucial for robust network operations, especially in scenarios where network requests may encounter delays or failures. While Pylint already provides warnings for missing timeouts in requests.get() calls, similar warnings for requests.Session() calls are equally important to maintain code quality and reliability.
Implementation Details
Custom Pylint Plugin
A custom Pylint plugin (requests_timeout_plugin.py) is introduced to extend Pylint's functionality. This plugin inspects requests.Session() calls and raises a warning if no timeout is explicitly set.
Warning Message
The new warning message (W3102) is introduced to indicate the absence of a timeout setting for requests.Session() objects. This warning prompts developers to review and add appropriate timeout settings to ensure robust network operations.
Integration with Pylint
To use this feature, developers need to run Pylint with the requests_timeout_plugin.py plugin. They can specify the plugin using the --load-plugins option when invoking Pylint.
Example
Consider the following Python code snippet:
import requests
Without timeout setting
session = requests.Session()
response = session.get('https://example.com')
With the custom Pylint plugin enabled, Pylint will raise a warning (W3102) for the requests.Session() call, indicating that no timeout is set.
also you can use the plugin like
pylint --load-plugins=pylint_plugins.requests_timeout_plugin <your_code.py>