PyCharm warns about unexpected arguments for SQLAlchemy User model
Question:
I’m working with Flask-SQLAlchemy in PyCharm. When I try to create instances of my User
model by passing keyword arguments to the model, PyCharm highlights the arguments with an “Unexpected argument(s)” warning. When I create instances of other models, I don’t get this warning. Why am I getting this error for my User
model?
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
new_user = User(username="test")
In the above example username="test"
is highlighted as a warning.
Answers:
This is a bug in PyCharm, not your code. PyCharm doesn’t recognize column names as arguments when using mixins. You can show your interest in the issue by clicking the thumbs up button next to its title. Until then, there’s nothing you can do to fix the issue besides disabling the inspection or ignoring the highlighting.
If you would like to solve without a lot of effort, you can use type hints on more recent versions of python.
When defining your class, simply place the type hint directly into the declaration:
class User(db.Model, UserMixin):
id: db.Column = db.Column(db.Integer, primary_key=True)
username: db.Column = db.Column(db.String, unique=True, nullable=False)
If you do this for all tables, it does turn out a bit verbose, but it will also be fully functional for all linters, not just PyCharm.
Another way to solve this annoying issue is to add a comment # type: ignore
to the line, which removes Pycharm’s type checking on that line and looks a lot nicer:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
new_user = User(username="test") # type: ignore
slightlynybbled I tried to implement your suggestion:
class User(db.Model, UserMixin):
id: db.Column = db.Column(db.Integer, primary_key=True)
username: db.Column = db.Column(db.String, unique=True, nullable=False)
In my auth.py the code is looking like this:
new_user = User(email=email, first_name=first_name,
password=generate_password_hash(password1, method='sha256'))
db.session.add(new_user)
db.session.commit()
And the models.py is looking like this:
class User(db.Model, UserMixin):
id: db.Column = db.Column(db.Integer, primary_key=True)
email: db.Column = db.Column(db.String(150), unique=True)
password: db.Column = db.Column(db.String(150))
first_name: db.Column = db.Column(db.String(150))
notes: db.Column = db.relationship('Note')
Unfortunately your hint did not solve the problem. From what I can read in the tickets that were linked here this problem is persistend to this day.
I’m working with Flask-SQLAlchemy in PyCharm. When I try to create instances of my User
model by passing keyword arguments to the model, PyCharm highlights the arguments with an “Unexpected argument(s)” warning. When I create instances of other models, I don’t get this warning. Why am I getting this error for my User
model?
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
new_user = User(username="test")
In the above example username="test"
is highlighted as a warning.
This is a bug in PyCharm, not your code. PyCharm doesn’t recognize column names as arguments when using mixins. You can show your interest in the issue by clicking the thumbs up button next to its title. Until then, there’s nothing you can do to fix the issue besides disabling the inspection or ignoring the highlighting.
If you would like to solve without a lot of effort, you can use type hints on more recent versions of python.
When defining your class, simply place the type hint directly into the declaration:
class User(db.Model, UserMixin):
id: db.Column = db.Column(db.Integer, primary_key=True)
username: db.Column = db.Column(db.String, unique=True, nullable=False)
If you do this for all tables, it does turn out a bit verbose, but it will also be fully functional for all linters, not just PyCharm.
Another way to solve this annoying issue is to add a comment # type: ignore
to the line, which removes Pycharm’s type checking on that line and looks a lot nicer:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
new_user = User(username="test") # type: ignore
slightlynybbled I tried to implement your suggestion:
class User(db.Model, UserMixin):
id: db.Column = db.Column(db.Integer, primary_key=True)
username: db.Column = db.Column(db.String, unique=True, nullable=False)
In my auth.py the code is looking like this:
new_user = User(email=email, first_name=first_name,
password=generate_password_hash(password1, method='sha256'))
db.session.add(new_user)
db.session.commit()
And the models.py is looking like this:
class User(db.Model, UserMixin):
id: db.Column = db.Column(db.Integer, primary_key=True)
email: db.Column = db.Column(db.String(150), unique=True)
password: db.Column = db.Column(db.String(150))
first_name: db.Column = db.Column(db.String(150))
notes: db.Column = db.relationship('Note')
Unfortunately your hint did not solve the problem. From what I can read in the tickets that were linked here this problem is persistend to this day.