From 6020c9b25ba8577c3e15ff782d336c3285a92190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sun, 17 Oct 2021 15:43:14 +0200 Subject: [PATCH] Some more typing --- pyproject.toml | 16 ++++++++++++++++ src/runboat/api.py | 8 ++++---- src/runboat/app.py | 2 +- src/runboat/db.py | 6 ++++-- src/runboat/models.py | 21 +++++++++++---------- src/runboat/webhooks.py | 6 +++--- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e3ed486..991961e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,10 +19,26 @@ dependencies = [ ] dynamic = ["version", "description"] +[project.optional-dependencies] +test = [ + "pytest", + "pytest-cov", +] +mypy = [ + "mypy", + "sqlalchemy[mypy]", + "types-requests", +] + [project.urls] Home = "https://github.com/sbidoul/runboat" [tool.isort] profile = 'black' +[tool.mypy] +plugins = [ + "sqlalchemy.ext.mypy.plugin", +] + # flake8 config is in .flake8 diff --git a/src/runboat/api.py b/src/runboat/api.py index e5f0cb6..35e1b04 100644 --- a/src/runboat/api.py +++ b/src/runboat/api.py @@ -117,8 +117,8 @@ def stop(repo_id: str, branch_id: str, build_id: str): ) def trigger_branch(org: str, repo: str, branch: str, db: Session = Depends(get_db)): branch_info = github.get_branch_info(org, repo, branch) - branch = models.Branch.for_github_branch(db, branch_info) - return models.Build.for_branch(db, branch, branch_info.head_sha) + _branch = models.Branch.for_github_branch(db, branch_info) + return models.Build.for_branch(db, _branch, branch_info.head_sha) @app.post( @@ -127,5 +127,5 @@ def trigger_branch(org: str, repo: str, branch: str, db: Session = Depends(get_d ) def trigger_pr(org: str, repo: str, pr: int, db: Session = Depends(get_db)): pr_info = github.get_pr_info(org, repo, pr) - branch = models.Branch.for_github_pr(db, pr_info) - return models.Build.for_branch(db, branch, pr_info.head_sha) + _branch = models.Branch.for_github_pr(db, pr_info) + return models.Build.for_branch(db, _branch, pr_info.head_sha) diff --git a/src/runboat/app.py b/src/runboat/app.py index d669b6c..60a89cc 100644 --- a/src/runboat/app.py +++ b/src/runboat/app.py @@ -6,5 +6,5 @@ app = FastAPI(title="Runboat") @app.on_event("startup") -async def startup(): +async def startup() -> None: create_tables() diff --git a/src/runboat/db.py b/src/runboat/db.py index f6ca867..f6bddd2 100644 --- a/src/runboat/db.py +++ b/src/runboat/db.py @@ -1,3 +1,5 @@ +from typing import Generator + from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session, sessionmaker @@ -9,11 +11,11 @@ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() -def create_tables(): +def create_tables() -> None: Base.metadata.create_all(engine) -def get_db() -> Session: +def get_db() -> Generator[Session]: db = SessionLocal() try: yield db diff --git a/src/runboat/models.py b/src/runboat/models.py index 4f766da..2a0a897 100644 --- a/src/runboat/models.py +++ b/src/runboat/models.py @@ -1,10 +1,12 @@ +from typing import List + from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func from sqlalchemy.ext.compiler import compiles -from sqlalchemy.orm import relationship +from sqlalchemy.orm import Session, relationship from sqlalchemy.sql import expression from .build_images import get_build_image -from .db import Base, Session +from .db import Base from .exceptions import RepoNotFound from .github import BranchInfo, PullRequestInfo from .settings import settings @@ -28,7 +30,7 @@ class Repo(Base): org = Column(String, nullable=False) name = Column(String, nullable=False) - branches = relationship("Branch", back_populates="repo") + branches: List["Branch"] = relationship("Branch", back_populates="repo") @property def display_name(self) -> str: @@ -62,13 +64,13 @@ class Branch(Base): id = Column(Integer, primary_key=True, index=True) created = Column(DateTime, nullable=False, server_default=utcnow()) - target_branch = Column(String, nullable=False) + target_branch: str = Column(String, nullable=False) pr = Column(Integer, nullable=True) repo_id = Column(Integer, ForeignKey("repo.id"), nullable=False, index=True) - repo = relationship("Repo", back_populates="branches") - builds = relationship("Build", back_populates="branch") + repo: Repo = relationship(Repo, back_populates="branches") + builds: List["Build"] = relationship("Build", back_populates="branch") @property def display_name(self) -> str: @@ -143,11 +145,11 @@ class Build(Base): created = Column(DateTime, nullable=False, server_default=utcnow()) branch_id = Column(Integer, ForeignKey("branch.id"), nullable=False, index=True) - branch = relationship("Branch", back_populates="builds") + branch: Branch = relationship(Branch, back_populates="builds") build_image = Column(String, nullable=False) - git_sha = Column(String, nullable=False) - status = Column(String, nullable=False) + git_sha: str = Column(String, nullable=False) + status: str = Column(String, nullable=False) # ressource_label = Column(String, nullable=False, unique=True, index=True) # TODO: add unique constraint on branch_id + git_sha @@ -166,7 +168,6 @@ class Build(Base): @classmethod def for_branch(cls, db: Session, branch: Branch, git_sha: str) -> "Build": - print("*******", branch) build = ( db.query(Build) .filter( diff --git a/src/runboat/webhooks.py b/src/runboat/webhooks.py index 5aebfad..a69e207 100644 --- a/src/runboat/webhooks.py +++ b/src/runboat/webhooks.py @@ -1,4 +1,4 @@ -def on_pr_open_or_update(): +def on_pr_open_or_update() -> None: # find Repo # find image from target branch (exit if not found) # find or create Branch @@ -7,13 +7,13 @@ def on_pr_open_or_update(): ... -def on_pr_close_or_merge(): +def on_pr_close_or_merge() -> None: # find Repo, Branch # delete branch (enqueue) ... -def on_push(): +def on_push() -> None: # find Repo, branch # find image from target branch (exit if not found) # find or create Branch