invalid-length-returned / E0303ΒΆ

Message emitted:

__len__ does not return non-negative integer

Description:

Used when a __len__ method returns something which is not a non-negative integer

Problematic code:

class FruitBasket:
    def __init__(self, fruits):
        self.fruits = ["Apple", "Banana", "Orange"]

    def __len__(self):  # [invalid-length-returned]
        return -len(self.fruits)

Correct code:

class FruitBasket:
    def __init__(self, fruits):
        self.fruits = ["Apple", "Banana", "Orange"]

    def __len__(self):
        return len(self.fruits)

Created by the classes checker.