1

Please consider this code after running pylint:

''' Test ''' i = 0 while i < 4: myvar = i i = i + 1 

pylint reports: Constant name "myvar" doesn't conform to UPPER_CASE naming style (invalid-name) But how much is myvar really a constant, when it obviously changes during the running process?

IIUC, it is not a false positive but rather myvar is regarded being a constant because it never changes during the iteration, and the next iteration the variable is regarded as "new". Did I understand it correctly?

1
  • 2
    I think I'd find it more confusing to have MYVAR. I'd say it was a false positive, but that's opinion. Commented Sep 5, 2018 at 6:44

1 Answer 1

2

Pylint thinks that myvar is a constant by convention, because it is global (declared on module level).

Generally, you should not be writing code like this on a module level, wrap it in a function instead:

def main(): i = 0 while i < 4: myvar = i i = i + 1 if __name__ == '__main__': main() 
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure it is inconceivable that you'd have some code like the OP's. In that case, this is a false positive, right?
I'd say it's more of a misleading description rather than a false positive, because the rule applies to module-level variables regardless of whether they are actually constant or not. On the other hand, I'm not sure how to correctly describe the rule in Python terms, but that would be going into too much details :)
Anyway, one generally shouldn't have changing variables on module level in production-grade code, so pylint assumes they are all constants.
I prefer your description over false positive :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.