[elbe-devel] [PATCH 04/10] elbepack: db: make class hierarchy mypy compatible

Thomas Weißschuh thomas.weissschuh at linutronix.de
Mon Aug 12 14:19:25 CEST 2024


Mypy can not check sqlalchemy.orm.declarative_base() correctly.
Inline the definition of the baseclass as recommended by the SQLAlchemy
documentation.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/db.py | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/elbepack/db.py b/elbepack/db.py
index ae7a67bb27c1..777ae686a8f1 100644
--- a/elbepack/db.py
+++ b/elbepack/db.py
@@ -31,7 +31,8 @@ from sqlalchemy import (
     create_engine,
 )
 from sqlalchemy.exc import OperationalError
-from sqlalchemy.orm import declarative_base, relationship, scoped_session, sessionmaker
+from sqlalchemy.orm import registry, relationship, scoped_session, sessionmaker
+from sqlalchemy.orm.decl_api import DeclarativeMeta
 from sqlalchemy.orm.exc import NoResultFound
 
 from elbepack.dosunix import dos2unix
@@ -39,7 +40,16 @@ from elbepack.elbeproject import ElbeProject
 from elbepack.elbexml import ElbeXML, ValidationMode
 
 
-Base = declarative_base()
+mapper_registry = registry()
+
+
+class Base(metaclass=DeclarativeMeta):
+    __abstract__ = True
+
+    registry = mapper_registry
+    metadata = mapper_registry.metadata
+
+    __init__ = mapper_registry.constructor
 
 
 class ElbeDBError(Exception):

-- 
2.46.0



More information about the elbe-devel mailing list