Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm warns about Unexpected type in a SqlAlchemy model

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?

like image 532
joaquin Avatar asked Jul 04 '11 17:07

joaquin


1 Answers

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.

like image 151
Andrey Vlasovskikh Avatar answered Nov 06 '22 09:11

Andrey Vlasovskikh