diff --git a/.github/actions/setup-project/action.yml b/.github/actions/setup-project/action.yml deleted file mode 100644 index 21b22333..00000000 --- a/.github/actions/setup-project/action.yml +++ /dev/null @@ -1,81 +0,0 @@ -# Action: Setup Project (composite action) -# -# Purpose: Bootstrap a Python project within GitHub Actions by: -# - Installing uv and uvx into a local ./bin directory and adding it to PATH -# - Detecting the presence of pyproject.toml and exposing that as an output -# - Creating a virtual environment with uv and (optionally) syncing dependencies -# -# Inputs: -# - python-version: Python version for the uv-managed virtual environment (default: 3.12) -# -# Outputs: -# - pyproject_exists: "true" if pyproject.toml exists, otherwise "false" -# -# Notes: -# - Safe to run in repositories without pyproject.toml; dependency sync will be skipped. -# - Purely a CI helper — it does not modify repository files. - -name: 'Setup Project' -description: 'Setup the project' - -inputs: - python-version: - description: 'Python version to use' - required: false - default: '3.12' - -outputs: - pyproject_exists: - description: 'Flag indicating whether pyproject.toml exists' - value: ${{ steps.check_pyproject.outputs.exists }} - -runs: - using: 'composite' - steps: - - name: Set up uv, uvx and the venv - shell: bash - run: | - mkdir -p bin - - # Add ./bin to the PATH - echo "Adding ./bin to PATH" - echo "$(pwd)/bin" >> $GITHUB_PATH - - # Install uv and uvx - curl -fsSL https://astral.sh/uv/install.sh | UV_INSTALL_DIR="./bin" sh - - - name: Check version for uv - shell: bash - run: | - uv --version - - - name: Check for pyproject.toml - id: check_pyproject - shell: bash - run: | - if [ -f "pyproject.toml" ]; then - echo "exists=true" >> "$GITHUB_OUTPUT" - else - echo "exists=false" >> "$GITHUB_OUTPUT" - fi - - - name: Build the virtual environment - shell: bash - run: uv venv --python ${{ inputs.python-version }} - - - name: "Sync the virtual environment for ${{ github.repository }} if pyproject.toml exists" - shell: bash - run: | - if [ -f "pyproject.toml" ]; then - uv sync --all-extras - else - echo "No pyproject.toml found, skipping package installation" - fi - - - name: Show dependencies - shell: bash - run: uv pip list - - - name: Show Python version - shell: bash - run: uv run python -c "import sys; print(sys.version)" diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6cc45e03..f91d248e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,13 +1,12 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python - -name: pytest +name: Install and test on: push: - branches: ["main"] + branches: + - main pull_request: - branches: ["main"] + branches: + - main concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -15,22 +14,23 @@ concurrency: jobs: code-quality: + name: code-quality runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v6 + - name: repository checkout step + uses: actions/checkout@v6 - - uses: actions/setup-python@v6 + - name: Install uv + uses: astral-sh/setup-uv@v7 with: - python-version: '3.14' + enable-cache: true + python-version: "3.14" + + - name: Create virtual environment + run: uv venv - name: install pre-commit - run: python3 -m pip install pre-commit - - - name: Checkout code - uses: actions/checkout@v6 - with: - fetch-depth: 0 + run: uv pip install pre-commit - name: Get changed files id: changed-files @@ -40,7 +40,7 @@ jobs: - name: Print changed files run: | - echo "Changed files: $CHANGED_FILES" + echo "Changed files:" && echo "$CHANGED_FILES" | tr ' ' '\n' - name: Run pre-commit on changed files run: | @@ -50,17 +50,42 @@ jobs: echo "No changed files to check." fi - pytest-nosoftdeps: + detect-notebooks-change: needs: code-quality - name: nosoftdeps (${{ matrix.python-version }}, ${{ matrix.os }}) - runs-on: ${{ matrix.os }} - env: - MPLBACKEND: Agg # https://github.com/orgs/community/discussions/26434 + name: detect change affecting notebooks + runs-on: ubuntu-latest + permissions: + pull-requests: read + outputs: + notebooks: ${{ steps.check.outputs.notebooks }} + steps: + - uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Fetch main branch + run: git fetch origin main + + - name: Check if cookbook, pypfopt or pyproject.toml changed + id: check + run: | + if git diff --quiet origin/main -- cookbook/ pypfopt/ pyproject.toml; then + echo "No notebook related changes" + echo "notebooks=false" >> $GITHUB_OUTPUT + else + echo "Detected changes in notebooks or pypfopt" + echo "notebooks=true" >> $GITHUB_OUTPUT + fi + + run-notebook-examples: + needs: detect-notebooks-change + if: ${{ needs.detect-notebooks-change.outputs.notebooks == 'true' }} + runs-on: ubuntu-latest + strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] - fail-fast: false # to not fail all combinations if just one fails + python-version: ['3.10', '3.11', '3.12', '3.13', '3.14'] + fail-fast: false steps: - uses: actions/checkout@v6 @@ -69,39 +94,42 @@ jobs: uses: astral-sh/setup-uv@v7 with: enable-cache: true - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v6 - with: python-version: ${{ matrix.python-version }} - name: Display Python version run: python -c "import sys; print(sys.version)" + - name: Create virtual environment + run: uv venv + - name: Install dependencies shell: bash - run: uv pip install ".[dev]" --no-cache-dir - env: - UV_SYSTEM_PYTHON: 1 + run: uv pip install ".[all_extras,notebook_test]" --group dev --no-cache - name: Show dependencies run: uv pip list - - name: Test with pytest + - name: Collect notebooks + id: notebooks + shell: bash run: | - pytest ./tests + NOTEBOOKS=$(find cookbook -name '*.ipynb' -print0 | xargs -0 echo) + echo "notebooks=$NOTEBOOKS" >> $GITHUB_OUTPUT - pytest: - needs: pytest-nosoftdeps - name: (${{ matrix.python-version }}, ${{ matrix.os }}) + - name: Run notebooks + shell: bash + run: | + uv run pytest --reruns 3 --nbmake --nbmake-timeout=3600 -vv ${{ steps.notebooks.outputs.notebooks }} + + test-nosoftdeps: + needs: code-quality + name: test-nosoftdeps (${{ matrix.python-version }}, ${{ matrix.os }}) runs-on: ${{ matrix.os }} - env: - MPLBACKEND: Agg # https://github.com/orgs/community/discussions/26434 strategy: + fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] - fail-fast: false # to not fail all combinations if just one fails steps: - uses: actions/checkout@v6 @@ -110,38 +138,38 @@ jobs: uses: astral-sh/setup-uv@v7 with: enable-cache: true - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v6 - with: python-version: ${{ matrix.python-version }} + + - name: Create virtual environment + run: uv venv - name: Display Python version run: python -c "import sys; print(sys.version)" + - name: Force non-GUI Matplotlib backend (Windows) + if: ${{ matrix.os == 'windows-latest' }} + shell: pwsh + run: echo "MPLBACKEND=Agg" >> $env:GITHUB_ENV + - name: Install dependencies shell: bash - run: uv pip install ".[dev,all_extras]" --no-cache-dir - env: - UV_SYSTEM_PYTHON: 1 + run: uv pip install "." --group dev --no-cache - name: Show dependencies run: uv pip list - - name: Test with pytest - run: | - pytest ./tests + - name: Run tests + run: uv run pytest ./tests - codecov: - name: coverage (${{ matrix.python-version }} on ${{ matrix.os }} + test-full: + needs: test-nosoftdeps + name: test-full (${{ matrix.python-version }}, ${{ matrix.os }}) runs-on: ${{ matrix.os }} - needs: code-quality - env: - MPLBACKEND: Agg # https://github.com/orgs/community/discussions/26434 strategy: + fail-fast: false matrix: + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] os: [ubuntu-latest, macos-latest, windows-latest] - python-version: ["3.12"] steps: - uses: actions/checkout@v6 @@ -150,28 +178,64 @@ jobs: uses: astral-sh/setup-uv@v7 with: enable-cache: true + python-version: ${{ matrix.python-version }} + + - name: Create virtual environment + run: uv venv - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v6 + - name: Display Python version + run: python -c "import sys; print(sys.version)" + + - name: Force non-GUI Matplotlib backend (Windows) + if: ${{ matrix.os == 'windows-latest' }} + shell: pwsh + run: echo "MPLBACKEND=Agg" >> $env:GITHUB_ENV + + - name: Install dependencies + shell: bash + run: uv pip install -e ".[all_extras]" --group dev --no-cache + + - name: Show dependencies + run: uv pip list + + - name: Run tests + run: uv run pytest ./tests + + # TODO: should we run this as a substep of test-no-deps and only upload for a specific version? + codecov: + name: coverage + needs: code-quality + runs-on: "ubuntu-latest" + steps: + - uses: actions/checkout@v6 + + - name: Install uv + uses: astral-sh/setup-uv@v7 with: - python-version: ${{ matrix.python-version }} + enable-cache: true + python-version: 3.12 + + - name: Create virtual environment + run: uv venv - name: Display Python version run: python -c "import sys; print(sys.version)" + - name: Force non-GUI Matplotlib backend (Windows) + if: ${{ matrix.os == 'windows-latest' }} + shell: pwsh + run: echo "MPLBACKEND=Agg" >> $env:GITHUB_ENV + - name: Install dependencies shell: bash - run: uv pip install ".[dev,all_extras]" --no-cache-dir - env: - UV_SYSTEM_PYTHON: 1 + run: uv pip install . --group dev --group cov - name: Show dependencies run: uv pip list - name: Generate coverage report run: | - pip install pytest pytest-cov - pytest --cov=./ --cov-report=xml + uv run pytest --cov=./ --cov-report=xml - name: Upload coverage to Codecov # if false in order to skip for now @@ -180,51 +244,3 @@ jobs: with: files: ./coverage.xml fail_ci_if_error: true - - notebooks: - needs: code-quality - runs-on: ubuntu-latest - - strategy: - matrix: - python-version: [ '3.10', '3.11', '3.12', '3.13', '3.14' ] - fail-fast: false - - steps: - - uses: actions/checkout@v6 - - - name: Install uv - uses: astral-sh/setup-uv@v7 - with: - enable-cache: true - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v6 - with: - python-version: ${{ matrix.python-version }} - - - name: Display Python version - run: python -c "import sys; print(sys.version)" - - - name: Install dependencies - shell: bash - run: uv pip install ".[dev,all_extras,notebook_test]" --no-cache-dir - env: - UV_SYSTEM_PYTHON: 1 - - - name: Show dependencies - run: uv pip list - - # Discover all notebooks - - name: Collect notebooks - id: notebooks - shell: bash - run: | - NOTEBOOKS=$(find cookbook -name '*.ipynb' -print0 | xargs -0 echo) - echo "notebooks=$NOTEBOOKS" >> $GITHUB_OUTPUT - - # Run all discovered notebooks with nbmake - - name: Test notebooks - shell: bash - run: | - uv run pytest --reruns 3 --nbmake --nbmake-timeout=3600 -vv ${{ steps.notebooks.outputs.notebooks }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c6b50c32..5fc3a265 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: PyPI Release +name: Build wheels and publish to PyPI on: release: @@ -32,21 +32,24 @@ jobs: fi build_wheels: + needs: check_tag name: Build wheels runs-on: ubuntu-latest - needs: [check_tag] steps: - uses: actions/checkout@v6 - - uses: actions/setup-python@v6 + - name: Install uv + uses: astral-sh/setup-uv@v7 with: + enable-cache: true python-version: '3.11' - + - name: Build wheel run: | - python -m pip install build - python -m build --wheel --sdist --outdir wheelhouse + uv build --wheel --sdist --out-dir wheelhouse + env: + UV_SYSTEM_PYTHON: 1 - name: Store wheels uses: actions/upload-artifact@v7 @@ -54,50 +57,49 @@ jobs: name: wheels path: wheelhouse/* - pytest-nosoftdeps: - name: no-softdeps + test_wheels: + needs: build_wheels + name: Test wheels on ${{ matrix.os }} with ${{ matrix.python-version }} runs-on: ${{ matrix.os }} - needs: [build_wheels] strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + os: [windows-latest, ubuntu-latest, macos-latest] python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] steps: - uses: actions/checkout@v6 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v6 + - uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} - - name: Setup macOS - if: runner.os == 'macOS' - run: | - brew install libomp # https://github.com/pytorch/pytorch/issues/20030 + - uses: actions/download-artifact@v7 + with: + name: wheels + path: wheelhouse - - name: Get full Python version - id: full-python-version - shell: bash - run: echo version=$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info))") >> $GITHUB_OUTPUT + - name: Display downloaded artifacts + run: ls -l wheelhouse - - name: Install dependencies - shell: bash - run: | - pip install ".[dev]" + - name: Get wheel filename (Unix) + if: runner.os != 'Windows' + run: echo "WHEELNAME=$(ls ./wheelhouse/pyportfolioopt-*none-any.whl)" >> $GITHUB_ENV + + - name: Get wheel filename (Windows) + if: runner.os == 'Windows' + run: echo "WHEELNAME=$(ls ./wheelhouse/pyportfolioopt-*none-any.whl)" >> $env:GITHUB_ENV - - name: Show dependencies - run: python -m pip list + - name: Install wheel and extras + run: python3 -m pip install "${{ env.WHEELNAME }}[all_extras]" --group dev - - name: Run pytest - shell: bash + - name: Run tests run: python -m pytest tests upload_wheels: name: Upload wheels to PyPI runs-on: ubuntu-latest - needs: [pytest-nosoftdeps] + needs: [build_wheels, test_wheels] permissions: id-token: write diff --git a/.gitignore b/.gitignore index 4b974123..41862b80 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ dist artifacts bin + +# uv +uv.lock diff --git a/Makefile b/Makefile index dd22c0e0..2c577f98 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ RESET := \033[0m UV_INSTALL_DIR := ./bin +# TODO: I don't think we should install uv locally inside the repository, but rather rely on the user having it installed globally. This is because uv is a tool that is meant to be used across multiple projects, and installing it locally in each project can lead to version conflicts and unnecessary duplication. Instead, we can specify in the documentation that users should have uv installed globally, and provide instructions on how to do so if they don't already have it ##@ Bootstrap install-uv: ## ensure uv (and uvx) are installed locally @mkdir -p ${UV_INSTALL_DIR} diff --git a/pyproject.toml b/pyproject.toml index f73f7125..90c9feac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ authors = [ { name = "Robert Andrew Martin", email = "martin.robertandrew@gmail.com" }, ] readme = "README.md" +requires-python = ">=3.10,<3.15" keywords= ["finance", "portfolio", "optimization", "quant", "investing"] classifiers=[ "Development Status :: 4 - Beta", @@ -58,13 +59,6 @@ all_extras = [ "cvxopt; python_version < '3.14'", ] -# dev - the developer dependency set, for contributors and CI -dev = [ - "pytest>=9.0.0", - "pytest-cov>=7.0.0", - "yfinance>=0.2.66", -] - # notebook tests notebook_test = [ "nbmake", @@ -108,6 +102,16 @@ indent-style = "space" line-ending = "auto" skip-magic-trailing-comma = false +[dependency-groups] +cov = [ + "coverage>=7.13.4", + "pytest-cov>=7.0.0", +] +dev = [ + "pytest>=9.0.2", + "yfinance>=1.2.0", +] + [tool.ruff.lint.isort] known-first-party = ["pypfopt"] combine-as-imports = true