while-used / W0149#

Message emitted:

Used while loop

Description:

Unbounded `while` loops can often be rewritten as bounded `for` loops.

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

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

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.