Remove Optional in favor of "| None" notation

Taking advange of latest pydantic features
This commit is contained in:
Stéphane Bidoul 2022-01-26 17:39:56 +01:00
parent bdcfa8872f
commit 65b0ebeeb4
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
4 changed files with 25 additions and 27 deletions

View file

@ -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

View file

@ -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")

View file

@ -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"

View file

@ -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)