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

12,224

Solution 1

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

Solution 2

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!

Share:
12,224

Related videos on Youtube

dputhier
Author by

dputhier

Updated on September 21, 2022

Comments

  • dputhier
    dputhier over 1 year

    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

  • dputhier
    dputhier over 5 years
    Thanks a lot Scott for your response. I understand well the case of fileHandler that is viewed as a constant in test_1 and should be written as uppercase. My concern is particularly about 'Token and Part_1' that in test_1 should be written as 'token' and 'part_1' ? PEP8 indicates that variable name should be lowercase. So both should be detected as non compliant ? No ?
  • Scott Skiles
    Scott Skiles over 5 years
    token and part_1 should be all lowercase, not starting with any uppercase.
  • dputhier
    dputhier over 5 years
    Yes they should be lowercase. Thus why are they accepted by pylint in test_1 ?
  • Scott Skiles
    Scott Skiles over 5 years
    What are you asking? Did I answer your question?
  • dputhier
    dputhier over 5 years
    I'm talking about Token and Part_1. That is capitalized variables (i.e starting with an uppercase letter, e.g. Foo) that are accepted in for loops and should not.
  • dputhier
    dputhier over 5 years
    Sorry if my question was unclear as it mixed issues related to variables in uppercase (FOO) and starting with an uppercase (Foo) variables. Please, see my answer and the issue in github. Thank you.
  • Scott Skiles
    Scott Skiles over 5 years
    NP. Looks like your question changed a lot from you originally posted it.