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

Incorrect lloc count #262

Open
shabi9605 opened this issue Feb 26, 2024 · 2 comments · May be fixed by #263
Open

Incorrect lloc count #262

shabi9605 opened this issue Feb 26, 2024 · 2 comments · May be fixed by #263

Comments

@shabi9605
Copy link

I'm getting incorrect lloc count, when a dictionary is in the code.

def sample_dict():
    a = {'user1': 'password1', 'user2': 'password2'}
    print(a)
    return a

I'm calculating lloc of this function with this below command :-
'radon raw 'path' -s'
Result:-
LOC: 4
LLOC: 5
SLOC: 4
Comments: 0
Single comments: 0
Multi: 0
Blank: 0
- Comment Stats
(C % L): 0%
(C % S): 0%
(C + M % L): 0%

In this result lloc is incorrect , there is only 4 lloc. If I remove the dictionary, then the lloc will be correct. I think, here the issue is colon is present in the dictionary.

@shabi9605 shabi9605 changed the title Incerrect lloc count Incorrect lloc count Feb 26, 2024
@devdanzin
Copy link
Contributor

devdanzin commented Feb 26, 2024

Confirmed, and you have identified the right reason.

A simple (but not perfect) fix would be to, besides checking for a colon, also check for a compound statement keyword on the line. That would fix the provided example.

So instead of:

return 2 - (token_pos == len(processed) - 2)

We'd have something like(untested):

            # 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

Where COMPOUND_KEYWORDS is:

COMPOUND_KEYWORDS = {"if", "while", "for", "try", "with", "match", "def", "class", "else", "elif", "except", "finally", "case"}

That still leaves at least one issue: both match and case are soft keywords, so we might get wrong lloc counts for something like:

case = {1: 2}
match = {3: 4}

It seems that a line with a soft keyword match always ends with a colon, so it would be possible to check for that. But a line with a case can have further statements, so it might be trickier to get right.

Edited to add: It would also be necessary to decide on how to treat lambda: currently it counts as two logical lines because it has a colon. Should it? And if it's two logical lines, how do we count the following:

def a(b=lambda x: 1):
    pass

@devdanzin
Copy link
Contributor

I've added a PR to fix this, #263. It doesn't consider lambda x: y as two logical lines, as it would make a mess of counting lambda x: lambda y: lambda z: 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants