Pylint can't find SQLAlchemy query member

前端 未结 13 1421
予麋鹿
予麋鹿 2021-01-30 08:25

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:



        
相关标签:
13条回答
  • 2021-01-30 08:53

    After a lot of investigation I was not able to make Pylint to understand this member, so I just added query to the generated-members list and the check is ignored.

    It's not a perfect solution but it works.

    0 讨论(0)
  • 2021-01-30 08:54

    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.

    0 讨论(0)
  • 2021-01-30 08:54

    Another alternative is to add scoped_session to the list of ignored classes:

    # List of class names for which member attributes should not be checked (useful
    # for classes with dynamically set attributes). This supports the use of
    # qualified names.
    ignored-classes=scoped_session
    
    0 讨论(0)
  • 2021-01-30 08:54

    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.

    0 讨论(0)
  • 2021-01-30 08:56

    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.
    
    0 讨论(0)
  • 2021-01-30 08:57

    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.

    0 讨论(0)
提交回复
热议问题