
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
75 lines
1.9 KiB
Plaintext
75 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[]
|