Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint can't find SQLAlchemy query member

I have a Flask (v0.10.1) application using Flask-SQLAlchemy (v2.0) and I'm trying to configure Pylint to check it. Running with Python 3.4.2.

First error was:

 Instance of 'SQLAlchemy' has no 'Table' member (no-member)

And I fixed this one ignoring the check for member attributes on SQLAlchemy:

ignored-classes=SQLAlchemy

But I'm having a problem with the query member on entities:

Class 'UserToken' has no 'query' member (no-member)

Is there any way to fix this issue without having to ignore no-member errors on every query call?


Flask bootstrap:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
app = Flask(__name__)
db.init_app(app)
app.run()

UserToken entity:

from app import db

class UserToken(db.Model):
    user_token_id = db.Column(db.Integer, primary_key=True, index=True)
    token_auth = db.Column(db.String(64), unique=True, nullable=False, index=True)

The controller:

from entities import UserToken

token = UserToken.query.filter(
    UserToken.token_auth == token_hash,
).first()
like image 504
Pedro Teixeira Avatar asked Jan 28 '15 13:01

Pedro Teixeira


3 Answers

Solution

pip install pylint-flask

pip install pylint-flask-sqlalchemy

Load the installed plugin.

For example, if you use VS code, please edit settings.json file as follows:

"python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy", "pylint_flask"]

Optional

If having other warnings, define remaining members in generated-members in pylintrc file.

like image 199
User Avatar answered Nov 03 '22 19:11

User


Any class you declare as inheriting from db.Model won't have query member until the code runs so Pylint can't detect it.

The workaround for this besides ignoring no-member errors on every query call is to add query on the generated-members list in a Pylint config file since it is a member that will only be created at runtime.

When you run Pylint, it will search for a configuration file as stated in its documentation:

You can specify a configuration file on the command line using the --rcfile option. Otherwise, Pylint searches for a configuration file in the following order and uses the first one it finds:

  1. pylintrc in the current working directory
  2. If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file
  3. The file named by environment variable PYLINTRC
  4. if you have a home directory which isn’t /root:
    1. .pylintrc in your home directory
    2. .config/pylintrc in your home directory
  5. /etc/pylintrc

So if you don't have a config and you want a system wide default config for pylint you can use pylint --generate-rcfile > /etc/pylintrc. This will generate a commented configuration file according to the current configuration (or the default if you don't have one) that you can edit to your preferences.

p.s.: generated-members on a config file is the right way to deal with this warning, as it's said by the commented config

  # List of members which are set dynamically and missed by pylint inference
  # system, and so shouldn't trigger E0201 when accessed. Python regular
  # expressions are accepted.
like image 32
0rkan Avatar answered Nov 03 '22 19:11

0rkan


I meet the same issue when using flask_sqlalchemy. my solution is:

pylint --generate-rcfile>~/.config/pylintrc

and then find the

ignored-modules

line, rewrite it to:

ignored-modules=flask_sqlalchemy

all E1101 errors are gone.

Remeber to read the comment:

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
like image 20
axis Avatar answered Nov 03 '22 18:11

axis