while-used / W0149ΒΆ

Message emitted:

Used `while` loop

Description:

Unbounded `while` loops can often be rewritten as bounded `for` loops. Exceptions can be made for cases such as event loops, listeners, etc.

Problematic code:

import requests


def fetch_data():
    i = 1
    while i < 6:  # [while-used]
        print(f"Attempt {i}...")
        try:
            return requests.get("https://example.com/data")
        except requests.exceptions.RequestException:
            pass
        i += 1

Correct code:

import requests


def fetch_data():
    for i in range(1, 6):
        print(f"Attempt {i}...")
        try:
            return requests.get("https://example.com/data")
        except requests.exceptions.RequestException:
            pass

Configuration file:

[main]
load-plugins=pylint.extensions.while_used

Related links:

Note

This message is emitted by the optional 'while_used' checker, which requires the pylint.extensions.while_used plugin to be loaded.

Created by the while_used checker.