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

Rule S5722: Special methods should have an expected number of parameters

    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

      Sources to see every method definition

      Methods with the expected number of parameters:

      • Two parameters including the first class argument: __class_getitem__
      • Only the "self" parameter: __del__, __repr__, __str__, __bytes__, __hash__, __bool__, __dir__, __len__, __length_hint__, __iter__, __reversed__, __neg__, __pos__, __abs__, __invert__, __complex__, __int__, __float__, __index__, __trunc__, __floor__, __ceil__, __enter__, __await__, __aiter__, __anext__, __aenter__, __getnewargs_ex__, __getnewargs__, __getstate__, __reduce__, __copy__, __unicode__, __nonzero__, __fspath__
      • "self" + 1 parameter: __format__, __lt__, __le__, __eq__, __ne__, __gt__, __ge__, __getattr__, __getattribute__, __delattr__, __delete__, __instancecheck__, __subclasscheck__, __getitem__, __missing__, __delitem__, __contains__, __add__, __sub__, __mul__, __matmul__, __truediv__, __floordiv__, __mod__, __divmod__, __lshift__, __rshift__, __and__, __xor__, __or__, __radd__, __rsub__, __rmul__, __rmatmul__, __rtruediv__, __rfloordiv__, __rmod__, __rdivmod__, __rlshift__, __rrshift__, __rand__, __rxor__, __ror__, __iadd__, __isub__, __imul__, __imatmul__, __itruediv__, __ifloordiv__, __imod__, __ilshift__, __irshift__, __iand__, __ixor__, __ior__, __round__, __setstate__, __reduce_ex__, __deepcopy__, __cmp__, __div__
      • "self" + 2 parameters: __setattr__, __get__, __set__, __setitem__, __set_name__, __pow__, __rpow__, __ipow__
      • "self" + 3 parameters: __exit__, __aexit__

      Optional parameters should be accepted even if they are optional. Example __pow__ accepts 1 mandatory parameter and 1 optional parameter, thus any implementation of this method should accept these two parameters or calling pow(myvar, power, modulo) will fail.

      Special methods which are out of scope for this rule: __new__, __init__, __call__, __init_subclass__
      These methods have no maximum number of parameters and require at minimum a "self" or "cls" parameter. Missing a "self" or "cls" parameter is already covered by RSPEC-5719.

      This rule won't raise for a missing "self" or "cls" parameter. It will raise in every other case when there are too many or not enough arguments.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Due:
                Created:
                Updated:
                Resolved: