getter/setter must have 1 or 2 arguments respectively

Metadata

ID: python-best-practices/get-set-arguments

Language: Python

Severity: Warning

Category: Best Practices

Description

Ensure that getter and setter have the right number of parameters:

  • getters must have exactly one parameter (the instance we are reading from)
  • setters must have exactly two parameters (the instance we are updating and the associated value)

Learn More

Non-Compliant Code Examples

class Foo:
   @property
   def get_my_attribute(self, foo):  # getter should have only one argument
      return self.my_attribute
   
   @attr.setter
   def set_attr(self, v, bar):  # setter should have only two arguments
      self._attr = v

   @attr.deleter
   def del_attr(self, foo):  # deleter should have only one argument
      del self._attr

Compliant Code Examples

class Foo:
   def get_my_attribute(self):
      return self.my_attribute
   
   def get_my_attribute(self, foo): # Not a property or attr, valid
      return self.my_attribute

   @property
   def get_my_attribute(self):
      return self.my_attribute

   def set_my_attribute(self, v):
      self.my_attribute = v

   @attr.setter
   def set_attr(self, v):
      self._attr = v

   @attr.deleter
   def del_attr(self,):
      return self._attr
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: mervebolat/span-id-preprocessing