86

I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:

C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)

Having read this answer I understand why it's doing this: it thinks the variable is a constant and applies the constant regex. However, I beg to differ: I think it's a variable. How do I tell PyLint that, so it doesn't complain? How does PyLint determine whether it's a variable or a constant - does it just treat all module-level variables as constants?

Community
  • 1
  • 1
EMP
  • 59,148
  • 53
  • 164
  • 220

6 Answers6

111
# pylint: disable-msg=C0103

Put it in the scope where you want these warnings to be ignored. You can also make the above an end-of-line comment, to disable the message only for that line of code.

IIRC it is true that pylint interprets all module-level variables as being 'constants'.

newer versions of pylint will take this line instead

# pylint: disable=C0103
studioj
  • 1,300
  • 17
  • 32
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • 9
    Thanks. I also added `# pylint: enable-msg=C0103` afterwards so that the rest of the code still gets checked. – EMP Dec 11 '09 at 03:13
  • 3
    Where do I put this code in order to disable this error message in all my Python source files? – renatov Apr 21 '14 at 16:29
  • 1
    `IIRC it is true that pylint interprets all module-level variables as being 'constants'.` - Why still they didn't make the Pylint to differentiate the `variable` & `constants`? Is there any technical challenge is there?. – Jai K Nov 08 '19 at 06:14
  • @JaiK well, there *is no* technical difference between constants and (other) variables in Python, so I couldn't think of any better way to distinguish the two... – lenz Sep 10 '20 at 10:09
  • @lenz How about not assuming a variable is a constant if the code has multiple assignments to it? – gherson Oct 08 '21 at 20:07
  • @gherson yeah, that might be a nice heuristic: If it's only assigned once, then it's a constant and should be all-caps (and vice versa). If it's assigned to multiple times it's a variable – but at the module level this would mean it's a global variable, which is something linters complain about anyway. – lenz Oct 08 '21 at 20:26
24

You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg:

[BASIC]
good-names=_log
AdamR
  • 460
  • 4
  • 9
20

Seems to me a bit of refactor might help. Pylint in looking at this as a module, so it would be reasonable not to expect to see variables at this level. Conversely it doesn't complain about vars in classes or functions. The following paradigm seems quite common and solves the issue:

def main():
    '''Entry point if called as an executable'''
    _log = MyLog()  # . . .

if __name__ == '__main__':
    main()

This has the benefit that if you have some useful classes, I can import them without running your main. The __name__ is that of the module so the "if" fails.

ryszards0.02c
  • 225
  • 2
  • 2
12

In newer versions of pylint this line is now

# pylint: disable=C0103

the enable message is as simple

# pylint: enable=C0103
studioj
  • 1,300
  • 17
  • 32
3

As other answers have indicated you can disable a specific PyLint warning (such C0103) as by including the following line:

# pylint: disable=C0103

but this generates the Locally disabling invalid-name warning. Note that this secondary warning could be useful if you want to be reminded of the disabled warning. If you want to disable the warning silently without altering your config file (which would disable the warning globally) you can use:

# pylint: disable=I0011,C0103

Note that PyLint does not issue a warning that you are disabling I0011!

banbh
  • 1,331
  • 1
  • 13
  • 31
2

If you disable a message locally in your file then Pylint will report another different warning!

Locally disabling invalid-name (C0103) [I:locally-disabled] 

If your intention is for a clean lint run, and surely that should be the target otherwise why are you bothering, then you can disable that message and the corresponding locally-enabled message in your configuration file:

disable=locally-disabled, locally-enabled
RobG
  • 744
  • 6
  • 16