2023-06-30 09:14:26 +02:00
This rule raises an issue when using an ``++assert++`` statement on a tuple literal.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-30 09:14:26 +02:00
When tested for truthiness, a sequence or collection will evaluate to `False` if it is empty (its ``++__len__++`` method returns 0) and to `True` if it contains at least one element.
2021-04-28 16:49:39 +02:00
2023-06-30 09:14:26 +02:00
Using the ``assert`` statement on a tuple literal will therefore always fail if the tuple is empty, and always succeed otherwise.
2021-04-28 16:49:39 +02:00
2023-06-30 09:14:26 +02:00
The ``++assert++`` statement does not take parentheses around its parameters. Calling ``++assert(x, y)++`` will test if the tuple ``++(x, y)++`` is True, which is always the case.
2021-04-28 16:49:39 +02:00
There are two possible fixes:
* If your intention is to test the first value of the tuple and use the second value as a message, simply remove the parentheses.
* If your intention is to check that every element of the tuple is ``++True++``, test each value separately.
2023-06-30 09:14:26 +02:00
=== Code examples
2021-04-28 18:08:03 +02:00
2023-06-30 09:14:26 +02:00
==== 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
----
def test_values(a, b):
2023-06-30 09:14:26 +02:00
assert (a, b) # Noncompliant: will always be True
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-06-30 09:14:26 +02:00
==== 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
----
def test_values(a, b):
# If you mean to test "a" and use "b" as an error message
assert a, b
# If you mean to test the values of "a" and "b"
assert a and b
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
* https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement[Python documentation - The ``++assert++`` statement]
2023-06-30 09:14:26 +02:00
* https://docs.python.org/3/library/stdtypes.html#truth-value-testing[Python documentation - Truth Value Testing]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Fix this assertion on a tuple literal; did you mean "assert A, B".
=== Highlighting
The tuple parameter
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== deprecates: S1721
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]