Pylint can't find SQLAlchemy query member

44,373

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

Warning: Do not just --load-plugins pylint-flask

  • By doing this (using dashes instead of underscores) you will just disable pylint altogether! This is because the --load-plugins needs the name of the python package to be imported. You can check this on command line (will get ImportError) if you do so. Pylinting in VS Code is nastier since you will not get any visible errors. Instead, you will have zero linting errors even if your code would have some linting problems.

pylint-flask will not work

pylint-flask is a good plugin, but it is not even meant to tackle this problem! You can see the source code for yourself. There is no mentions about Flask-SQLAlchemy; it is only designed to fix issues (false positives) with Flask.

pylint-flask-sqlalchemy

The pylint-flask-sqlalchemy was created to fix the pylint false positives with Flask-SQLAlchemy. After installing with

pip install pylint-flask-sqlalchemy

one should add it with1

# NOT: "pylint-flask-sqlalchemy"
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy"]

1Applies directly only for VS Code. With other IDEs or command line, use the same arguments in the same order.

pylint-flask with pylint-flask-sqlalchemy

If used together, for some reason

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

will not work but

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

does. So, presumably, the pylint-flask must be loaded after pylint-flask-sqlalchemy.

Solution 5

The one that worked for me was switching to flake8 python linter. Below are the steps:

  1. Open VSCode and run Ctrl+shift+P(For Windows Users)
  2. In the VSCode Search prompt, type Python:Select Linter. You will see a list of all Linters and select flake8.
  3. If you do not have flake8 installed as a VScode extension for pylint, it will prompt you to install it. Proceed and install it.
Share:
44,373
Pedro Teixeira
Author by

Pedro Teixeira

Updated on August 02, 2020

Comments

  • Pedro Teixeira
    Pedro Teixeira almost 4 years

    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()
    
  • 0rkan
    0rkan over 8 years
    It is the solution since query will only be added at runtime.
  • Romain Vincent
    Romain Vincent over 6 years
    Do you mean that adding all used members of sql_alchemy is the correct way to deal with this? This is both tedious and possibly masking further warnings of similar member names of different classes. Not the best of way in my opinion.
  • Lutaaya Huzaifah Idris
    Lutaaya Huzaifah Idris about 6 years
    Are all these commands we have to run , because the first one refused
  • Krzysztof Krasoń
    Krzysztof Krasoń over 5 years
    After doing that I still get the same errors like Instance of 'SQLAlchemy' has no 'Column' member (no-member)
  • Sonic Soul
    Sonic Soul over 5 years
    this doesn't seem to work for me.. perhaps there are other settings that need to be set
  • antgel
    antgel over 5 years
    This doesn't / shouldn't work,. Read the plugin source code, it's nothing to do with sqlalchemy. Only ignored-modules or generated-members help.
  • kod kristoff
    kod kristoff over 4 years
    for me the following works: "python.linting.pylintArgs": ["--load-plugins", "pylint-flask"]
  • Mirko
    Mirko over 4 years
    I had to use "python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy"] (note the underscores), otherwise pylint throws an ImportError: No module named 'pylint-flask-sqlalchemy', furthermore it did not work with only adding pylint-flask, I needed the plugin pylint-flask-sqlalchemy as suggested by stackoverflow.com/a/58979882/4112816
  • nsssayom
    nsssayom over 4 years
    it worked for me too. No solution worked for pylint
  • Clintm
    Clintm about 4 years
    spelling mistake pylint_flask_sqlalcheny should be pylint_flask_sqlalchemy
  • Bahadır İsmail Aydın
    Bahadır İsmail Aydın about 4 years
    @Mirko I got the same error. When I checked my packages it's named "pylint_flask" with an underscore
  • np8
    np8 almost 4 years
    This is not an answer! Doing so will just disable pylint completely (and silently).
  • bmakan
    bmakan over 3 years
    This helps with flask-sqlalchemy members, but not with sqlalchemy members.
  • Nuclear03020704
    Nuclear03020704 over 3 years
    Spoiler alert: I unexpectedly get lots of hot new red wavy lines simply because flake8 linting is different from pylint linting. It was really unexpected.
  • Gary Vernon Grubb
    Gary Vernon Grubb over 3 years
    I like this. Don't want to spend time configuring linters :-)
  • Tom
    Tom over 2 years
    Only pylint_flask_alchemy is necessary, pylint_flask is irrelevant. However, it seems to no longer work since SQLAlchemy 1.4.0 (specifically commit 2665a0c4cb3e94e6545d0b9bbcbcc39ccffebaba, which removes Session.public_methods, which pylint_flask_alchemy makes use of). It would actually be quite easy to adapt pylint_sql_alchemy to this change, but it seems it's no longer maintained (last commit was on 2020-01-14) :(
  • Tom
    Tom over 2 years
    I meant pylint_flask_sqlalchemy in the comment above, obviously (I accidentally omitted sql). Also, I see now that work is actually still ongoing on it, it's just that it has moved to GitHub. But the published version is still 0.2.0; I guess/hope things will work properly once the next version (seems it will be 1.0.0) is released.