Skip to content

Commit

Permalink
feat: replace poetry with uv
Browse files Browse the repository at this point in the history
  • Loading branch information
dlstadther committed Aug 23, 2024
1 parent 38a80ee commit cb184b9
Show file tree
Hide file tree
Showing 11 changed files with 1,224 additions and 1,482 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
- name: "Install dependencies"
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
poetry install
make init-uv
make install
- name: "Build mkdocs"
run: make doc-deploy
32 changes: 16 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:

- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
poetry install
make init-uv
make install
- name: Format checking with Ruff Format
run: poetry run ruff format --check .
run: make format

lint:
runs-on: ubuntu-latest
Expand All @@ -38,14 +38,14 @@ jobs:

- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
poetry install
make init-uv
make install
- name: Lint with Ruff
run: poetry run ruff check --output-format=github .
run: make lint-python

- name: Lint with Sqlfluff
run: poetry run sqlfluff lint .
run: make lint-sql

type-check:
runs-on: ubuntu-latest
Expand All @@ -59,11 +59,11 @@ jobs:

- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
poetry install
make init-uv
make install
- name: Type checking with Mypy
run: poetry run mypy .
run: make type

test:
runs-on: ubuntu-latest
Expand All @@ -77,11 +77,11 @@ jobs:

- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
poetry install
make init-uv
make install
- name: Test with pytest
run: poetry run coverage run -m pytest -vv tests && poetry run coverage report
run: make test

# nox:
# runs-on: ubuntu-latest
Expand All @@ -99,8 +99,8 @@ jobs:

# - name: Install dependencies
# run: |
# curl -sSL https://install.python-poetry.org | python3 - --version 1.6.1
# poetry install
# make init-uv
# make install

# - name: Test with nox
# run: poetry run nox --reuse-existing-virtualenvs --no-install
# run: make test-all
25 changes: 17 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ repos:
additional_dependencies: ['pydantic', 'types-requests']

# poetry
- repo: https://github.com/python-poetry/poetry
rev: 1.6.0
hooks:
- id: poetry-check # make sure the poetry configuration does not get committed in a broken state
- id: poetry-lock # make sure the lock file is up-to-date when committing changes
args:
- --check
# - id: poetry-export # sync your requirements.txt file with your current dependencies
#- repo: https://github.com/python-poetry/poetry
# rev: 1.6.0
# hooks:
# - id: poetry-check # make sure the poetry configuration does not get committed in a broken state
# - id: poetry-lock # make sure the lock file is up-to-date when committing changes
# args:
# - --check
# # - id: poetry-export # sync your requirements.txt file with your current dependencies

# sqlfluff
- repo: https://github.com/sqlfluff/sqlfluff
Expand All @@ -49,6 +49,15 @@ repos:
- id: sqlfluff-fix
- id: sqlfluff-lint

# uv
#- repo: https://github.com/astral-sh/uv-pre-commit
# rev: 0.3.1
# hooks:
# # will be added in 0.3.2
# - id: uv-lock
# args:
# - --locked # Requires that the lockfile is up-to-date. If the lockfile is missing or needs to be updated, uv will exit with an error.

# commitlint
# (configuration in commitlint.config.js)
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.4
1 change: 1 addition & 0 deletions .uv-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.3.1
41 changes: 36 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
TEST_DIR="tests"

# Docs
doc-build:
poetry run mkdocs build
uv run mkdocs build

doc-serve: doc-build
poetry run mkdocs serve
uv run mkdocs serve

doc-deploy:
poetry run mkdocs gh-deploy --force
uv run mkdocs gh-deploy --force

# Setup
init: init-uv init-python

init-uv:
curl -LsSf https://astral.sh/uv/$(shell cat .uv-version)/install.sh | sh

init-python:
uv python install $(cat .python-version)

install:
uv sync

lock:
uv lock

# Validations
format:
uv run ruff format --check .

lint: lint-python lint-sql

lint-python:
uv run ruff check --output-format=github .

lint-sql:
uv run sqlfluff lint .

type:
uv run mypy .

test:
poetry run coverage run -m pytest -vv $(TEST_DIR) && poetry run coverage report -m
uv run coverage run -m pytest -vv $(TEST_DIR) && uv run coverage report -m

test-all:
poetry run nox --reuse-venv=yes --no-install
uv run nox --reuse-venv=yes --no-install
55 changes: 27 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,35 @@ Sample structure and setup for a Python project which includes:

