rspec/rules/S935/python/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

72 lines
1.8 KiB
Plaintext

== Why is this an issue?
Python does not check returned types by default, however some methods are expected to return a specific type otherwise builtin functions will fail.
This rule raises an issue when a builtin function or method:
* does not return a value of the expected type.
* returns a value when no value should be returned.
* returns no value when a return value is expected.
In such way that the resulting code would result in a runtime error.
=== Noncompliant code example
[source,python]
----
class MyInt:
def __init__(self):
return self # Noncompliant. __init__ should return nothing. This will raise a TypeError.
def __int__(self):
return 3.0 # Noncompliant. __int__ should always return an integer
int(MyInt()) # This will fail with "TypeError: __int__ returned non-int (type float)"
----
=== Compliant solution
[source,python]
----
class MyInt:
def __int__(self):
return 3
int(MyInt())
----
== Resources
* https://docs.python.org/3/reference/datamodel.html#special-method-names[Python documentation - Special method names]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Return a value of type "XXX" instead of "YYY", as should any "ZZZ" method.
* Return "None" instead of a value of type "XXX", as should any "ZZZ" method.
* Return a value of type "XXX"; "ZZZ" may reach its end and return None.
=== Highlighting
* if the returned type is wrong or nothing should be returned.
** Primary: the ``++return++`` statement.
** Secondary: The function/method name.
* if no value is returned and one is expected.
** Primary: the function/method name
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]