Skip to content

Commit

Permalink
Models: add type defs, remove ambiguity of Base
Browse files Browse the repository at this point in the history
To comply with mypy, type definitions have been added and the name Base which
before was using the factory declarative_base now use the decorator
as_declarative. Disable no-member for cls, pylint thinks its a reference to an
instance(a "self") of the class rather than a reference to the class.

type: ignore on Base.__tablename__ due to false-positive error,
see sqlalchemy/sqlalchemy2-stubs#127
  • Loading branch information
teoti001 committed Jul 21, 2021
1 parent 271e60e commit 75b39c2
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions caramel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import sqlalchemy as _sa
from sqlalchemy.ext.declarative import (
declarative_base as _declarative_base,
declared_attr as _declared_attr,
declared_attr,
as_declarative
)
import sqlalchemy.orm as _orm
from zope.sqlalchemy import register
Expand All @@ -14,7 +14,7 @@
import datetime as _datetime
import dateutil.parser
import uuid

from typing import List

X509_V3 = 0x2 # RFC 2459, 4.1.2.1

Expand Down Expand Up @@ -88,10 +88,11 @@ def _fkcolumn(referent, *args, **kwargs):
register(DBSession)


@as_declarative()
class Base(object):
@_declared_attr
def __tablename__(cls):
return cls.__name__.lower()
@declared_attr # type: ignore
def __tablename__(cls) -> str:
return cls.__name__.lower() # pylint: disable=no-member

id = _sa.Column(_sa.Integer, primary_key=True)

Expand All @@ -108,10 +109,6 @@ def all(cls):
return cls.query().all()


# XXX: Newer versions of sqlalchemy have a decorator variant 'as_declarative'
Base = _declarative_base(cls=Base)


# XXX: not the best of names
def init_session(engine, create=False):
DBSession.configure(bind=engine)
Expand All @@ -135,10 +132,10 @@ class CSR(Base):
orgunit = _sa.Column(_sa.String(_UB_OU_LEN))
commonname = _sa.Column(_sa.String(_UB_CN_LEN))
rejected = _sa.Column(_sa.Boolean(create_constraint=True))
accessed = _orm.relationship(
accessed: List["AccessLog"] = _orm.relationship(
"AccessLog", backref="csr", order_by="AccessLog.when.desc()"
)
certificates = _orm.relationship(
certificates: List["Certificate"] = _orm.relationship(
"Certificate",
backref="csr",
order_by="Certificate.not_after.desc()",
Expand Down

0 comments on commit 75b39c2

Please sign in to comment.