Pycharm 警告 SqlAlchemy 模型中出现意外类型
在 SqlAlchemy 模型中,我收到来自 pycharm 的警告,指出列具有意外类型。
产生警告的简化代码如下:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Peptide(Base):
__tablename__ = 'peptides'
sequence = Column(String, primary_key=True)
scan = Column(Integer)
def __init__(self, scan, sequence):
self.scan = scan
self.sequence = sequence
def __repr__(self):
return '<Peptide "%s" Scan %i>' % (self.sequence, self.scan)
警告是针对 __repr__
方法中的 self.scan
给出的。 如果我将格式字符串更改为:
return '<Peptide "%s" Scan %s>' % (self.sequence, self.scan)
警告就会消失。但实际上 self.scan 在模型中已经被定义为整数,而不是字符串。令人惊讶的是,以下字符串不会产生任何警告:
return '<Scan %i>' % self.scan
这是 pycharm 检查器的过度反应还是与 SqlAlchemy 类型相关的内容?
In a SqlAlchemy model I am getting a warning from pycharm saying that a column has an unexpected type.
The simplified code that produces the warning is as follows:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Peptide(Base):
__tablename__ = 'peptides'
sequence = Column(String, primary_key=True)
scan = Column(Integer)
def __init__(self, scan, sequence):
self.scan = scan
self.sequence = sequence
def __repr__(self):
return '<Peptide "%s" Scan %i>' % (self.sequence, self.scan)
The warning is given for self.scan
in the __repr__
method.
If I change the format string to:
return '<Peptide "%s" Scan %s>' % (self.sequence, self.scan)
the warning goes away. But in fact self.scan has been defined as an integer in the model, not a string. Surprisingly the following string does not produce any warning:
return '<Scan %i>' % self.scan
Is this an overreaction of the pycharm checker or it is something related to SqlAlchemy types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前 PyCharm 不知道 SQLAlchemy ORM 约定,因此通常它不知道模型字段的真实类型。如果您想在 PyCharm 中获得对 SQLAlchemy 的特殊支持,请随时为该问题投票 PY- PyCharm 问题跟踪器中的 4186。
更新:PY-4536已于2013年修复。正如下面的评论提到的,如果您使用Mixins,仍然存在问题,请参阅PY-12002。
Currently PyCharm is unaware of SQLAlchemy ORM conventions, so usually it doesn't know the real types of model fields. If you'd like to get special support for SQLAlchemy in PyCharm, feel free to vote for the issue PY-4186 in the PyCharm issue tracker.
Update: PY-4536 was fixed in 2013. As the comments mention below there is still an issue if you use Mixins, see PY-12002.