0

I've been linting my python code for some time now in order to make it more Pythonian and to that effect I've been using pylint to help identify problematic code blocks. However, now I'm having a kind of weird error, where pylint is flagging a correctly formatted constant name as not conforming to the regex provided.

Orginially, the constant was named main, which should match with the regex [a-z\_][a-z0-9\_]{2,30}$, but I got the convention violation message anyway. I tried changing the constant to run_main without any change. I even tried chaning the regex to [\_][a-z0-9\_]{2,30}$|[a-z][\_][a-z0-9\_]{2,30}$but the convention violation persists. I've tried testing the expressions on several regex testing sites to make sure I was not in the wrong. Is it a bug in pylint or am I missing something obvious?

The constant is defined in the following code block:

if __name__ == "__main__":
javabridge.start_vm(class_path=bf.JARS)
run_main = mainInterface()

and the relevant part of my pylintrc file is:

# Naming style matching correct constant names
#const-naming-style=

# Regular expression matching correct constant names. Overrides const-naming-
# style
const-rgx='[\_][a-z0-9\_]{2,30}$|[a-z][\_][a-z0-9\_]{2,30}$'

which yields the following output:

393,4,convention,C0103:Constant name "run_main" doesn't conform to "'[\\_] 
[a-z0-9\\_]{2,30}$|[a-z][\\_][a-z0-9\\_]{2,30}$'" pattern ("'[\\_][a-z0- 
9\\_]{2,30}$|[a-z][\\_][a-z0-9\\_]{2,30}$'" pattern)
Bjarne Thorsted
  • 117
  • 1
  • 11
  • 1
    FYI: `_` is a word char, you do not need to escape it. – Wiktor Stribiżew May 07 '18 at 08:54
  • @WiktorStribiżew I edited from the pylintrc file generated by pylint, which contained the original sequence, so I just assumed it was necessary. But good to know, thanks. – Bjarne Thorsted May 07 '18 at 09:51
  • Why you can't simply use - `r'[_]?[a-z0-9\_]{2,30}$'` ? optional underscore followed by alhapnum_. Also there is a small problem with this regex , which might match something like 20_main, which is not a valid thing. so something like `r'[_]?[a-z][a-z0-9_]{1,30}$'` will be closer to what you want – gabhijit May 15 '18 at 13:48
  • @gabhijit this did not work either. I still get the same C0103 message. – Bjarne Thorsted May 17 '18 at 08:42
  • It might be interesting to note that commenting out `const-rgx` and setting `const-naming-style=any` will suppress the message, at the cost of losing the style checking for constants. – Bjarne Thorsted Jun 06 '18 at 09:52

1 Answers1

0

Pylint wants any variable assigned in the outermost scope to be in all uppercase. Calling it MAIN should remove the warning.