Skip to content

Commit

Permalink
Merge pull request #703 from seapagan/migrate-to-uv
Browse files Browse the repository at this point in the history
  • Loading branch information
seapagan authored Nov 5, 2024
2 parents 449412a + 753a2b5 commit 371b7b0
Show file tree
Hide file tree
Showing 24 changed files with 3,519 additions and 4,751 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv
24 changes: 24 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Type Checking

on: [push, pull_request, workflow_dispatch]

jobs:
mypy:
# uncomment the line before to disable this job if needed.
# if: false
name: mypy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: "uv.lock"

- name: Set up Python
run: uv python install 3.12

- name: Run mypy
run: uv run --all-extras mypy . --strict
59 changes: 18 additions & 41 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,27 @@ jobs:
--health-retries 5

steps:
# ---------------------------------------------------------------------- #
# checkout repo and setup Python #
# ---------------------------------------------------------------------- #
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
# ---------------------------------------------------------------------- #
# install and configure poetry #
# ---------------------------------------------------------------------- #
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
# ---------------------------------------------------------------------- #
# load cached venv if cache exists #
# ---------------------------------------------------------------------- #
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
path: .venv
key:
venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version
}}-${{ hashFiles('**/poetry.lock') }}
# ---------------------------------------------------------------------- #
# install dependencies if cache does not exist #
# ---------------------------------------------------------------------- #
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
enable-cache: true
cache-dependency-glob: "uv.lock"

- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}

- name: Run tests
# For example, using `pytest`
run:
uv run --all-extras -p ${{ matrix.python-version }} pytest tests
--cov-report=xml

# ---------------------------------------------------------------------- #
# run Pytest #
# ---------------------------------------------------------------------- #
- name: Test with pytest
run: |
poetry run pytest --cov-report=xml
- name: Run codacy-coverage-reporter
if: ${{ env.SKIP_COVERAGE_UPLOAD != 'true'}}
env:
CODACY_CONFIGURED: ${{ secrets.CODACY_PROJECT_TOKEN }}
if: ${{ env.CODACY_CONFIGURED != ''}}
uses: codacy/codacy-coverage-reporter-action@v1
continue-on-error: true
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,4 @@ test.db
docs/CNAME
.python-version
.changelog_generator.toml
.envrc
70 changes: 29 additions & 41 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ci:
autofix_commit_msg: "[pre-commit.ci] auto fixes from pre-commit.com hooks [dependabot skip]"
autofix_commit_msg: "[pre-commit.ci] auto fixes from pre-commit.com hooks"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-yaml
args: [--unsafe]
Expand All @@ -11,60 +11,48 @@ repos:
- id: check-merge-conflict
- id: end-of-file-fixer

- repo: https://github.com/renovatebot/pre-commit-hooks
rev: 39.0.1
hooks:
- id: renovate-config-validator
files: ^renovate\.json$

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
rev: v0.7.2
hooks:
- id: ruff
args: ["--output-format=concise"]
name: "lint with ruff"
- id: ruff-format
name: "format with ruff"

- repo: https://github.com/jackdewinter/pymarkdown
rev: v0.9.23
hooks:
- id: pymarkdown
name: "check markdown"
exclude: ^.github/|CHANGELOG
args: [-d, "MD046", scan]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.11.2" # Use the sha / tag you want to point at
rev: "v1.13.0" # Use the sha / tag you want to point at
hooks:
- id: mypy
name: "Check with Mypy"

- repo: https://github.com/python-poetry/poetry
rev: "1.8.0"
hooks:
- id: poetry-check
name: "check poetry files"
# - id: poetry-lock
name: "run mypy"
additional_dependencies:
- pydantic

