60

I made a simple python script to post data on a website.

#Imports

url_to_short = sys.argv[1]

post_url = 'https://www.googleapis.com/urlshortener/v1/url'
headers = {'Content-Type': 'application/json'}

data = {'longUrl': url_to_short}
post_data = json.dumps(data)

req = urllib2.Request(post_url, post_data, headers)
resp = urllib2.urlopen(req)

if resp.getcode() == 200:  
    content = json.loads(resp.read())

#Other stuff

Now I thought lets check my script for coding standards with pylint tool.

My pylint output is as follows:

************* Module post
C:  1,0: Missing docstring
C:  6,0: Invalid name "url_to_short" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C:  8,0: Invalid name "post_url" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C:  9,0: Invalid name "headers" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)

# Other stuff

Now, my question is why pylint is showing my variable names as Invalid name. Is naming variable this way a wrong coding convention.

My complete pylint output.

RanRag
  • 48,359
  • 38
  • 114
  • 167

3 Answers3

63

As your code is not contained in a class or function it is expecting those variables to be constants and as such they should be uppercase.

You can read PEP8 for further information.

Christian Witts
  • 11,375
  • 1
  • 33
  • 46
  • 5
    More especially the [naming conventions part](http://www.python.org/dev/peps/pep-0008/#naming-conventions) of PEP8 – Cédric Julien May 30 '12 at 11:47
  • So, is it always necessary that if i write such small code snippets they should be inside a class. – RanRag May 30 '12 at 11:48
  • 2
    I wouldn't write it in a class, I would just put it in a function definition instead which is good practice for code re-use too. – Christian Witts May 30 '12 at 11:50
  • 45
    Pep 8 doesn't prohibit global variables... It would be dumb to do so because python is often used to write short scripts. This is obviously a case of pylint putting in restrictions that don't exist in PEP 8. – catphive Jun 19 '12 at 01:31
  • 5
    @catphive pylint doesn't prohibit them either, it just requires that they follow the ALL_CAPS naming convention. – This company is turning evil. Dec 11 '14 at 12:59
  • 10
    @Kroltan In the "global variables" section, PEP8 says that global variables should be formatted like functions, so ALL_CAPS is wrong. My guess is that it is hard for pylint to check if a variable is used as a constant or a variable, so it defaults to only allowing module-level constants since global variables are generally discouraged any way and you can disable pylint on a case-by-case basis for them. – ws_e_c421 Aug 26 '16 at 21:29
  • @catphive Restriction is brilliant - to write every program in same coding standard. – Denis Barmenkov May 17 '22 at 23:27
  • @Kroltan: And what happens if those variables aren't constants? – Eric Duminil Feb 25 '23 at 19:38
27

EDIT: As others have mentioned, pylint expects that global variables should be UPPERCASE. If the warnings really bother you, you can circumvent them by wrapping small snippets like this in a main()-function and then use the if __name__ == "__main__"-convention. Or if you care, you can modify the regular expressions that pylint uses to validate variable names.

From the developers of Pylint.

In this case Pylint is telling me that those variables appear to be constants and should be all UPPERCASE. This rule is in fact a naming convention that is specific to the folks at Logilab who created Pylint. That is the way they have chosen to name those variables. You too can create your own in-house naming conventions but for the purpose of this tutorial, we want to stick to the PEP-8 standard. In this case, the variables I declared should follow the convention of all lowercase. The appropriate rule would be something like: "should match [a-z_][a-z0-9_]{2,30}$". Notice the lowercase letters in the regular expression (a-z versus A-Z)

You can test it by running: pylint --const-rgx='[a-z_][a-z0-9_]{2,30}$' x.py

msvalkon
  • 11,887
  • 2
  • 42
  • 38
7

It's because url_to_short is declared in the global namespace, and pylint requires global variables (e.g. constants) to be named ALL_UPPERCASE.
Therefore it checks whether your variable name matches the regex used for globals, which is: (([A-Z_][A-Z0-9_]*)|(__.*__))$ (note the A-Z ranges). Hence the Invalid name error.

rubik
  • 8,814
  • 9
  • 58
  • 88