rspec/rules/S3801/python/rule.adoc

53 lines
1.1 KiB
Plaintext

Because it is dynamically typed, Python does not enforce a return type on a function. This means that different paths through a function can return different types of values, which can be very confusing to the user and significantly harder to maintain.
In particular, it is consequently also possible to mix empty ``++return++`` statements (implicitly returning ``++None++``) with some returning an expression. This rule verifies that all the ``++return++`` statements from a function are consistent.
== Noncompliant Code Example
[source,python]
----
def foo(a): # Noncompliant, function will return "true" or None
if a == 1:
return True
return
----
== Compliant Solution
[source,python]
----
def foo(a):
if a == 1:
return True
return False
----
or
[source,python]
----
def foo(a):
if a == 1:
return True
return None
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]