From 65b0ebeeb4aa3ac932b7c39cde003415c8cd7111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 26 Jan 2022 17:39:56 +0100 Subject: [PATCH] Remove Optional in favor of "| None" notation Taking advange of latest pydantic features --- src/runboat/api.py | 32 ++++++++++++++++---------------- src/runboat/github.py | 4 ++-- src/runboat/settings.py | 11 +++++------ src/runboat/webui.py | 5 ++--- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/runboat/api.py b/src/runboat/api.py index b1e2e9f..7f923cb 100644 --- a/src/runboat/api.py +++ b/src/runboat/api.py @@ -1,6 +1,6 @@ import asyncio import datetime -from typing import AsyncGenerator, Optional +from typing import AsyncGenerator from ansi2html import Ansi2HTMLConverter from fastapi import APIRouter, Depends, HTTPException, Request, status @@ -46,7 +46,7 @@ class Build(BaseModel): commit_info: github.CommitInfo deploy_link: str repo_target_branch_link: str - repo_pr_link: Optional[str] + repo_pr_link: str | None repo_commit_link: str webui_link: str status: models.BuildStatus @@ -78,11 +78,11 @@ async def repos() -> list[models.Repo]: response_model_exclude_none=True, ) async def builds( - repo: Optional[str] = None, - target_branch: Optional[str] = None, - branch: Optional[str] = None, - pr: Optional[int] = None, - status: Optional[models.BuildStatus] = None, + repo: str | None = None, + target_branch: str | None = None, + branch: str | None = None, + pr: int | None = None, + status: models.BuildStatus | None = None, ) -> list[models.Build]: return list( controller.db.search( @@ -96,10 +96,10 @@ async def builds( dependencies=[Depends(authenticated)], ) async def undeploy_builds( - repo: Optional[str] = None, - target_branch: Optional[str] = None, - branch: Optional[str] = None, - pr: Optional[int] = None, + repo: str | None = None, + target_branch: str | None = None, + branch: str | None = None, + pr: int | None = None, ) -> None: for build in controller.db.search( repo=repo, target_branch=target_branch, branch=branch, pr=pr @@ -254,11 +254,11 @@ class BuildEventSource: @router.get("/build-events") async def build_events( request: Request, - repo: Optional[str] = None, - target_branch: Optional[str] = None, - branch: Optional[str] = None, - pr: Optional[int] = None, - build_name: Optional[str] = None, + repo: str | None = None, + target_branch: str | None = None, + branch: str | None = None, + pr: int | None = None, + build_name: str | None = None, ) -> EventSourceResponse: event_source = BuildEventSource( request, repo, target_branch, branch, pr, build_name diff --git a/src/runboat/github.py b/src/runboat/github.py index 08bf5be..524c468 100644 --- a/src/runboat/github.py +++ b/src/runboat/github.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Any, Optional +from typing import Any import httpx from pydantic import BaseModel, validator @@ -26,7 +26,7 @@ async def _github_request(method: str, url: str, json: Any = None) -> Any: class CommitInfo(BaseModel): repo: str target_branch: str - pr: Optional[int] + pr: int | None git_commit: str @validator("repo") diff --git a/src/runboat/settings.py b/src/runboat/settings.py index 6cf3a72..6d2a356 100644 --- a/src/runboat/settings.py +++ b/src/runboat/settings.py @@ -1,5 +1,4 @@ import re -from typing import Optional from pydantic import BaseModel, BaseSettings, validator @@ -41,18 +40,18 @@ class Settings(BaseSettings): # The wildcard domain where the builds will be reacheable. build_domain: str # A dictionary of environment variables to set in the build container and jobs. - build_env: Optional[dict[str, str]] + build_env: dict[str, str] | None # A dictionary of secret environment variables to set in the build container and # jobs. - build_secret_env: Optional[dict[str, str]] + build_secret_env: dict[str, str] | None # A dictionary of variables to be set in the jinja rendering context for the # kubefiles. - build_template_vars: Optional[dict[str, str]] + build_template_vars: dict[str, str] | None # The token to use for the GitHub api calls (to query branches and pull requests, # and report build statuses). - github_token: Optional[str] + github_token: str | None # The file with the python logging configuration to use for the runboat controller. - log_config: Optional[str] + log_config: str | None # The base url where the runboat UI and API is exposed on internet. # Used to generate backlinks in GitHub statuses base_url: str = "http://localhost:8000" diff --git a/src/runboat/webui.py b/src/runboat/webui.py index 0c08c3b..a334806 100644 --- a/src/runboat/webui.py +++ b/src/runboat/webui.py @@ -1,7 +1,6 @@ import shutil from importlib import resources from pathlib import Path -from typing import Optional import jinja2 from fastapi import APIRouter, FastAPI, HTTPException, Response, status @@ -58,7 +57,7 @@ def mount(app: FastAPI) -> None: @router.get("/builds", response_class=RedirectResponse) -async def builds(repo: str, target_branch: Optional[str] = None) -> Response: +async def builds(repo: str, target_branch: str | None = None) -> Response: url = f"/webui/builds.html?repo={repo}" if target_branch: url += f"&target_branch={target_branch}" @@ -66,7 +65,7 @@ async def builds(repo: str, target_branch: Optional[str] = None) -> Response: @router.get("/builds/{name}", response_class=RedirectResponse) -async def build(name: str, live: Optional[str] = None) -> Response: +async def build(name: str, live: str | None = None) -> Response: build = controller.db.get(name) if not build: raise HTTPException(status.HTTP_404_NOT_FOUND)