question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect getters and setters

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Feb 22, 2020

Yes 👍

1reaction
sobolevncommented, Feb 22, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Determine if a JavaScript property has a getter or setter defined?
I only want to define a getter/setter if there is not already one defined on the property. I need it to work in...
Read more >
@Getter and @Setter - Project Lombok
A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type...
Read more >
Detect getters and setters stricter · Issue #1853 - GitHub
This code currently does not produce any violations: class SomeExample(object): my_value = 1 def get_value(self) -> int: return ...
Read more >
Property getters and setters - The Modern JavaScript Tutorial
Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by get and set :.
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
Getters give you a way to define a property of an object, but they do not calculate the property's value until it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found