dropbox / sqlalchemy-stubs

Mypy plugin and stubs for SQLAlchemy
Apache License 2.0
570 stars 101 forks source link

`backref` relationships raise `has no attribute` #168

Open ojomio opened 4 years ago

ojomio commented 4 years ago

For example, the following code fails to validate:

from sqlalchemy.orm import backref, relationship
from sqlalchemy.sql.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class A(Base):
    id = Column(Integer, primary_key=True)

class B(Base):
    id = Column(Integer, primary_key=True)
    a_id = Column(
        Integer, ForeignKey('a.id', ondelete='CASCADE'), index=True, nullable=False
    )

    a = relationship(A, backref=backref('bs'))

print(B().a)
print(A().bs)
sql.py:24: error: "A" has no attribute "bs"
Found 1 error in 1 file (checked 1 source file)
zhammer commented 3 years ago

does anyone have a workaround for this?