rspec/rules/S1186/python/rule.adoc

92 lines
2.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
There are several reasons for a function or a method not to have a body:
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
* It is an unintentional omission, and should be fixed to prevent an unexpected behavior in production.
* It is not yet, or never will be, supported. In this case an exception should be thrown.
* The method is an intentionally-blank override. In this case a nested comment should explain the reason for the blank override.
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:47:33 +02:00
----
def myfunc1(foo="Noncompliant"):
pass
class MyClass:
def mymethod1(self, foo="Noncompliant"):
pass
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:47:33 +02:00
----
def myfunc1():
pass # comment explaining why this function is empty
def myfunc2():
raise NotImplementedError()
def myfunc3():
"""
Docstring explaining why this function is empty.
"""
class MyClass:
def mymethod1(self):
pass # comment explaining why this function is empty
def mymethod2(self):
raise NotImplementedError()
def mymethod3(self):
"""
Docstring explaining why this method is empty. Note that this is not recommended for classes
which are meant to be subclassed.
"""
----
=== Exceptions
2020-06-30 12:47:33 +02:00
2021-01-27 13:42:22 +01:00
No issue will be raised when the empty method is abstract and meant to be overriden in a subclass, i.e. it is decorated with ``++abc.abstractmethod++``, ``++abc.abstractstaticmethod++``, ``++abc.abstractclassmethod++`` or ``++abc.abstractproperty++``. Note however that these methods should normally have a docstring explaining how subclasses should implement these methods.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
[source,python]
2020-06-30 12:47:33 +02:00
----
import abc
class MyAbstractClass(abc.ABC):
@abc.abstractproperty
def myproperty(self):
pass
@abc.abstractclassmethod
def myclassmethod(cls):
pass
@abc.abstractmethod
def mymethod(self):
pass
@abc.abstractstaticmethod
def mystaticmethod():
pass
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]