too-many-ancestors / R0901ΒΆ

Message emitted:

Too many ancestors (%s/%s)

Description:

Used when class has too many parent classes, try to reduce this to get a simpler (and so easier to use) class.

Problematic code:

class Animal: ...
class BeakyAnimal(Animal): ...
class FurryAnimal(Animal): ...
class Swimmer(Animal): ...
class EggLayer(Animal): ...
class VenomousAnimal(Animal): ...
class ProtectedSpecie(Animal): ...
class BeaverTailedAnimal(Animal): ...
class Vertebrate(Animal): ...


# max of 7 by default, can be configured
# each edge of a diamond inheritance counts
class Playtypus(  # [too-many-ancestors]
    BeakyAnimal,
    FurryAnimal,
    Swimmer,
    EggLayer,
    VenomousAnimal,
    ProtectedSpecie,
    BeaverTailedAnimal,
    Vertebrate,
):
    pass

Correct code:

class Animal:
    beaver_tailed: bool
    can_swim: bool
    has_beak: bool
    has_fur: bool
    has_vertebrae: bool
    lays_egg: bool
    protected_specie: bool
    venomous: bool


class Invertebrate(Animal):
    has_vertebrae = False


class Vertebrate(Animal):
    has_vertebrae = True


class Mammal(Vertebrate):
    has_beak = False
    has_fur = True
    lays_egg = False
    venomous = False


class Playtypus(Mammal):
    beaver_tailed = True
    can_swim = True
    has_beak = True
    lays_egg = False
    protected_specie = True
    venomous = True

Created by the design checker.