- repo: https://github.com/python-poetry/poetry-plugin-export
rev: "1.8.0"
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
rev: 0.4.30
hooks:
- id: poetry-export
name: "export production dependencies"
# Update the uv lockfile
- id: uv-lock
- id: uv-export
name: "Export dependencies to 'requirements.txt'"
args:
[
"--without-hashes",
"-f",
"requirements.txt",
"-o",
"requirements.txt",
"--without",
"dev",
"--no-hashes",
"--no-dev",
"--no-emit-project",
"--output-file=requirements.txt",
]
- id: poetry-export
name: "export development dependencies"
- id: uv-export
name: "Export dev dependencies to 'requirements-dev.txt'"
args:
[
"--without-hashes",
"-f",
"requirements.txt",
"-o",
"requirements-dev.txt",
"--with",
"dev",
"--no-hashes",
"--no-emit-project",
"--output-file=requirements-dev.txt",
]
60 changes: 45 additions & 15 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,34 @@ Mac OS X. For Windows, you can use the
[here](https://github.com/pyenv-win/pyenv-win#installation ) for installation
instructions.

We also use [Poetry](https://python-poetry.org/) to manage our dependencies. You
should have this installed as well. You can install Poetry by following the
instructions on the [Poetry
website](https://python-poetry.org/docs/#installation).
- We use [uv](https://docs.astral.sh/uv/) to manage our dependencies. You should
have this installed as well. You can install `uv` by following the instructions
on their [website](https://docs.astral.sh/uv/getting-started/installation/).

`uv` can be used to actually install Python, even if you do not have it
installed locally (either by system, pyenv or similar).

For example, to install Python 3.12 using `uv`, you can run the following command:

```console
uv python install 3.12
```

If you already have a Python version installed, uv will use this.

!!! tip

If you **don't** want to have Poetry installed globally for any reason,
If you **don't** want to have `uv` installed globally for any reason,
there is an auto-generated `requirements-dev.txt` file in the root of the
project. You can use this to install the dependencies using `pip`:

```console

$ pip install -r requirements-dev.txt
```

With this, `Poetry` is also installed as a dependency for `poe` anyway so
you may as well use it :rofl:! It's a great tool and I highly recommend it.
Also, it is an integral part of this project development.
However, if you are going to be contributing to the project, `uv` is
strongly recommended as this is what we use internally and in the CI.

## Getting Started

Expand All @@ -87,14 +97,21 @@ To get started, follow these steps:

Run the following command to install the required dependencies:

```console
$ poetry install
```terminal
uv sync
```

You then need to activate the virtual environment:

```console
$ poetry shell
```terminal
source .venv/bin/activate
```

If you are using Windows, you can activate the virtual environment using the
following command instead:

```terminal
.venv\Scripts\activate
```

From here you can start working on the project. If you are using an IDE such as
Expand All @@ -106,11 +123,22 @@ the virtual environment that has just been created.
I am quite strict about linting and code formatting and have set up a number of
pre-commit hooks and tasks to ensure that the code meets the required standards.

### Code linting and formatting

We are using [Ruff](https://docs.astral.sh/ruff/) for linting and formatting.
These are set up as pre-commit hooks and can be run as below. You can also use
the `poe ruff` and `poe format` commands to run these manually.

### Type hinting

All code must pass `mypy` checks. This can be run manually using the `poe mypy`
and is part of the pre-commit hooks.

### Install Git Pre-Commit hooks

Please install this if you are intending to submit a PR. It will check commits
locally before they are pushed up to the Repo. The GitHub CI runs the linting
checks (and in future probably MyPy as well), and will fail if there are any errors.
and mypy checks, and will fail if there are any errors.

```console
$ pre-commit install
Expand All @@ -132,11 +160,11 @@ poe pre

We are using [pytest](https://docs.pytest.org/) for testing.

If you add any new features, please add tests for them. This will help us to
If you add any new features, **please add tests for them**. This will help us to
ensure that the code is working as expected and will prevent any regressions.

GitHub Actions will run the tests on every commit and PR, **failing tests will
block the PR from being merged**.
block the PR from being merged**, as will any major drop in test coverage.

There is a task set up to run tests:

Expand All @@ -151,6 +179,8 @@ $ pytest
```

The task is set up so we can automatically add other options in the future.
Notice that both these commands will run the tests with the `--cov` option and
show the coverage report at the end.

## Changelog

Expand Down
35 changes: 22 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
FROM python:3.12-slim AS dev
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

RUN apt-get update -y && apt-get install -y \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
RUN pip install poetry==1.8.3
# Configuring poetry
RUN poetry config virtualenvs.create false

# Copying requirements of a project
COPY pyproject.toml poetry.lock README.md /app/src/
WORKDIR /app/src
# Installing requirements
RUN poetry install --no-root
# Removing gcc
RUN apt-get purge -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
# Copying actual application
COPY . /app/src/
COPY pyproject.toml uv.lock README.md /app/src/
WORKDIR /app

# Install dependencies
ENV UV_SYSTEM_PYTHON=1
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project

# Copy the project into the image
COPY . /app/
# Install the project
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen

ENV PATH="/app/.venv/bin:$PATH"
ENV DOCKER_RUNNING=1

# Running the application
CMD ["uvicorn", "--host", "0.0.0.0", "--port","8001","app.main:app", "--reload"]
7 changes: 5 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## General

- Convert to an installable package that just works(tm), with all the existing
functionality. This will make it much easier to use and maintain. Obviously
everything should be able to be subclassed as required.
- allow to tag endpoints as belonging to a group so can then have similar auth
etc.
- add time-limited bans (configurable)
Expand Down Expand Up @@ -91,8 +94,8 @@ Add Quota functionality.
- add a Redis cache to the endpoints.
[fastapi-redis-cache](https://pypi.org/project/fastapi-redis-cache/) should
make this reasonably painless. Note that project seems to be abandoned with a
lot of un-merged PRs so I have forkd and updated the project to fix a few
exisitng bugs, merge some PRs and add some new features. I'm still putting the
lot of un-merged PRs so I have forked and updated the project to fix a few
existing bugs, merge some PRs and add some new features. I'm still putting the
finishing touches on it but it should be ready soon.

## Frontend
Expand Down
Loading

0 comments on commit 371b7b0

Please sign in to comment.