Skip to content
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
15 changes: 9 additions & 6 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
python-version: ["3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13", "3.14"]

steps:
- uses: actions/checkout@v4
Expand All @@ -19,16 +19,19 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pip install -r requirements.txt
run: pip install -e ".[dev]" ruff==0.9.6 mypy==1.15.0 mypy-protobuf==3.6.0 types-requests

- name: Prepare for tests
run: sh prepare_for_tests.sh
run: make test-setup

- name: Run unit tests
run: PYTHONPATH=src python -m unittest discover tests
run: make test

- name: Check formatting with Ruff
run: make format-check

- name: Lint with Ruff
run: ruff check src/ tests/
run: make lint

- name: Type-check with MyPy
run: mypy src/
run: make typecheck
29 changes: 9 additions & 20 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ jobs:
test:
runs-on: ubuntu-latest
env:
# Probably unnecessary, if we install the code as a package
PYTHONPATH: src
# Set your thresholds here
OVERALL_COVERAGE_FAIL_UNDER: "40"
OVERALL_COVERAGE_FAIL_UNDER: "60"
DIFF_COVER_FAIL_UNDER: "50"
steps:
- name: Checkout
Expand All @@ -30,35 +27,28 @@ jobs:
python-version: "3.12"
cache: "pip"

- name: Install test deps
run: |
python -m pip install --upgrade pip
pip install coverage diff-cover
- name: Install dependencies
run: pip install -e ".[dev]" ruff==0.9.6 mypy==1.15.0 mypy-protobuf==3.6.0 types-requests

- name: Prepare for tests
run: sh prepare_for_tests.sh
run: make test-setup

- name: Run unit tests with coverage
run: |
pip install -e ".[dev]"
coverage run --rcfile=.coveragerc --source=src -m unittest discover tests
coverage report -m --fail-under="${OVERALL_COVERAGE_FAIL_UNDER}"
coverage xml -o coverage.xml
coverage html -d htmlcov
run: make coverage COVERAGE_ARGS="--fail-under=${OVERALL_COVERAGE_FAIL_UNDER}"

- name: Generate coverage XML
run: coverage xml -o coverage.xml

- name: Diff coverage for PR changes
if: ${{ github.event_name == 'pull_request' }}
run: |
# Ensure main is available for comparison
git fetch origin main:refs/remotes/origin/main

# Compute coverage on changed lines only and fail if under threshold
diff-cover coverage.xml \
--compare-branch=origin/main \
--fail-under="${DIFF_COVER_FAIL_UNDER}" \
--html-report diffcov.html > diffcov.txt || RC=$?

# Attach a concise summary to the job summary
{
echo "## Diff Coverage Summary"
echo ""
Expand All @@ -69,7 +59,6 @@ jobs:
echo '```'
} >> "$GITHUB_STEP_SUMMARY"

# Exit with diff-cover status if it failed
exit ${RC:-0}

- name: Upload HTML reports
Expand All @@ -81,4 +70,4 @@ jobs:
htmlcov/
diffcov.html
coverage.xml
if-no-files-found: ignore
if-no-files-found: ignore
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.PHONY: lint lint-fix format format-check typecheck test test-setup coverage protobuf check

lint:
ruff check src/ tests/

lint-fix:
ruff check --fix src/ tests/

format:
ruff format src/ tests/

format-check:
ruff format --check src/ tests/

typecheck:
mypy src/

test-setup:
docker build -t model_repo_for_lwpython_tests -f tests/docker/Dockerfile tests/docker

test:
PYTHONPATH=src python -m unittest discover tests

coverage:
coverage run --rcfile=.coveragerc --source=src -m unittest discover tests
coverage report -m $(COVERAGE_ARGS)
coverage html
@echo "HTML report: htmlcov/index.html"

protobuf:
protoc --proto_path=./src --python_out=./src --mypy_out=./src -I . ./src/lionweb/serialization/proto/Chunk.proto

