From 80e97561dbd4947d9910ded46149e49e20d69377 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Wed, 20 Dec 2023 13:08:09 +0900 Subject: [PATCH] ci(gha): set up CI (#4) * chore: configure mypy Signed-off-by: Isac Byeonghoon Yoo * fix: type errors Signed-off-by: Isac Byeonghoon Yoo * docs: sort imports of README.md Signed-off-by: Isac Byeonghoon Yoo * ci(gha): add ci Signed-off-by: Isac Byeonghoon Yoo * ci(gha): add release Signed-off-by: Isac Byeonghoon Yoo * build: install fastapi on dev Signed-off-by: Isac Byeonghoon Yoo * build: exclude py3.8 Signed-off-by: Isac Byeonghoon Yoo * fix: replace BaseModel with pydantic dataclass Signed-off-by: Isac Byeonghoon Yoo * build: specify minimum version of fastapi Signed-off-by: Isac Byeonghoon Yoo * Revert "build: exclude py3.8" This reverts commit a12f5ac8d23c12d1cb66e38497fcc0e769c71ea2. * ci(gha): build Signed-off-by: Isac Byeonghoon Yoo * Reapply "build: exclude py3.8" This reverts commit 55eb498bcf64cec4be7529302a71639d67accf81. * Revert "fix: replace BaseModel with pydantic dataclass" This reverts commit 8142c80a40b03769aa55f4e50dfd2b4599d09e79. * ci(gha): fix build Signed-off-by: Isac Byeonghoon Yoo * ci(gha): fix build Signed-off-by: Isac Byeonghoon Yoo * ci(gha): skip existing versions on publish Signed-off-by: Isac Byeonghoon Yoo --------- Signed-off-by: Isac Byeonghoon Yoo --- .github/workflows/ci.yaml | 45 ++++++++++++++++++++++++++++++++++ .github/workflows/release.yaml | 27 ++++++++++++++++++++ README.md | 5 +--- plugbear/fastapi.py | 7 +++--- pyproject.toml | 39 ++++++++++++++++++++++++----- 5 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..cd2df6f --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,45 @@ +name: CI + +on: + push: + workflow_dispatch: + +jobs: + ci: + runs-on: ubuntu-latest + strategy: + matrix: + pyver: ["3.9", "3.10", "3.12"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "${{ matrix.pyver }}" + cache: pip + - name: Install dependencies + # language=Bash + run: | + python -m pip install --upgrade pip + pip install -e '.[dev]' + - name: Test with pytest + # language=Bash + run: | + pytest + - name: Lint + # language=Bash + run: | + ruff check --show-fixes --show-source + mypy -p plugbear + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + - name: Build + # language=Bash + run: | + python -m pip install --upgrade pip build + python -m build . diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..c0200a6 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,27 @@ +on: + push: + branches: + - master + + workflow_dispatch: + +jobs: + pypi-publish: + runs-on: ubuntu-latest + environment: release + permissions: + # IMPORTANT: this permission is mandatory for trusted publishing + id-token: write + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + - name: Build + # language=Bash + run: | + pip install -U build + python -m build . + - name: Publish package distributions to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true diff --git a/README.md b/README.md index 0ddedc4..0b74d39 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,6 @@ Here's a simple example to get you started: ```python import contextlib -import plugbear import plugbear.fastapi from fastapi import FastAPI @@ -54,10 +53,8 @@ async def some_llm(context: plugbear.fastapi.Request) -> str: For versions below 0.93.0 ```python -from fastapi import FastAPI - -import plugbear import plugbear.fastapi +from fastapi import FastAPI app = FastAPI() PLUGBEAR_API_KEY = "" diff --git a/plugbear/fastapi.py b/plugbear/fastapi.py index d2914db..2776c32 100644 --- a/plugbear/fastapi.py +++ b/plugbear/fastapi.py @@ -2,9 +2,10 @@ import inspect from collections.abc import Coroutine, Sequence -from typing import Any, Optional, Protocol, Union, runtime_checkable +from typing import Any, Optional, Protocol, Union, cast, runtime_checkable import fastapi +import fastapi.responses from pydantic import BaseModel import plugbear @@ -41,9 +42,9 @@ async def register(app: fastapi.FastAPI, *, llm_func: LLMHandler, api_key: str, @app.post(endpoint, response_class=fastapi.responses.PlainTextResponse) async def plugbear_callback(request: Request) -> str: - return await llm_func(request) + return cast(str, await llm_func(request)) else: @app.post(endpoint, response_class=fastapi.responses.PlainTextResponse) def plugbear_callback(request: Request) -> str: - return llm_func(request) + return cast(str, llm_func(request)) diff --git a/pyproject.toml b/pyproject.toml index a51b6ae..7d7a87e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,14 +7,13 @@ name = "plugbear" authors = [{name = "Runbear", email = "dev@runbear.io"}] readme = "README.md" dynamic = ["version", "description"] -requires-python = ">=3.8" +requires-python = ">=3.9" classifiers = [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", @@ -31,9 +30,9 @@ Home = "https://plugbear.io/" Source = "https://github.com/runbear-io/plugbear-python-sdk" [project.optional-dependencies] -fastapi = ["fastapi"] +fastapi = ["fastapi>=0.51.0"] -dev = ["plugbear[test]", "plugbear[lint]"] +dev = ["plugbear[test]", "plugbear[lint]", "plugbear[fastapi]"] test = [ "pytest~=7.4", "pytest-asyncio~=0.23", @@ -41,6 +40,7 @@ test = [ ] lint = [ "ruff~=0.1", + "mypy~=1.7", ] @@ -50,7 +50,7 @@ asyncio_mode = "auto" [tool.ruff] line-length = 120 -target-version = "py38" +target-version = "py39" [tool.ruff.format] # Like Black, use double quotes for strings. @@ -60,4 +60,31 @@ indent-style = "space" # Like Black, respect magic trailing commas. skip-magic-trailing-comma = false # Like Black, automatically detect the appropriate line ending. -line-ending = "auto" \ No newline at end of file +line-ending = "auto" + + +[tool.mypy] +python_version = "3.9" +allow_redefinition = true +disallow_incomplete_defs = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +no_implicit_optional = true +no_implicit_reexport = true +no_warn_no_return = true +strict_equality = true +warn_redundant_casts = true +warn_return_any = true +warn_unreachable = true +warn_unused_ignores = true + +pretty = true +show_column_numbers = true +show_error_codes = true +show_error_context = true + +# Plugins +plugins = [ + "pydantic.mypy", +]