Quickstart¶
This page takes a small library from "tested on the Python in my .venv" to "tested on
every version it claims to support". The finished project is
examples/minimal
in the repository.
1. A library that claims Python 3.11¶
chunks splits an iterable into tuples of a fixed size. Its pyproject.toml says it
supports Python 3.11 and newer:
[project]
name = "chunks"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = []
[build-system]
requires = ["uv_build>=0.13,<0.14"]
build-backend = "uv_build"
[tool.pytest.ini_options]
testpaths = ["tests"]
src/chunks/__init__.py uses itertools.batched:
"""Split an iterable into fixed-size chunks."""
from collections.abc import Iterable, Iterator
from itertools import batched
from typing import TypeVar
T = TypeVar("T")
def chunked(items: Iterable[T], size: int) -> Iterator[tuple[T, ...]]:
"""Yield tuples of ``size`` items; the last one may be shorter."""
if size < 1:
raise ValueError("size must be at least 1")
return batched(items, size)
tests/test_chunks.py:
import pytest
from chunks import chunked
def test_even_split():
assert list(chunked("abcdef", 2)) == [("a", "b"), ("c", "d"), ("e", "f")]
def test_last_chunk_is_shorter():
assert list(chunked(range(5), 2)) == [(0, 1), (2, 3), (4,)]
def test_empty_input():
assert list(chunked([], 3)) == []
def test_rejects_zero_size():
with pytest.raises(ValueError, match="at least 1"):
list(chunked("abc", 0))
With a 3.13 .venv, uv run pytest passes all four tests.
2. Add pytest-uv¶
uv add --dev pytest-uv
3. Run on every version¶
uv run pytest --python-versions 3.11,3.12,3.13
The first run builds .pytest-uv/py3.11, .pytest-uv/py3.12 and .pytest-uv/py3.13
from uv.lock, downloading any interpreter you don't have. Then each version gets
one xdist worker, and the report shows what 3.11 thinks of the code:
created: 3/3 workers
3 workers [0 items]
........ [100%]
==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_chunks.py _____________________
ImportError while importing test module '/home/you/chunks/tests/test_chunks.py'.
tests/test_chunks.py:3: in <module>
from chunks import chunked
src/chunks/__init__.py:4: in <module>
from itertools import batched
E ImportError: cannot import name 'batched' from 'itertools' (unknown location)
========================== 8 passed, 1 error in 0.88s ==========================
itertools.batched is new in Python 3.12. The eight passes are the four tests on 3.12
and 3.13.
4. Fix it and run again¶
Fall back to islice on older versions:
"""Split an iterable into fixed-size chunks."""
import sys
from collections.abc import Iterable, Iterator
from itertools import islice
from typing import TypeVar
T = TypeVar("T")
def chunked(items: Iterable[T], size: int) -> Iterator[tuple[T, ...]]:
"""Yield tuples of ``size`` items; the last one may be shorter."""
if size < 1:
raise ValueError("size must be at least 1")
if sys.version_info >= (3, 12):
from itertools import batched
yield from batched(items, size)
return
iterator = iter(items)
while chunk := tuple(islice(iterator, size)):
yield chunk
uv run pytest --python-versions 3.11,3.12,3.13
created: 3/3 workers
3 workers [4 items]
............ [100%]
============================== 12 passed in 2.01s ==============================
The environments already exist, so this run starts in about a second. Changing your source code does not rebuild them: the project is installed in editable mode.
5. Stop typing the version list¶
Let requires-python decide, using the interpreters you have installed:
uv run pytest --python-versions auto
Or fix the list in pyproject.toml and use declared:
[tool.pytest-uv]
versions = ["3.11", "3.12", "3.13", "3.14"]
uv run pytest --python-versions declared
Next steps¶
- Choosing versions:
auto,declaredand explicit lists. - Debugging failures: re-run or step through a failure on one version.
- GitHub Actions: the same command in CI.