diff --git a/radon/raw.py b/radon/raw.py index 1ac2bc1..6efeb6d 100644 --- a/radon/raw.py +++ b/radon/raw.py @@ -29,6 +29,22 @@ 'analyze', ] +COMPOUND_KEYWORDS = { + "if", + "while", + "for", + "try", + "with", + "match", + "def", + "class", + "else", + "elif", + "except", + "finally", + "case", +} + COMMENT = tokenize.COMMENT OP = tokenize.OP NL = tokenize.NL @@ -160,7 +176,10 @@ def aux(sub_tokens): # ENDMARKER. There are two cases: if the colon is at the end, it # means that there is only one logical line; if it isn't then there # are two. - return 2 - (token_pos == len(processed) - 2) + # Only consider colons when a compound statement is found in the line + if any(token.string in COMPOUND_KEYWORDS for token in processed): + return 2 - (token_pos == len(processed) - 2) + return 1 except ValueError: # The colon is not present # If the line is only composed by comments, newlines and endmarker diff --git a/radon/tests/test_raw.py b/radon/tests/test_raw.py index 4267a8f..f07d272 100644 --- a/radon/tests/test_raw.py +++ b/radon/tests/test_raw.py @@ -180,6 +180,38 @@ def test_find(code, result): ''', 5, ), + ( + ''' + [].sort(key=lambda x: 1) + ''', + 1, + ), + ( + ''' + [1][1:] + ''', + 1, + ), + ( + ''' + a = {'user1': 'password1', 'user2': 'password2'} + ''', + 1, + ), + ( + ''' + def a(b: str\n): pass + ''', + 2, + ), + # "case" as a variable name with a colon on the same line + # (e.g. from a dict) causes a false positive + # ( + # ''' + # case = {'user1': 'password1', 'user2': 'password2'} + # ''', + # 1, + # ), ]