python-3.x - Cloud 9 CS50 上的 pylint 错误消息

标签 python-3.x flask flask-sqlalchemy pylint

我正在学习 CS50 类(class),但 application.py 遇到问题。我在 Cloud 9 代码编辑器 (Ace) 中收到以下警告:

instance of SQLAlchemy has no column member
instance of SQLAlchemy has no integer member
instance of SQLAlchemy has no text member
instance of scoped_session has no add member
instance of scoped_session has no commit member
Class Registrants has no query member

我在主目录中创建了一个文件 .pylintrc 并添加了以下两行:

ignored-modules=flask_sqlalchemy
ignored-classes=SQLObject,Registrant

这消除了大部分错误,但我留下了:

instance of scoped_session has no add member
instance of scoped_session has no commit member

这是导致问题的代码:

from flask import Flask, render_template, redirect, request, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

# Flask-SQLAlchemy
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///froshims3.db"
app.config["SQLALCHEMY_ECHO"] = True
db = SQLAlchemy(app)

class Registrant(db.Model):

    __tablename__ = "registrants"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)
    dorm = db.Column(db.Text)

    def __init__(self, name, dorm):
        self.name = name
        self.dorm = dorm

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/register", methods=["POST"])
def register():
    if request.form["name"] == "" or request.form["dorm"] == "":
        return render_template("failure.html")
    registrant = Registrant(request.form["name"], request.form["dorm"])
    db.session.add(registrant)
    db.session.commit()
    return render_template("success.html")

@app.route("/registrants")
def registrants():
    rows = Registrant.query.all()
    return render_template("registrants.html", registrants=rows)

@app.route("/unregister", methods=["GET", "POST"])
def unregister():
    if request.method == "GET":
        rows = Registrant.query.all()
        return render_template("unregister.html", registrants=rows)
    elif request.method == "POST":
        if request.form["id"]:
            Registrant.query.filter(Registrant.id ==    request.form["id"]).delete()
            db.session.commit()
        return redirect(url_for("registrants"))

最佳答案

需要在.pylintrc文件中添加:

ignored-classes=SQLObject,Registrant,scoped_session

显然 Python 正在运行时创建一些类,而 pylint 无法获取该信息。

对这个答案不太满意,因为它忽略了问题而不是解决问题。如果有人有更好的解决方案请告诉我。 CS50 的工作人员正在研究此问题,但尚未找到其他解决方案。

关于python-3.x - Cloud 9 CS50 上的 pylint 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42789666/

相关文章:

jquery - 使用 AJAX 将 sessionStorage 对象 POST 到 Flask

python / flask : only one user can call a endpoint at one time

Flask reSTLess - 在一对多关系的情况下,用于 relationship.property != val 的搜索过滤器

python - 获取按时间戳排序的一组用户的所有帖子

python - SQLAlchemy 中的 Session 和 db.session 有什么区别?

python-3.x - Python PIP导致错误

python-3.x - 如何使 python 3 print() utf8

python - 当使用flask.config.from_object()时,Flask在哪里寻找配置文件?

python - 三元运算符在Python中是如何实现的

Python 字符串修复,用于在所需的数组元素中插入空格