Skip to content

Commit

Permalink
Reemplaza pip+pip-tools por uv (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertotb authored Apr 3, 2024
1 parent 170a082 commit e1ada49
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 219 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/mypy.yml

This file was deleted.

20 changes: 10 additions & 10 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ jobs:
default-shell:
name: Default shell
runs-on: "ubuntu-latest"
defaults:
run:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "pip"
python-version: ${{ matrix.python-version }}
- name: Test with pytest
run: |
pip install -r requirements.txt
pip install pytest-cov
pip install -e .
pytest --cov=./ --cov-report=xml --optional
pip install uv
uv pip install --system -r requirements-dev.txt
uv pip install --system pytest-cov
uv pip install --system -e .
pytest --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: codecov-umbrella
fail_ci_if_error: true
fail_ci_if_error: false
verbose: true
25 changes: 14 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ repos:
rev: v1.6.1
hooks:
- id: mypy
args: ["--install-types", "--non-interactive"]
additional_dependencies: [numpy]
# Esto tiene la desventaja de que siempre se va a ejecutar mypy
# sobre todos los ficheros del repo (incluidos en la conf de mypy)
# Se puede sustituir por `files:`
Expand All @@ -59,20 +61,21 @@ repos:
hooks:
- id: prettier
types_or: [markdown, yaml]
- repo: https://github.com/jazzband/pip-tools
rev: 7.3.0
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.1.28
hooks:
- id: pip-compile
name: pip-compile requirements
args: [pyproject.toml]
files: ^(pyproject\.toml|requirements\.txt)$
name: pip-compile requirements.in
args: [-o, requirements.txt, requirements.in]
- id: pip-compile
name: pip-compile requirements-dev
name: pip-compile requirements-dev.in
args:
[
--constraint=requirements.txt,
--extra=dev,
--output-file=requirements-dev.txt,
pyproject.toml,
-o,
requirements-dev.txt,
-c,
requirements.txt,
requirements.in,
requirements-dev.in,
]
files: ^(pyproject\.toml|requirements-dev\.txt)$
files: ^(requirements\.(in|txt)|requirements-dev\.(in|txt))$
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ ENV PIP_ROOT_USER_ACTION=ignore \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1

RUN pip install -r /code/requirements.txt
RUN pip install uv
RUN uv pip install --system --no-cache -r /code/requirements.txt

COPY ./template /code/template
COPY ./pyproject.toml /code/pyproject.toml
Expand Down
35 changes: 23 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
.ONESHELL:
.PHONY: env install compile sync hooks docker build run debug push
.PHONY: env install compile sync hooks ruff test mypy docker build run debug push

SHELL=/bin/bash
DOCKER_IMG_NAME=ghcr.io/komorebi-ai/python-template
DOCKER_CONTAINER=template
GH_USER=albertotb
GH_TOKEN_FILE=/home/atorres/GITHUB_TOKEN.txt
GH_USER=GITHUB_USERNAME
GH_TOKEN_FILE=GITHUB_TOKEN_PATH

env:
conda create -n python-template python=3.11

install:
pip install -r requirements-dev.txt
pip install -e .[dev]
pip install uv
uv pip install -r requirements-dev.txt
uv pip install -e .[dev]

compile:
pip install pip-tools
pip-compile pyproject.toml
pip-compile --extra dev -o requirements-dev.txt -c requirements.txt pyproject.toml
pip install uv
uv pip compile -o requirements.txt requirements.in
uv pip compile -o requirements-dev.txt -c requirements.txt requirements.in requirements-dev.in

sync:
pip install pip-tools
pip-sync requirements-dev.txt
pip install -e .[dev]
pip install uv
uv pip sync requirements-dev.txt
uv pip install -e .[dev]

hooks:
pre-commit install
pre-commit run --all-files

ruff:
ruff check .
ruff format .

test:
python -m pytest

mypy:
mypy --install-types --non-interactive

docker: build run

build:
Expand All @@ -36,7 +47,7 @@ build:
# https://stackoverflow.com/questions/26564825/what-is-the-meaning-of-a-double-dollar-sign-in-bash-makefile
run:
[ "$$(docker ps -a | grep $(DOCKER_CONTAINER))" ] && docker stop $(DOCKER_CONTAINER) && docker rm $(DOCKER_CONTAINER)
docker run -d --restart=unless-stopped --name $(DOCKER_CONTAINER) -p 7654:80 $(DOCKER_IMG_NAME)
docker run -d --restart=unless-stopped --name $(DOCKER_CONTAINER) -p 80:80 $(DOCKER_IMG_NAME)

debug:
docker run -it $(DOCKER_IMG_NAME) /bin/bash
Expand Down
54 changes: 14 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Tools:
- [ruff](https://docs.astral.sh/ruff/)
- [mypy](https://mypy.readthedocs.io/)
- [pytest](https://docs.pytest.org/en/)
- [pip-tools](https://github.com/jazzband/pip-tools)
- [uv](https://github.com/astral-sh/uv)
- [pre-commit](https://pre-commit.com/)
- [prettier](https://prettier.io/)
- [codespell](https://github.com/codespell-project/codespell)
Expand All @@ -38,12 +38,12 @@ Install a specific version of the package with `pip`
pip install git+ssh://git@github.com/Komorebi-AI/template.git@0.1.0
```

## Install dependencies
## Setup development environment

Create isolated environment with required Python version. This can be done with tools like venv or conda:
Create isolated environment with required Python version. This assumes that you have `conda` installed ([see instructions](https://docs.anaconda.com/free/miniconda/)):

```{bash}
conda create -n python-template python=3.9
make env
```

Then, activate the environment:
Expand All @@ -55,34 +55,12 @@ conda activate python-template
Install dependencies:

```{bash}
pip install -r requirements.txt
```

Install package in editable mode:

```{bash}
pip install -e .[dev]
make install
```

Install and run pre-commit hooks:

```{bash}
pre-commit install
pre-commit run --all-files
```

Alternatively, you can use the included `env.yml` file that performs all the previous steps (except the pre-commit hooks):

```{bash}
conda env create -f env.yml
```

or `make`

```{bash}
make env
conda activate python-template
make install
make hooks
```

Expand All @@ -91,33 +69,29 @@ make hooks
The `requirements.txt` is generated automatically with `pip-tools` and it should not be edited manually. Add abstract dependencies to `requirements.in` and `requirements-dev.in`. If necessary, add version requirements but try to be as flexible as possible. Then, update the `requirements.txt` file with:

```{bash}
pip-compile --extra dev pyproject.toml
make compile
```

If you want to pin separately production and dev dependencies you can use instead:
Sync the local environment with the `requirements-dev.txt` file:

```{bash}
pip-compile pyproject.toml
make sync
```

And:
## Run linter and formatter

```{bash}
pip-compile --extra dev -o requirements-dev.txt -c requirements.txt pyproject.toml
make ruff
```

Or simply:
## Run tests

```{bash}
make compile
make test
```

Flag `-c` constrains the `dev` dependencies to be the same exact versions as the production dependencies. `pip-tools` also has a `pip-sync` command to make sure that the local environment is in sync with the `requirements.txt` or `requirements-dev.txt` file:
## Run type checker

```{bash}
make sync
make mypy
```

## Run tests

- Run tests: `python -m pytest`
9 changes: 0 additions & 9 deletions env.yml

This file was deleted.

27 changes: 14 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
build-backend = "setuptools.build_meta"

[project]
requires-python = ">=3.11,<3.12"
requires-python = ">=3.9,<3.12"
name = "template"
# La versión se obtiene automáticamente de git con `setuptools_scm`
#version = "0.1.0"
Expand All @@ -30,8 +30,8 @@ optional-dependencies.dev = { file = ["requirements-dev.in"] }

[tool.ruff]
line-length = 88
target-version = "py311"
lint.select = [
[tool.ruff.lint]
select = [
"E", # pycodestyle
"F", # Pyflakes
"UP", # pyupgrade
Expand All @@ -49,22 +49,14 @@ lint.select = [

# Ignore D100 Missing docstring in public module
ignore = ["D100"]
[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
# Also ignore `D104` in all `__init__.py` files.
"__init__.py" = ["D104"]
"tests/*.py" = ["D"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.pytest.ini_options]
minversion = "7"
testpaths = ["tests"]
log_cli_level = "INFO"
xfail_strict = true
addopts = ["-ra", "--strict-config", "--strict-markers"]
filterwarnings = ["error", "ignore::DeprecationWarning"]

[tool.mypy]
files = "./template"
strict = true
Expand All @@ -73,5 +65,14 @@ ignore_missing_imports = true
python_version = "3.11"
warn_return_any = true
warn_unused_configs = true
allow_untyped_decorators = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]

[tool.pytest.ini_options]
minversion = "7"
testpaths = ["tests"]
log_cli_level = "INFO"
xfail_strict = true
addopts = ["-ra", "--strict-config", "--strict-markers"]
filterwarnings = ["error", "ignore::DeprecationWarning"]


2 changes: 1 addition & 1 deletion requirements-dev.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pip-tools
uv
pytest
ruff
mypy
Expand Down
Loading

0 comments on commit e1ada49

Please sign in to comment.