Skip to content

Commit

Permalink
db: Add type hints to SchedulersConnectorComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
tdesveaux committed May 21, 2024
1 parent 113347a commit 2c43fef
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions master/buildbot/db/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

import sqlalchemy as sa
import sqlalchemy.exc
Expand All @@ -26,6 +27,9 @@
from buildbot.db import base
from buildbot.warnings import warn_deprecated

if TYPE_CHECKING:
from typing import Literal


class SchedulerAlreadyClaimedError(Exception):
pass
Expand Down Expand Up @@ -58,18 +62,18 @@ def __getitem__(self, key: str):


class SchedulersConnectorComponent(base.DBConnectorComponent):
# returns a Deferred that returns None
def enable(self, schedulerid, v):
def thd(conn):
def enable(self, schedulerid: int, v: bool) -> defer.Deferred[None]:
def thd(conn) -> None:
tbl = self.db.model.schedulers
q = tbl.update().where(tbl.c.id == schedulerid)
conn.execute(q.values(enabled=int(v)))

return self.db.pool.do(thd)

# returns a Deferred that returns None
def classifyChanges(self, schedulerid, classifications):
def thd(conn):
def classifyChanges(
self, schedulerid: int, classifications: dict[int, bool]
) -> defer.Deferred[None]:
def thd(conn) -> None:
tbl = self.db.model.scheduler_changes
ins_q = tbl.insert()
upd_q = tbl.update().where(
Expand All @@ -94,9 +98,10 @@ def thd(conn):

return self.db.pool.do(thd)

# returns a Deferred that returns None
def flushChangeClassifications(self, schedulerid, less_than=None):
def thd(conn):
def flushChangeClassifications(
self, schedulerid: int, less_than: int | None = None
) -> defer.Deferred[None]:
def thd(conn) -> None:
sch_ch_tbl = self.db.model.scheduler_changes
wc = sch_ch_tbl.c.schedulerid == schedulerid
if less_than is not None:
Expand All @@ -106,13 +111,17 @@ def thd(conn):

return self.db.pool.do(thd)

# returns a Deferred that returns a value
def getChangeClassifications(
self, schedulerid, branch=-1, repository=-1, project=-1, codebase=-1
):
self,
schedulerid: int,
branch: str | None | Literal[-1] = -1,
repository: str | None | Literal[-1] = -1,
project: str | None | Literal[-1] = -1,
codebase: str | None | Literal[-1] = -1,
) -> defer.Deferred[dict[int, bool]]:
# -1 here stands for "argument not given", since None has meaning
# as a branch
def thd(conn):
def thd(conn) -> dict[int, bool]:
sch_ch_tbl = self.db.model.scheduler_changes
ch_tbl = self.db.model.changes

Expand Down Expand Up @@ -141,7 +150,7 @@ def thd(conn):

return self.db.pool.do(thd)

def findSchedulerId(self, name):
def findSchedulerId(self, name: str) -> int:
tbl = self.db.model.schedulers
name_hash = self.hashColumns(name)
return self.findSomethingId(
Expand All @@ -150,9 +159,8 @@ def findSchedulerId(self, name):
insert_values={"name": name, "name_hash": name_hash},
)

# returns a Deferred that returns None
def setSchedulerMaster(self, schedulerid, masterid):
def thd(conn):
def setSchedulerMaster(self, schedulerid: int, masterid: int | None) -> defer.Deferred[None]:
def thd(conn) -> None:
sch_mst_tbl = self.db.model.scheduler_masters

# handle the masterid=None case to get it out of the way
Expand Down Expand Up @@ -189,15 +197,19 @@ def thd(conn):
return self.db.pool.do(thd)

@defer.inlineCallbacks
def getScheduler(self, schedulerid):
def getScheduler(self, schedulerid: int):
sch = yield self.getSchedulers(_schedulerid=schedulerid)
if sch:
return sch[0]
return None

# returns a Deferred that returns a value
def getSchedulers(self, active=None, masterid=None, _schedulerid=None):
def thd(conn):
def getSchedulers(
self,
active: bool | None = None,
masterid: int | None = None,
_schedulerid: int | None = None,
) -> defer.Deferred[list[SchedulerModel]]:
def thd(conn) -> list[SchedulerModel]:
sch_tbl = self.db.model.schedulers
sch_mst_tbl = self.db.model.scheduler_masters

Expand Down

0 comments on commit 2c43fef

Please sign in to comment.