Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect lloc count when there's a colon in the line (fixes #262) #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion radon/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions radon/tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
# ),
]


Expand Down