Uploaded image for project: 'SonarPython'
  1. SonarPython
  2. SONARPY-584

Rule S5719: Instance and class methods should have at least one parameter

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.8
    • Component/s: Rules
    • Labels:

      Description

      Note that before decorators existed, the only way to create a static method was:

      class MyClass:
          def old_style_static_method():
              print("old_style_static_method")
          old_style_static_method = staticmethod(old_style_static_method)
      

      staticmethod is used after the method is defined to replace it with a decorated version.

      We shouldn't raise any issue when the method is used as an argument to staticmethod.

      Here is a real world example.

      Also we shouldn't raise when the method is called inside the body of its class. This use case can be seen in django.

      class A:
          def without_self():  #  No issue
              return 42
          attr = without_self()  # Calling the function in the class's body
      

      No issue will be raised when the first argument is a vararg, i.e. starting with a * (ex: *args). This enables an unlimited number of positional arguments. However we should raise an issue for single * with no positional arguments, i.e. only keyword arguments.

      class A:
          def without_positional_args(*, keyword_arg):  #  Noncompliant
              return 42
          def with_vararg(*args, keyword_arg):  #  Compliant
              return 42
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              gyula.sallai Gyula Sallai
              Reporter:
              nicolas.harraudeau Nicolas Harraudeau (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: