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

feat: Add a python package #885

Merged
merged 10 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 26 additions & 0 deletions .github/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,30 @@ then
exit 1
fi

# Run `ruff` python formatting
if ! poetry run ruff format --check
then
echo ""
echo "There are some python code style issues."
echo "Run `ruff format` first."
exit 1
fi

# Run `mypy` for python type checking
if ! poetry run mypy .
then
echo ""
echo "There are some python code style issues."
echo "Run `ruff format` first."
aborgna-q marked this conversation as resolved.
Show resolved Hide resolved
exit 1
fi

# Run `ruff` python linting
if ! poetry run ruff check
then
echo ""
echo "There are some python linting issues."
exit 1
fi

exit 0
103 changes: 103 additions & 0 deletions .github/workflows/ci-py.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Continuous integration 🐍

on:
push:
branches:
- main
pull_request:
branches:
- main
merge_group:
types: [checks_requested]
workflow_dispatch: {}

env:
SCCACHE_GHA_ENABLED: "true"

jobs:
# Check if changes were made to the relevant files.
# Always returns true if running on the default branch, to ensure all changes are throughly checked.
changes:
runs-on: ubuntu-latest
# Required permissions
permissions:
pull-requests: read
# Set job outputs to values from filter step
outputs:
python: ${{ github.ref_name == github.event.repository.default_branch || steps.filter.outputs.python }}
steps:
# For pull requests it's not necessary to checkout the code
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
python:
- 'quantinuum-hugr-py/**'
- 'pyproject.toml'

check:
needs: changes
if: ${{ needs.changes.outputs.python == 'true' }}

name: check python
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.10']

steps:
- uses: actions/checkout@v3
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
- name: Install poetry
run: pipx install poetry
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"

- name: Install the project libraries
run: poetry install

- name: Type check with mypy
run: poetry run mypy .

- name: Check formatting with ruff
run: poetry run ruff format --check

- name: Lint with ruff
run: poetry run ruff check

- name: Run tests
run: poetry run pytest

coverage:
needs: [changes, check]
if: ${{ needs.changes.outputs.python == 'true' }} && github.event_name != 'merge_group'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
- name: Install poetry
run: pipx install poetry
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: '3.10'
cache: "poetry"

- name: Install the project libraries
run: poetry install

- name: Run python tests with coverage instrumentation
run: poetry run pytest --cov=./ --cov-report=xml

- name: Upload python coverage to codecov.io
uses: codecov/codecov-action@v3
with:
files: coverage.xml
name: python
token: ${{ secrets.CODECOV_TOKEN }}
36 changes: 31 additions & 5 deletions .github/workflows/ci.yml → .github/workflows/ci-rs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Continuous integration
name: Continuous integration 🦀

on:
push:
Expand All @@ -21,7 +21,30 @@ env:
RUSTC_WRAPPER: "sccache"

jobs:
# Check if changes were made to the relevant files.
# Always returns true if running on the default branch, to ensure all changes are throughly checked.
changes:
runs-on: ubuntu-latest
# Required permissions
permissions:
pull-requests: read
# Set job outputs to values from filter step
outputs:
rust: ${{ github.ref_name == github.event.repository.default_branch || steps.filter.outputs.rust }}
steps:
# For pull requests it's not necessary to checkout the code
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
rust:
- 'quantinuum-hugr/**'
- 'Cargo.toml'
- 'specification/schema/**'

check:
needs: changes
if: ${{ needs.changes.outputs.rust == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -40,7 +63,8 @@ jobs:
RUSTDOCFLAGS: "-Dwarnings"

benches:
if: github.event_name != 'merge_group'
needs: changes
if: ${{ needs.changes.outputs.rust == 'true' }} && github.event_name != 'merge_group'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -53,6 +77,8 @@ jobs:
run: cargo bench --verbose --no-run --workspace --all-features

tests:
needs: changes
if: ${{ needs.changes.outputs.rust == 'true' }}
runs-on: ubuntu-latest
cqc-alec marked this conversation as resolved.
Show resolved Hide resolved
strategy:
matrix:
Expand Down Expand Up @@ -88,8 +114,8 @@ jobs:
run: cargo test --verbose --workspace --all-features

coverage:
if: github.event_name != 'merge_group'
needs: [tests, check]
needs: [changes, tests, check]
if: ${{ needs.changes.outputs.rust == 'true' }} && github.event_name != 'merge_group'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -110,6 +136,6 @@ jobs:
uses: codecov/codecov-action@v4
with:
files: coverage.json
name: ubuntu
name: rust
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
147 changes: 146 additions & 1 deletion .gitignore
cqc-alec marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,149 @@ devenv.local.nix
.pre-commit-config.yaml

# Coverage report
lcov.info
lcov.info
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
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

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/

# 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

# 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/#use-with-ide
.pdm.toml

# 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
.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/

# ruff
.ruff_cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ See [DEVELOPMENT.md](https://github.com/CQCL/hugr/blob/main/DEVELOPMENT.md) for
This project is licensed under Apache License, Version 2.0 ([LICENSE][] or http://www.apache.org/licenses/LICENSE-2.0).

[API documentation here]: https://docs.rs/quantinuum-hugr/
[build_status]: https://github.com/CQCL/hugr/workflows/Continuous%20integration/badge.svg?branch=main
[build_status]: https://github.com/CQCL/hugr/actions/workflows/ci-rs.yml/badge.svg?branch=main
[msrv]: https://img.shields.io/badge/rust-1.75.0%2B-blue.svg
[crates]: https://img.shields.io/crates/v/quantinuum-hugr
[codecov]: https://img.shields.io/codecov/c/gh/CQCL/hugr?logo=codecov
Expand Down
14 changes: 14 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test:
# Auto-fix all clippy warnings
fix:
cargo clippy --all-targets --all-features --workspace --fix --allow-staged
poetry run ruff check --fix

# Run the pre-commit checks
check:
Expand All @@ -17,7 +18,20 @@ check:
# Format the code
format:
cargo fmt
poetry run ruff format

# Generate a test coverage report
coverage:
cargo llvm-cov --lcov > lcov.info

# Load a poetry shell with the dependencies installed
pyshell:
poetry shell

# Run the python tests
pytest:
poetry run pytest

# Generate a python test coverage report
pycoverage:
poetry run pytest --cov=./ --cov-report=html
Loading
Loading