sqlalchemy2-stubs icon indicating copy to clipboard operation
sqlalchemy2-stubs copied to clipboard

Regression of #63? `Need type annotation for` <relationship>

Open posita opened this issue 3 years ago • 0 comments

#63 is closed as a duplicate of sqlalchemy/sqlalchemy#6255, which is closed as fixed. However, the problem still occurs:

# test_case.py
from __future__ import annotations

from sqlalchemy import Column, ForeignKey, Integer, MetaData
from sqlalchemy.orm import registry, relationship
from sqlalchemy.orm.decl_api import DeclarativeMeta

# See
# <https://docs.sqlalchemy.org/en/14/orm/mapping_styles.html#creating-an-explicit-base-non-dynamically-for-use-with-mypy-similar>.
mapper_registry = registry(
    metadata=MetaData(schema="test_db"),
)


class Base(metaclass=DeclarativeMeta):
    __abstract__ = True

    # these are supplied by the sqlalchemy2-stubs, so may be omitted
    # when they are installed
    registry = mapper_registry
    metadata = mapper_registry.metadata

    __init__ = mapper_registry.constructor


# Taken verbatim from <https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#many-to-one>.
class Parent(Base):
    __tablename__ = "parent"
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey("child.id"))
    child = relationship("Child")


class Child(Base):
    __tablename__ = "child"
    id = Column(Integer, primary_key=True)
% python --version
Python 3.9.9
% mypy --version
mypy 0.931
% pip list | grep -i alchemy
SQLAlchemy        1.4.29
sqlalchemy2-stubs 0.0.2a19
% mypy --config=/dev/null test_case.py
/dev/null: No [mypy] section in config file
test_case.py:31: error: Need type annotation for "child"
Found 1 error in 1 file (checked 1 source file)

posita avatar Jan 15 '22 18:01 posita