rspec/rules/S2734/python/rule.adoc

45 lines
1.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
By contract, every Python function returns something, even if it's the ``++None++`` value, which can be returned implicitly by omitting the ``++return++`` statement, or explicitly.
2021-04-28 16:49:39 +02:00
The ``++__init__++`` method is required to return ``++None++``. A ``++TypeError++`` will be raised if the ``++__init__++`` method either ``++yield++``s or ``++return++``s any expression other than ``++None++``. Returning some expression that evaluates to ``++None++`` will not raise an error, but is considered bad practice.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class MyClass(object):
def __init__(self):
self.message = 'Hello'
return self # Noncompliant
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class MyClass(object):
def __init__(self):
self.message = 'Hello'
----
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[]