Wemake-python-styleguide: Detect getters and setters

Created on 4 Dec 2019  路  12Comments  路  Source: wemake-services/wemake-python-styleguide

Rule request

Thesis

We can inspect class and instance methods to find these pattern: if class contains two methods like get_X and set_X we should raise UnpythonicGetterSetterViolation

We should also detect cases like:

  • class has instance or class level attribute x or _x or __x
  • class has method called get_x or set_x

It is oop violation.

Reasoning

Because python does not need this abstraction:

  • Either use @property
  • Or just make the property public and change it
help wanted starter pr-available rule request

All 12 comments

Hi, can I take the issue?

Sure! @Gwin73 thanks a lot!

Feel free to ask any questions.

@Gwin73 I have updated the task description. Please, take a look 馃檪

Hello, i am part of the same group as @Gwin73 who is working on this issue. Just to clarify some things, should it be:

  • class has instance or class level attribute x or _x or __x
    AND
  • class has method called get_x or set_x?

So when both cases are satisfied we should raise UnpythonicGetterSetterViolation or should we do so when either case is satisfied?

When either case is satisfied 馃檪 (logical or)

Thanks for the quick answer! So this would mean no instance or class level attributes are allowed since x matches anything?

I mean x is an abstract field name. It can be anything. Any name.

Wait I think there must be some misunderstanding here. Instance or class level attributes x or _x or __x must be allowed right, as long as they are "public" and have no corresponding get_x or set_x methods?

Only allowing properties and NO attributes seems a bit weird.

Sorry, that I am not able to express myself clear enough 馃檪

This rule only checks method names. We need to detect patterns like get_${existing_attribute_name} and set_${existing_attribute_name} and raise violations when any of these methods are found.

Where ${existing_attribute_name} can be public, protected, or private name of instance or class level attribute.

So it should be:

  • class has instance or class level attribute x or _x or __x
    AND
  • class has method called get_x or set_x

Logical AND

Yes 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webknjaz picture webknjaz  路  3Comments

sobolevn picture sobolevn  路  4Comments

Dreamsorcerer picture Dreamsorcerer  路  3Comments

sobolevn picture sobolevn  路  5Comments

vnmabus picture vnmabus  路  4Comments