Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing imports and arguments when using sqlalchemy.orm.DeclarativeBase #257

Open
LordWolfenstein opened this issue Apr 18, 2023 · 3 comments

Comments

@LordWolfenstein
Copy link

When I create a database following this guide: https://docs.sqlalchemy.org/en/20/orm/declarative_styles.html
Mypy says imports are missing and missing argument. The code itself works but I get errors with mypy. I am using Python 3.9

My file:

from datetime import datetime
from typing import Optional

from sqlalchemy import ForeignKey, create_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship, scoped_session, sessionmaker

from ..settings import DATABASE


class Base(DeclarativeBase):
    pass


class Parent(Base):
    __tablename__ = "parent_table"
    id: Mapped[int] = mapped_column(primary_key=True)
    children: Mapped[list["Child"]] = relationship(back_populates="parent")


class Child(Base):
    __tablename__ = "child_table"
    id: Mapped[int] = mapped_column(primary_key=True)
    parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
    parent: Mapped["Parent"] = relationship(back_populates="children")

My errors:

db.py:5: error: Module "sqlalchemy.orm" has no attribute "DeclarativeBase"
db.py:5: error: Module "sqlalchemy.orm" has no attribute "Mapped"; maybe "Mapper"?
db.py:5: error: Module "sqlalchemy.orm" has no attribute "mapped_column"
db.py:17: error: Missing positional argument "argument" in call to "RelationshipProperty"
db.py:24: error: Missing positional argument "argument" in call to "RelationshipProperty"
@Kakadus
Copy link

Kakadus commented Aug 27, 2023

As a workaround, you can import those from sqlalchemy.orm.decl_api instead

@BakinSergey
Copy link

in my case, it was just necessary to delete stubs:
pip uninstall sqlalchemy-stubs
pip uninstall sqlalchemy2-stubs

@markalanboyd
Copy link

in my case, it was just necessary to delete stubs: pip uninstall sqlalchemy-stubs pip uninstall sqlalchemy2-stubs

Thank you! This solved it for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants