rspec/rules/S5845/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

76 lines
1.9 KiB
Plaintext

== Why is this an issue?
Calling ``++unittest++`` methods ``++assertEqual++``, ``++assertNotEqual++``, ``++assertIs++`` or ``++assertIsNot++`` on objects of incompatible types will always fail or always succeed.
For methods ``++assertEqual++`` and ``++assertNotEqual++``, arguments' types are incompatible if:
they are unrelated builtin types such as string and integer.
they are instances of unrelated classes which do not implement ``++__eq__++`` or ``++__ne__++`` (if a class implements one of these methods it could compare to any other type it wants).
As for methods ``++assertIs++`` and ``++assertIsNot++``, if arguments' types are different it is not possible for them to point to the same object, thus ``++assertIs++`` will always fail and ``++assertIsNot++`` will always succeed.
=== Noncompliant code example
[source,python]
----
import unittest
class A(): ...
class MyTest(unittest.TestCase):
def test_something(self):
a = A()
mydict = {"x": a}
self.assertEqual(a, "x") # Noncompliant
self.assertIs(a, "x") # Noncompliant
----
=== Compliant solution
[source,python]
----
import unittest
class A(): ...
class MyTest(unittest.TestCase):
def test_something(self):
a = A()
mydict = {"x": a}
self.assertEqual(a, mydict["x"]) # OK
self.assertIs(a, mydict["x"]) # OK
----
== Resources
* S2159 Silly equality checks should not be made
* S3403 Identity operators should not be used with dissimilar types
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Primary: Change this assertion to not compare dissimilar types ("XXX" and "YYY").
Secondary: Last assignment of "ZZZ".
=== Highlighting
Primary: the arguments of the assertXXX call.
Secondaries (if args are variables): the last assignment of the variable.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]