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

switch to uv #1

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv
52 changes: 12 additions & 40 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,21 @@
name: pr

on:
pull_request:
types: [opened, reopened, synchronize]

concurrency:
group: pr-${{ github.event.number }}
cancel-in-progress: true

jobs:
changes:
test:
runs-on: ubuntu-latest
permissions:
pull-requests: read
outputs:
python: ${{ steps.filter.outputs.python }}
steps:
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
python:
- '**/*.py'
- 'pyproject.toml'

test-python:
runs-on: ubuntu-latest
needs: changes
if: needs.changes.outputs.python == 'true'
strategy:
matrix:
python-version: ['3.12', '3.13']
steps:
- uses: actions/checkout@v4
- uses: eifinger/setup-rye@v3
id: setup-rye
- uses: astral-sh/setup-uv@v5
with:
version: '0.34.0'
- run: rye pin 3.12.3
- name: Sync
run: |
rye sync
if [[ $(git diff --stat requirements.lock) != '' ]]; then
echo 'Rye lockfile not up-to-date'
git diff requirements.lock
exit 1
fi
- run: rye fmt --check
- run: rye lint
- run: rye run check
- run: rye run test
version: "0.5.14"
python-version: ${{ matrix.python-version }}
- run: uv sync --all-extras --locked # abort if the lockfile changes
- run: uv run poe ci:fmt # check formatting is correct
- run: uv run poe ci:lint # and linting
- run: uv run poe check # typecheck too
- run: uv run poe test # then run your tests!
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: astral-sh/setup-uv@v5
with:
python-version: "3.12"
- run: pip install build && python -m build # build
- uses: pypa/gh-action-pypi-publish@release/v1 # publish
version: "0.5.14"
- run: uv build
- uses: pypa/gh-action-pypi-publish@release/v1
160 changes: 4 additions & 156 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,162 +1,10 @@
# Byte-compiled / optimized / DLL files
# Python-generated files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
*.py[oc]
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
*.egg-info

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
# Virtual environments
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
27 changes: 20 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
FROM python:3.12.2-slim-bookworm
# always nice to pin as precisely as possible
FROM python:3.13.1-slim-bookworm

ENV PYTHONUNBUFFERED True
ENV PYTHONUNBUFFERED=True
WORKDIR /app

COPY pyproject.toml requirements.lock README.md ./
RUN sed -i '/^-e file:/d' requirements.lock
RUN pip install . --constraint requirements.lock
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

COPY . ./
# First we copy just the project definition files
# so these layers can be cached
COPY pyproject.toml uv.lock ./

CMD ["python", "postmodern/entry.py"]
# install dependencies
RUN uv sync --frozen

# now copy in all the rest as late as possible
# and depending on how we're running this, we don't even need
# to install it, just copy-paste and run
COPY . /app

# if you DO want to install it, do that here

# or however else you bootstrap your app
CMD ["/app/.venv/bin/python", "/app/postmodern/server.py"]
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# postmodern-python
A very simple template for productive Python.

**Now with [uv](https://docs.astral.sh/uv/)!**

This template pulls together most of the advice and ideas from this blog post, which explains the choices in more detail:

[Beyond Hypermodern: Python is easy now](https://rdrn.me/postmodern-python/)

It includes the following bits:
- Rye for Python, virtual env, dependency management and script running
- uv for Python, virtual env, dependency management and script running
- Ruff for formatting and linting (replaces isort, flake, isort etc)
- Pyright for type checking (set to strict mode, but do what makes you happy)
- Pytest for tests
Expand All @@ -23,10 +25,10 @@ It includes the following bits:
```bash
./rename.sh your-cool-new-name
```
4. Run `rye sync`
4. Run `uv sync`
5. Have a look at the stuff below here, try out some commands and edit this README as you like!

## 💾 Installation
## 💾 Installation (not development!)
```bash
git clone [email protected]:carderne/postmodern-python.git
cd postmodern-python
Expand All @@ -49,20 +51,23 @@ hello()
```

## 🧱 Development
Using [Rye](https://rye.astral.sh/) for development (installation instructions at the link).
Using [uv](https://docs.astral.sh/uv/) for development:
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

Install Python and dependencies:
```bash
rye sync
uv sync
```

Format, lint, typecheck etc:
```bash
rye run fmt
lint
check
test
all # runs all the above sequentially
uv run poe fmt
lint
check
test
all # runs all the above sequentially
```

## 🦺 CI/CD
Expand All @@ -78,7 +83,7 @@ But you can change that!
It also has a Dockerfile that you can try out as follows:
1. Build it
```bash
docker build --tag postmdoern-image .
docker build --tag postmodern-image .
```

2. Run it
Expand Down
2 changes: 0 additions & 2 deletions postmodern/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
def hello() -> str:
return "Hello from postmodern!"
4 changes: 2 additions & 2 deletions postmodern/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from postmodern import hello
from postmodern.hello import main

hello()
main()
2 changes: 1 addition & 1 deletion postmodern/adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def add_two[T: (int, float)](num: T) -> T:
>>> res = add_two(0.5)
>>> assert res == 2.5

>>> res = add_two(1)
>>> res = add_two(2)
>>> assert res == 4 # note this is wrong!
"""
return num + 2
2 changes: 2 additions & 0 deletions postmodern/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main() -> None:
print("Hello from postmodern!")
File renamed without changes.
Loading
Loading