From 8899f8b5b5dfc84e8e0481a5587d0285c6835ec5 Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Fri, 7 Jun 2019 14:17:31 -0500 Subject: Hacking N363: `in (not_a_tuple)` A few places in the code had conditionals including: if something in (element): which was clearly intended to be if something in (element,): (I.e. `in $tuple`, not `in element` with redundant parens) or just if something == element: Fix those [1] and introduce hacking rule N363 to disallow this kind of thing in the future. [1] NOTE: These weren't actually latent bugs because 'foo' in ('foo') which is the same as 'foo' in 'foo' returns True. In order to be a bug, the left operand would have to be able to be a substring of the right: 'foo' in ('foobar') # True 'foo' in ('foobar',) # False ...which I don't think is possible in any of the scenarios found. Change-Id: I950d07eb533e0d43466c58e36b314aaaf8560251 --- nova/tests/unit/test_hacking.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'nova/tests/unit/test_hacking.py') diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index aa519e91d6..00631e07b5 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -894,3 +894,27 @@ class HackingTestCase(test.NoDBTestCase): for filename in (good_filenames + bad_filenames): self._assert_has_no_errors( code, checks.privsep_imports_not_aliased, filename=filename) + + def test_did_you_mean_tuple(self): + code = """ + if foo in (bar): + if foo in ('bar'): + if foo in (path.to.CONST_1234): + if foo in ( + bar): + """ + errors = [(x + 1, 0, 'N363') for x in range(4)] + self._assert_has_errors( + code, checks.did_you_mean_tuple, expected_errors=errors) + code = """ + def in(this_would_be_weird): + # A match in (any) comment doesn't count + if foo in (bar,) + or foo in ('bar',) + or foo in ("bar",) + or foo in (set1 + set2) + or foo in ("string continuations " + "are probably okay") + or foo in (method_call_should_this_work()): + """ + self._assert_has_no_errors(code, checks.did_you_mean_tuple) -- cgit v1.2.1