Wemake-python-styleguide: Forbid old-styled classes

Created on 4 Jul 2018  路  8Comments  路  Source: wemake-services/wemake-python-styleguide

In python2 we had different classes: class Some: and class Some(object): were totally different things.

Currently, we don't have this thing in python3, but since these times I believe that new-styled classes are still better.

  1. It has more semantics: this class is related to object
  2. It has unified interface: class Base(object) and class Child(Base) vs class Base and class Child(Base)
  3. Explicit is better than implicit

So, I believe we should ban classes with old-styled base class.
General rule is: always write super-classes' names

Correct

class Philosopher(object):
    def __init_subclass__(cls, default_name, **kwargs):
        ...

class AustralianPhilosopher(Philosopher, default_name="Bruce"):
    ...

class WithMetaClass(object, metaclass=MyMeta):
   ...

Wrong

class Philosopher: ...
class AustralianPhilosopher(default_name="Bruce"): ...
class WithMetaClass(metaclass=MyMeta): ...
help wanted starter rule request

All 8 comments

ezgif-2-dc2f4987da

Welcome to the most opinionated linter ever!

@AlwxSin do you want to help me with this feature?

@AlwxSin there's like three reasons in favour of that in the ticket :)

My point is that Class(object) style is legacy.
Every good python developer should know that in python everything comes from object. Otherwise he is not that good.
Why should you write about that explicitly?

That's not about the technical aspect. It is about consistency.

We have projects that share both class Some: and class Some(object): in the code base.
And that's inconsistent.

To fix that we need to stick to a single writing style. We have a choice here: new style vs old style.
And that's where I prefer old style better. By the reasons I have provided in the description.

So, now we will have consistent styles across the class definitions. Which is a good thing for us.

Disagree.

It has unified interface: class Base(object) and class Child(Base) vs class Base and class Child(Base)

In first case we can see that both classes depends on something. Both are subclasses. We should look to class Base(object): and figure out that this is base class.
In second case from the first look we can see that class Base: is base class and other are subclasses.

Both are subclasses

Yes, both classes are subclasses. The first one is a subclass of object, while the second one is a subclass of Some.

Yes, both classes are subclasses

And good developer knows that.
But you cannot clearly see the difference between these classes though.

Was this page helpful?
0 / 5 - 0 ratings