Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does pylint require capitalized variable names when outside a function?

Why does pylint accept capitalized variables when outside a function and reject them inside a function? Conversely, why does pylint reject camelCase ouside a function and accept it inside a function?

I just installed pylint (version 2.2.2) to check my Python 3. There must be something that I missed. My relevant Python/package versions are:

pylint 2.2.2
astroid 2.1.0
Python 3.6.7 | packaged by conda-forge | (default, Nov 20 2018, 18:20:05)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]

Consider the following code (test_1) where I'm using camelCase and Capitalized named for variables. The Capitalized variable are accepted (why ?) and camelCase rejected (because the code is not wrapped into a function, I guess).

'''
Nothing important
'''

fileHandler = open("afile.txt")

for line in fileHandler:
    Token = line.split("\t")
    Part_1 = Token[0]
    print(Part_1)

Which give upon calling pylint:

$ pylint --py3k --enable=all  test_1.py 
************* Module test_1
test_1.py:5:0: C0103: Constant name "fileHandler" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)

Now if I put everything into a function (test_2).

'''
Nothing important
'''

def foo():
    fileHandler = open("afile.txt")

    for line in fileHandler:
        Token = line.split("\t")
        Part_1 = Token[0]
        print(Part_1)

if __name__ == '__main__':
    foo()

Then the capitalized variable are detected as non compliant (which is what I expected) :

$ pylint --py3k --enable=all  test_2.py
************* Module test_2
test_2.py:5:0: C0102: Black listed name "foo" (blacklisted-name)
test_2.py:5:0: C0111: Missing function docstring (missing-docstring)
test_2.py:6:4: C0103: Variable name "fileHandler" doesn't conform to snake_case naming style (invalid-name)
test_2.py:9:8: C0103: Variable name "Token" doesn't conform to snake_case naming style (invalid-name)
test_2.py:10:8: C0103: Variable name "Part_1" doesn't conform to snake_case naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 3.75/10 (previous run: 3.75/10, +0.00)

There is something unclear for me... Any clarification welcome...

Best

like image 715
dputhier Avatar asked Jan 11 '19 17:01

dputhier


People also ask

Should Python variables be capitalized?

Part 3: Variables Variables can only contain upper and lowercase letters (Python is case-sensitive) and _ (the underscore character). Hence, because we can't have spaces in variable names a common convention is to capitalize the first letter of every word after the first. For example, myName, or debtAmountWithInterest.


2 Answers

When you put variables inside of a function pylint no longer "sees" them as constants. After putting variables inside a function pylint "sees" them as normal variables and no longer requires you to capitalize them, but instead wants "snake_case." Note, pylint prefers snake_case over camelCase by default, but you can override this in the .pylintrc to prefer camelCase.

Python Script (no method)

#!/usr/bin/env python3

# pylint wants 'FILEHANDLER'
fileHandler = open("afile.txt") # <-- pylint sees constant, wants UPPER_CASE 

for line in fileHandler:
    Token = line.split("\t")
    Part_1 = Token[0]
    print(Part_1)

With a method

#!/usr/bin/env python3

def run_stuff():

    # pylint wants 'file_handler'
    fileHandler = open("afile.txt") # <-- pylint sees normal variable

    for line in fileHandler:
        Token = line.split("\t")
        Part_1 = Token[0]
        print(Part_1)

if __name__ == '__main__':
    run_stuff()

Generally, .pylintrc files will follow PEP8. If none is provided, it will default to PEP8 as noted on the pylint website. Happy linting!

like image 113
Scott Skiles Avatar answered Sep 17 '22 20:09

Scott Skiles


I was puzzled regarding the case of capitalized variables (i.e starting with an uppercase letter, e.g. Token, Part_1) that were accepted in the for loops (see test_1) while they should have been rejected. I have posted an issue in github. This issue revealed a bug in pylint. https://github.com/PyCQA/pylint/issues/2695

like image 31
dputhier Avatar answered Sep 21 '22 20:09

dputhier