question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'DeclarativeMeta' is not defined

See original GitHub issue

Describe your question When creating a Base class with @as_declarative the following errors shows up using mypy error: Name 'DeclarativeMeta' is not defined. Am I doing something wrong or is there a better way?

Example - please use the Minimal, Complete, and Verifiable guidelines if possible Using the example as provided in the documentation, it works without a problem:

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String)

    addresses: Mapped[List["Address"]] = relationship("Address", back_populates="user")

class Address(Base):
    __tablename__ = 'address'

    id = Column(Integer, primary_key=True)
    user_id: int = Column(ForeignKey("user.id"))

    user: Mapped[User] = relationship(User, back_populates="addresses")
main  mypy --config mypy.ini --strict simple-test.py
Success: no issues found in 1 source file

When trying to use it as following it shows the error:

@as_declarative()
class Base(object):
    id = Column(Integer, primary_key=True)


class User(Base):
    __tablename__ = "user"

    name = Column(String)

    addresses: Mapped[List["Address"]] = relationship("Address", back_populates="user")


class Address(Base):
    __tablename__ = "address"

    user_id: int = Column(ForeignKey("user.id"))

    user: Mapped[User] = relationship(User, back_populates="addresses")
main  mypy --config mypy.ini --strict simple-test.py
simple-test.py: error: Name 'DeclarativeMeta' is not defined

mypy config

[mypy]
plugins = sqlalchemy.ext.mypy.plugin

Versions

  • OS: MacOS 11.0
  • Python: Python 3.9.2
  • Mypy: mypy 0.812
  • SQLAlchemy: 1.4.3
  • sqlalchemy2-stub: 0.0.1a4
  • Database: n/a
  • DBAPI: n/a

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:22 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
CaselITcommented, Dec 23, 2021

I think it’s currently required for how the plugin works. In any case this issue seems different from this one, so if you could open a new issue it would be great

0reactions
xncbfcommented, Dec 26, 2021

@CaselIT Thanks, i just opened new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLAlchemy declarative one-to-many not defined error
I'm trying to figure how to define a one-to-many relationship using SQLAlchemy's declarative ORM, and trying to get the example to work, ......
Read more >
sqlalchemy.ext.declarative DeclarativeMeta Example Code
Example code for understanding how to use the DeclarativeMeta class from the sqlalchemy.ext.declarative module of the SQLAlchemy project.
Read more >
Declarative API — SQLAlchemy 1.3 Documentation
The AbstractConcreteBase class does not intend to set up the mapping for the base class until all the subclasses have been defined, as...
Read more >
Cannot define declarative base class as attribute of object #54
I'd like to switch from the dropbox/sqlalchemy stubs to the official stubs, however when the declarative base is defined as attribute of an...
Read more >
Python sqlalchemy.ext.declarative.DeclarativeMeta() Examples
This page shows Python examples of sqlalchemy.ext.declarative.DeclarativeMeta.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found