## Installation
```shell
# install poetry
curl -sSL https://install.python-poetry.org | python3 -
# update poetry
poetry self update
# install requirements
make init
make install

# ensure venv is created within project
poetry config virtualenvs.in-project true

# install project dependencies into venv
poetry install
# install pre-commit configuration
poetry run pre-commit install
```


## Usage

### Run Pre-Commmit Hooks
### Run Pre-Commit Hooks
```shell
# To run the pre-commit hooks against staged files before committing:
poetry run pre-commit run
uv run pre-commit run

# To run a particular pre-commit hook against staged files before committing:
# poetry run pre-commit run <hook>
poetry run pre-commit run ruff-format
poetry run pre-commit run ruff
poetry run pre-commit run mypy
# uv run pre-commit run <hook>
uv run pre-commit run ruff-format
uv run pre-commit run ruff
uv run pre-commit run mypy

# To run a particular pre-commit hook against all files (staged and unstaged, before committing):
poetry run pre-commit run ruff --all-files
uv run pre-commit run ruff --all-files

# run checks regardless of git status
poetry run ruff format --check src
poetry run mypy src
poetry run ruff src
uv run ruff format --check src
uv run mypy src
uv run ruff src
```

### Run Unittests
Expand All @@ -54,7 +48,7 @@ poetry run ruff src
make test

# Run specific tests
poetry run pytest -vv tests/test_placeholder.py::test_Sample_init
uv run pytest -vv tests/test_placeholder.py::test_Sample_init
```

### Run All Checks
Expand All @@ -63,7 +57,7 @@ poetry run pytest -vv tests/test_placeholder.py::test_Sample_init
make test-all

# run all nox lints
poetry run nox -R -s lint
uv run nox -R -s lint
```

### Build Docs
Expand All @@ -76,17 +70,22 @@ make doc-serve


## Distribution
> As of 2024-08-22, uv does not yet have dedicated commands for building and publishing a package.
> Their [documentation recommends](https://docs.astral.sh/uv/guides/publish/) using the PyPA tools `build` and `twine`.
```shell
# Build sdist and wheel
poetry build
uvx --from build pyproject-build --installer uv

# Only build wheel
poetry build --format wheel
uvx --from build pyproject-build --installer uv --wheel

# Publish to TEST PyPI
export TWINE_USERNAME="__token__"
export TWINE_PASSWORD="<my-pypi-token>"
uvx twine upload --repository testpypi dist/*

# Publish to PYPI
# https://python-poetry.org/docs/repositories/#configuring-credentials
poetry config pypi-token.pypi <my-token>
poetry publish
# Publish to PyPI
uvx twine upload dist/*
```


Expand All @@ -98,8 +97,8 @@ The checks contained in this repo include (in the order in which they run):
* `ruff format` applies standard code style formatting (just like `black`, but faster)
* `ruff` checks code for "lint"
* `mypy` is used for static type checking
* `poetry` checks on valid and aligned pyproject.toml and poetry.lock files
* `sqlfluff` checks and fixes sql formatting and linting
* (COMING SOON) `uv` checks on valid and aligned pyproject.toml and uv.lock files
* `commitlint` enforces commit message conforms to [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) format

If you want `ruff format` to ignore a particular section of code, you can add the comments `# fmt: off` and `# fmt: on` before and after the respective block of code (same as you would if using `black`).
Expand Down
12 changes: 6 additions & 6 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import nox


def _install_poetry(session: nox.Session) -> nox.Session:
session.install("poetry")
session.run("poetry", "install")
def _install_package_manager(session: nox.Session) -> nox.Session:
session.install("uv")
session.run("uv", "sync")
return session


@nox.session
def tests(session: nox.Session) -> None:
session = _install_poetry(session=session)
session = _install_package_manager(session=session)
session.run("coverage", "run", "-m", "pytest", "-vv")
session.run("coverage", "report")


@nox.session
def lint(session: nox.Session) -> None:
session = _install_poetry(session=session)
session = _install_package_manager(session=session)
session.run("ruff", "format", "--check", ".")
session.run("ruff", "check", "--output-format=github", ".")
session.run("sqlfluff", "lint", ".")


@nox.session
def typing(session: nox.Session) -> None:
session = _install_poetry(session=session)
session = _install_package_manager(session=session)
session.run("mypy", ".")
Loading

0 comments on commit cb184b9

Please sign in to comment.