47

I'm confused about the error(s) in this photo:

Enter image description here

I don't know how to fix them. My program is a Python-Flask web frame. When I use Visual Studio Code to debug my program, Pylint shows these errors. I know this problem doesn't matter, but it makes me annoyed. How can I fix it?

# -*- coding: utf-8 -*-
import sys
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_moment import Moment
#from flask_wtf import Form
#from wtforms import StringField, SubmitField
#from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy

reload(sys)
sys.setdefaultencoding('utf-8')

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/test?'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)


if __name__ == '__main__':
    db.create_all()
    app.run()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xing
  • 167
  • 1
  • 4
  • 8

5 Answers5

58

As explained by Kundor, PEP 8 states that:

Constants are usually defined on a module level and written in all capital letters with underscores separating words.

The point is that "constants" in Python don't really exist. Pylint, as per PEP 8, expects module level variables to be "constants."

That being said you've several options:

  • you don't want this "constant" thing, then change Pylint's const-rgx regular expression to be the same as e.g. variable-rgx,

  • you may deactivate those warnings for this file, or even locally in the file, using # pylint: disable=invalid-name,

  • avoid module level variables, by wrapping them into a function.

In your case, I would go with the third option, by creating a build_app function or something similar. That would return the application (and maybe the 'db' object as well, but you have several choices there). Then you could add a salt of the second option to get something like:

app = build_app() # pylint: disable=invalid-name

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sthenault
  • 14,397
  • 5
  • 38
  • 32
  • 4
    "Pylint, as per PEP 8, expects module level variables to be 'constants.'" AFAICT PEP 8 is *not* saying that all module-level variables are constants. It's saying the converse: constants are module-level variables. – alecbz Jan 05 '18 at 00:34
  • 4
    -1 PEP 8 (from your link): `Constants are usually defined on a module level and written in all capital letters with underscores separating words.` @Alec is right. This does not say don't use module (non-constant) variables. That would be silly. I also disagree with your advice to obfuscate code just to avoid what is essentially a flaw in static code checkers like Pylint. – C S Oct 07 '18 at 01:04
  • Definitely a misrepresentation of PEP 8. – AdamC Mar 15 '23 at 13:52
20

The fact that PEP 8 considers only constants at the module level is probably the reason why many developers use a dedicated main() function.

So you could solve your problem like this:

def main():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'hard to guess string'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/test?'
    app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

    bootstrap = Bootstrap(app)
    moment = Moment(app)
    db = SQLAlchemy(app)
    db.create_all()
    app.run()

if __name__ == '__main__':
    main()
martineau
  • 119,623
  • 25
  • 170
  • 301
T.M.
  • 589
  • 8
  • 10
4

PEP 8 decrees that names of constants should be in all caps.

So, rename those variables to be all caps.

Tip: if you google 'C0103' it'll take you to the Pylint messages wiki entry for that message, with details on it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Matteo
  • 4,453
  • 1
  • 24
  • 35
  • 1) Google actually took me here. 2) "Constants" are not really specified in Python, and you have to consider intent, which Pylint doesn't know. – AdamC Mar 15 '23 at 13:51
2

Rename those variables to be all caps.

Such as

app = Flask(__name__)      => APP = Flask(__name__)
bootstrap = Bootstrap(app) => BOOTSTRAP = Bootstrap(app)
JChen___
  • 3,593
  • 2
  • 20
  • 12
1

You can use the following line at the beginning of your Python script and execute it:

# pylint: disable=invalid-name

It will disable all the invalid constant convention messages in the static code analyzer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131