Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use UV as package manager #245

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@ env:
jobs:
pre-commit:
runs-on: ubuntu-latest
env:
UV_CACHE_DIR: /tmp/.uv-cache
steps:
- uses: actions/checkout@master

- name: Set up uv
run: |
curl -LsSf https://astral.sh/uv/0.3.2/install.sh | sh

- name: Set up python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Install pre-commit
run: pip install pre-commit
python-version-file: "api/pyproject.toml"
- name: Restore uv cache
uses: actions/cache@v4
with:
path: /tmp/.uv-cache
key: uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}
restore-keys: |
uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}
uv-${{ runner.os }}

- name: Set up venv
run: |
pip install poetry
poetry config virtualenvs.in-project true
cd api
poetry install
- name: Install deps
run: cd api && uv sync --dev --locked

- name: Run pre-commit
run: pre-commit run --all-files
run: cd api && uv run pre-commit run --all-files

- name: Minimize uv cache
run: uv cache prune --ci

test-api:
runs-on: ubuntu-latest
Expand All @@ -54,10 +64,10 @@ jobs:

- name: BDD Integration tests
if: ${{ false }} # disable for now
run: docker-compose -f docker-compose.yml -f docker-compose.ci.yml run api behave
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml run api uv run behave

- name: Pytest Integration tests
run: docker-compose -f docker-compose.yml -f docker-compose.ci.yml run --rm api pytest --integration
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm api uv run pytest --integration

test-web:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{".":"1.5.0"}
{ ".": "1.5.0" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Create a copy of `.env-template` called `.env` and populate it with values.
Once you have done the configuration, you can start running:

```sh
docker-compose up
docker compose up
```

The application will be served at http://localhost
Expand Down
15 changes: 8 additions & 7 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
FROM --platform=linux/amd64 python:3.11-slim as base
FROM --platform=linux/amd64 python:3.11-slim AS base
WORKDIR /code
CMD ["/code/src/init.sh", "api"]
EXPOSE 5000

ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/code

RUN pip install --upgrade pip && \
pip install poetry && \
poetry config virtualenvs.create false
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv

COPY pyproject.toml pyproject.toml
COPY poetry.lock poetry.lock
COPY uv.lock uv.lock

RUN mkdir -p /.cache/uv && \
chown -R 1000:1000 /.cache/uv/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is best practice, but uv needs read/write access to this folder. (even with --no-cache)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can undo this chown after running "uv sync". At least in the prod image

FROM base as development
RUN poetry install
RUN uv sync --frozen --no-cache
WORKDIR /code/src
COPY src .
USER 1000

FROM base as prod
RUN poetry install --no-dev
RUN uv sync --frozen --no-cache
WORKDIR /code/src
COPY src .
USER 1000
1,626 changes: 0 additions & 1,626 deletions api/poetry.lock

This file was deleted.

59 changes: 33 additions & 26 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
[tool.poetry]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["src/"]

[project]
name = "api"
version = "1.4.0" # x-release-please-version
description = "API for Template Fastapi React"
authors = []
license = ""

[tool.poetry.dependencies]
cachetools = "^5.3.0"
python = "^3.10"
fastapi = "^0.101.0"
pyjwt = "^2.8.0"
uvicorn = {extras = ["standard"], version = "^0.21.1"}
pymongo = "4.1.1"
certifi = "^2023.7.22"
httpx = "^0.23.3"
opencensus-ext-azure = "^1.1.9"
pydantic = "^2.1"
pydantic-settings = "^2.0.1"
pydantic-extra-types = "^2.0.0"

[tool.poetry.dev-dependencies]
pre-commit = ">=3"
pytest = "^7.2.2"
mongomock = "^4.1.2"
requires-python = ">=3.10"
dependencies = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
requires-python = ">=3.10"
requires-python = ">=3.10"

Should probably refine this, docker uses 3.11 now, while locally/pre-commit using uv will select latest supported python version -> 3.12

"cachetools>=5.3.0,<6.0.0",
"fastapi>=0.101.0,<1",
"pyjwt>=2.8.0,<3.0.0",
"uvicorn[standard]>=0.21.1,<1",
"pymongo==4.1.1",
"certifi>=2023.7.22,<2024.0.0",
"httpx>=0.23.3,<1",
"opencensus-ext-azure>=1.1.9,<2",
"pydantic>=2.1,<2.4",
"pydantic-settings>=2.0.1,<3",
"pydantic-extra-types>=2.0.0,<3",
"setuptools>=73.0.1",
]

[tool.uv]
dev-dependencies = [
"pre-commit >=3",
"pytest >=8.1.1,<9",
"mongomock >=4.1.2,<5"
]



[tool.interrogate]
ignore-init-method = true
Expand All @@ -45,10 +56,6 @@ whitelist-regex = []
color = true
omit-covered-files = false

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]

plugins = ["pydantic.mypy"]
Expand Down Expand Up @@ -88,7 +95,7 @@ ignore = [
"src/tests/*" = ["S101"] # Allow the use of ´assert´ in tests

[tool.codespell]
skip = "*.lock,*.cjs"
skip = "*.lock,*.cjs,documentation/docs/changelog/changelog.md"
ignore-words-list = "ignored-word"

[tool.pytest]
Expand Down
2 changes: 1 addition & 1 deletion api/src/common/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def add_exception_handlers(app: FastAPI) -> None:

# Override built-in default handler
app.add_exception_handler(RequestValidationError, validation_exception_handler) # type: ignore
app.add_exception_handler(HTTPStatusError, http_exception_handler) # type: ignore
app.add_exception_handler(HTTPStatusError, http_exception_handler)

# Fallback exception handler for all unexpected exceptions
app.add_exception_handler(Exception, fall_back_exception_handler)
Expand Down
2 changes: 1 addition & 1 deletion api/src/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eu

if [ "$1" = 'api' ]; then
python3 ./app.py run
uv run ./app.py run
else
exec "$@"
fi
Loading
Loading