check: format-check lint typecheck test
57 changes: 22 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@ Read the [Documentation](https://lionweb.io/lionweb-python)

We support Python 3.11 to 3.13

## Linting
## Development

Install the package and its dependencies in editable mode:

```
ruff check src/ tests/
mypy src/
pip install -e ".[dev]" ruff mypy mypy-protobuf types-requests
```

## Formatting
## Common tasks

```
black src/ tests/
isort src/ tests/
```
All common tasks are available via `make`:

| Command | Description |
|---|---|
| `make check` | Run all checks (format, lint, typecheck, tests) |
| `make test-setup` | Build the Docker image required for integration tests (run once) |
| `make test` | Run the test suite |
| `make coverage` | Run tests with coverage and generate an HTML report under `htmlcov/` |
| `make format` | Auto-format code with Ruff |
| `make format-check` | Check formatting without modifying files |
| `make lint` | Lint with Ruff |
| `make lint-fix` | Lint and auto-fix fixable issues |
| `make typecheck` | Type-check with MyPy |
| `make protobuf` | Regenerate Protobuf classes from `Chunk.proto` |

## Build locally

Expand All @@ -33,37 +44,13 @@ python -m build

## Release process

* Update version in pyproject.toml and setup.py and src/lionwweb/__init__.py
* Update version in `pyproject.toml` and `src/lionweb/__init__.py`
* Create tag: `git tag -a v0.1.1 -m "Version 0.1.1"`
* Release on Pypi:
* Release on PyPI:

```
pip install setuptools wheel twine
python setup.py sdist bdist_wheel
twine upload dist/*
twine upload dist/*
```
* Push tag

## Tests

```
sh prepare_for_tests.sh # to be run just once
PYTHONPATH=src python -m unittest discover tests
```

You can measure coverage like this:
```
PYTHONPATH=src coverage run -m unittest discover tests
```

And then generate a report with:
```
coverage html
# report generated under htmlcov/index.html
```

## Update Protobuffer classes

```
protoc --proto_path=./src --python_out=./src --mypy_out=./src -I . ./src/lionweb/serialization/proto/Chunk.proto
```
1 change: 0 additions & 1 deletion all_checks.sh

This file was deleted.

4 changes: 0 additions & 4 deletions mypy.ini

This file was deleted.

1 change: 0 additions & 1 deletion prepare_for_tests.sh

This file was deleted.

21 changes: 19 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ where = ["src"]
package-dir = {"" = "src"}

[tool.setuptools.package-data]
"lionweb" = ["py.typed"]
"*" = ["py.typed", "*.pyi"]

[project]
name = "lionweb"
Expand All @@ -36,4 +36,21 @@ dev = [
"coverage",
"diff-cover",
"testcontainers>=4.0",
]
"types-requests",
]

[tool.ruff]
line-length = 100

[tool.ruff.lint]
select = ["E", "F", "W", "I", "UP"]
ignore = ["E501"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["F401"]
"src/lionweb/serialization/proto/**" = ["E", "F", "W", "I", "UP"]

[tool.mypy]
python_version = "3.13"
mypy_path = "src"
namespace_packages = true
6 changes: 2 additions & 4 deletions src/lionweb/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from .classifier_instance_resolver import ClassifierInstanceResolver
from .composite_classifier_instance_resolver import \
CompositeClassifierInstanceResolver
from .composite_classifier_instance_resolver import CompositeClassifierInstanceResolver
from .local_classifier_instance_resolver import LocalClassifierInstanceResolver
from .unresolved_classifier_instance_exception import \
UnresolvedClassifierInstanceException
from .unresolved_classifier_instance_exception import UnresolvedClassifierInstanceException

__all__ = [
"ClassifierInstanceResolver",
Expand Down
6 changes: 3 additions & 3 deletions src/lionweb/api/classifier_instance_resolver.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Optional

from lionweb.api.unresolved_classifier_instance_exception import \
UnresolvedClassifierInstanceException
from lionweb.api.unresolved_classifier_instance_exception import (
UnresolvedClassifierInstanceException,
)
from lionweb.model.impl.proxy_node import ProxyNode

if TYPE_CHECKING:
from lionweb.model import ClassifierInstance


class ClassifierInstanceResolver(ABC):

@abstractmethod
def resolve(self, instance_id: str) -> Optional["ClassifierInstance"]:
"""Return the classifier instance or None if not found."""
Expand Down
6 changes: 2 additions & 4 deletions src/lionweb/api/composite_classifier_instance_resolver.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from typing import List, Optional

from lionweb.api.classifier_instance_resolver import ClassifierInstanceResolver
from lionweb.model import ClassifierInstance


class CompositeClassifierInstanceResolver(ClassifierInstanceResolver):
def __init__(self, *classifier_instance_resolvers: ClassifierInstanceResolver):
self.classifier_instance_resolvers: List[ClassifierInstanceResolver] = list(
self.classifier_instance_resolvers: list[ClassifierInstanceResolver] = list(
classifier_instance_resolvers
)

Expand All @@ -16,7 +14,7 @@ def add(
self.classifier_instance_resolvers.append(classifier_instance_resolver)
return self

def resolve(self, instance_id: str) -> Optional[ClassifierInstance]:
def resolve(self, instance_id: str) -> ClassifierInstance | None:
for resolver in self.classifier_instance_resolvers:
instance = resolver.resolve(instance_id)
if instance is not None:
Expand Down
5 changes: 3 additions & 2 deletions src/lionweb/api/local_classifier_instance_resolver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING, Dict, Iterable, Optional, cast
from collections.abc import Iterable
from typing import TYPE_CHECKING, Optional, cast

from lionweb.api.classifier_instance_resolver import ClassifierInstanceResolver

Expand All @@ -8,7 +9,7 @@

class LocalClassifierInstanceResolver(ClassifierInstanceResolver):
def __init__(self, *instances: "ClassifierInstance"):
self.instances: Dict[str, ClassifierInstance] = {
self.instances: dict[str, ClassifierInstance] = {
cast(str, instance.id): instance for instance in instances if instance.id
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class UnresolvedClassifierInstanceException(RuntimeError):

def __init__(self, instance_id: str):
super().__init__(f"Unable to resolve classifier instance with ID={instance_id}")
self._instance_id = instance_id
Expand Down
Loading
Loading