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

dputhier picture dputhier · Jan 11, 2019 · Viewed 8.1k times · Source

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

Answer

Scott Skiles picture Scott Skiles · Jan 11, 2019

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!