Skip to content

Commit

Permalink
chore: Migrate from hatch to uv (#660)
Browse files Browse the repository at this point in the history
* chore: Migrate from hatch to uv

* chore: Pin python 3.11

* Fix requirements
  • Loading branch information
manzt authored Aug 22, 2024
1 parent 462d70a commit bc31487
Show file tree
Hide file tree
Showing 8 changed files with 2,257 additions and 130 deletions.
79 changes: 28 additions & 51 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,50 @@ jobs:
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
# Disable jupyter-builder build hook because not needed for linting/formatting, and requires pnpm
- run: |
sed -i '' 's/\[tool.hatch.build.hooks.\(.*\)\]/\[_tool.hatch.build.hooks.\1\]/' pyproject.toml
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/0.3.1/install.sh | sh
- run: |
pip install uv
uv pip install --system ruff
ruff check
ruff format
uv run ruff check
uv run ruff format
TypecheckPython:
name: Python / Typecheck
runs-on: macos-14
steps:
- uses: actions/checkout@v4

# Disable jupyter-builder build hook because not needed for typechecking, and requires npm
# Disable jupyter-builder build hook because not needed for typechecking, and requires pnpm
- run: |
sed -i '' 's/\[tool.hatch.build.hooks.\(.*\)\]/\[_tool.hatch.build.hooks.\1\]/' pyproject.toml
cat pyproject.toml
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: |
pip install uv
uv pip install --system -e '.[test,dev]'
- run: mypy
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/0.3.1/install.sh | sh
- run: uv run mypy

TestPython:
name: Python / Test
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
- run: pnpm install --frozen-lock

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- if: ${{ matrix.python-version != '3.7' }}
run: |
pip install uv
uv pip install --system -e '.[test,dev]'
- if: ${{ matrix.python-version == '3.7' }}
run: |
pip install -e '.[test,dev]'
python-version-file: ".python-version"
- uses: pnpm/action-setup@v4
with:
run_install: true
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/0.3.1/install.sh | sh
- name: Run tests
run: pytest ./tests --color=yes --cov anywidget --cov-report xml

run: uv run --with pytest-cov pytest ./tests --color=yes --cov anywidget --cov-report xml
- uses: codecov/codecov-action@v4

LintJavaScript:
Expand All @@ -91,29 +75,22 @@ jobs:
version: latest
- run: biome ci .


TypecheckJavaScript:
name: JavaScript / Typecheck
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
- run: |
pnpm install
pnpm typecheck
run_install: true
- run: pnpm typecheck

TestJavaScript:
name: JavaScript / Test
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
- run: |
pnpm install
pnpm test
run_install: true
- run: pnpm test
20 changes: 4 additions & 16 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,16 @@ jobs:
issues: read
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
- run: pnpm install --frozen-lock

- uses: actions/setup-python@v5
with:
python-version: "3.11"
- run: |
python -m pip install --upgrade pip
pip install build twine
run_install: true
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/0.3.1/install.sh | sh
- name: Create Release Pull Request or Publish
id: changesets
uses: changesets/action@v1
with:
# this expects you to have a npm script called version that runs some logic and then calls `changeset version`.
version: npm run version
# This expects you to have a script called release which does a build for the packages and calls `changeset publish`
publish: npm run release
publish: pnpm release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12
3.11
40 changes: 16 additions & 24 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,16 @@ contains both JavaScript and Python components, it requires dual package
managers:

- [pnpm](https://pnpm.io) for JavaScript
- [hatch](https://github.com/pypa/hatch) for Python
- [uv](https://github.com/astral-sh/uv) for Python

You can [install pnpm](https://pnpm.io/installation) and
[install hatch](https://github.com/pypa/hatch) with:
[install uv](https://github.com/astral-sh/uv) with:

```bash
npm i -g pnpm
pipx install hatch
curl -LsSf https://astral.sh/uv/install.sh | sh # for Linux and macOS, see link for Windows
```

Then, create a Python virtual environment with `hatch`:

```bash
hatch shell
```

This command creates a virtual environment and installs all dependencies
specified in the `pyproject.toml`. It also installs **anywidget** in development
mode. The environment setup might take some time during the first run, but
`hatch` reuses the environment in subsequent runs.

You can deactivate the environment with `Ctrl + D` and reset it using
`hatch env remove`. For more information, read the
[hatch docs](https://hatch.pypa.io/latest/).

## Code structure

Entry points to be aware of:
Expand Down Expand Up @@ -60,12 +45,12 @@ Entry points to be aware of:
Once your environment is set up, you can start making changes to the codebase.
We recommend using the classic Jupyter Notebook or Jupyter Lab for development.

If you are using the classic Jupyter Notebook you need to install the local
If you are using the classic Jupyter Notebook (<v7) you need to install the local
nbextension:

```bash
jupyter nbextension install --py --symlink --sys-prefix anywidget
jupyter nbextension enable --py --sys-prefix anywidget
uv run jupyter nbextension install --py --symlink --sys-prefix anywidget
uv run jupyter nbextension enable --py --sys-prefix anywidget
```

Note for developers:
Expand All @@ -78,7 +63,7 @@ Note for developers:
For developing with JupyterLab:

```bash
jupyter labextension develop --overwrite anywidget
uv run jupyter labextension develop --overwrite anywidget
```

> **Note** If you make changes to the Python code, you'll need to restart the
Expand All @@ -94,8 +79,15 @@ There are a few guidelines we follow:

- For JavaScript, internal variables are written with `snake_case` while
external APIs are written with `camelCase` (if applicable).
- For Python, ensure `hatch run lint` passes. You can run `hatch run fmt` to
format and fix linting errors.
- For Python, ensure typechecking and linting passes.

Commands to know:

```sh
uv run ruff check # linting
uv run ruff format # formatting
uv run mypy # typechecking
```

### Generating changelogs

Expand Down
2 changes: 1 addition & 1 deletion anywidget/_cellmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def vfile(self, line: str, cell: str) -> None:
self._files[name] = vfile
_VIRTUAL_FILES[name] = vfile

@line_magic # type: ignore[misc]
@line_magic # type: ignore[misc]
def clear_vfiles(self, line: str) -> None:
"""Clear all virtual files."""
self._files.clear()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"build": "rspack build -c ./packages/anywidget/scripts/jlab.config.cjs",
"build:packages": "tsc --build && pnpm --filter=\"./packages/**\" -r build && pnpm publint",
"clean": "rm -rf anywidget/nbextension/index.js anywidget/labextension && pnpm -r exec rm -rf dist",
"version": "changeset version && pnpm install",
"release": "pnpm build:packages && python -m build && changeset publish && twine upload --skip-existing dist/*",
"version": "changeset version",
"release": "pnpm build:packages && uvx --from build pyproject-build && changeset publish && uvx twine upload --skip-existing dist/*",
"test": "vitest --environment=happy-dom --run",
"typecheck": "tsc --build && vitest --typecheck.only --run",
"fix": "biome check --write .",
Expand Down
54 changes: 19 additions & 35 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ authors = [
license = { text = "MIT" }
dynamic = ["version"]
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.8"
dependencies = [
"ipywidgets>=7.6.0",
"importlib-metadata; python_version < '3.8'",
"typing-extensions>=4.2.0",
"psygnal>=0.8.1",
]
Expand All @@ -29,33 +28,34 @@ classifiers = [
"Programming Language :: Python :: 3",
]

[project.urls]
homepage = "https://github.com/manzt/anywidget"

[project.optional-dependencies]
test = [
"mypy==1.11.1; python_version > '3.7'",
"pydantic",
"pytest",
"pytest-cov",
"ruff",
"msgspec; python_version > '3.7'",
"ipython<8.13; python_version < '3.9'",
]
dev = [
"comm>=0.1.0",
"watchfiles>=0.18.0",
dev = ["watchfiles>=0.18.0"]

[tool.uv]
dev-dependencies = [
"comm>=0.1.4",
"jupyterlab>=4.2.4",
"msgspec>=0.18.6",
"mypy>=1.11.1",
"pydantic>=2.5.3",
"pytest>=7.4.4",
"ruff>=0.6.1",
"watchfiles>=0.23.0",
]

[project.urls]
homepage = "https://github.com/manzt/anywidget"
[tool.uv.sources]
anywidget = { workspace = true }

[tool.hatch.build.targets.wheel.shared-data]
"anywidget/nbextension" = "share/jupyter/nbextensions/anywidget"
"anywidget/labextension" = "share/jupyter/labextensions/anywidget"
"anywidget.json" = "etc/jupyter/nbconfig/notebook.d/anywidget.json"

[tool.hatch.build]
exclude = [
".github",
]
exclude = [".github"]
artifacts = [
"anywidget/nbextension/index.*",
"anywidget/labextension/*.tgz",
Expand Down Expand Up @@ -84,22 +84,6 @@ build_cmd = "build"
path = "packages/anywidget/package.json"
pattern = "\"version\": \"(?P<version>.+?)\""

[tool.hatch.envs.default]
features = ["test", "dev"]
uv = true

[tool.hatch.envs.default.scripts]
lint = [
"ruff check {args:.}",
"ruff format --check --diff {args:.}",
]
fmt = [
"ruff format {args:.}",
"ruff check --fix {args:.}",
]
typecheck = "mypy anywidget"
test = "pytest . --cov anywidget --cov-report term-missing"

# https://github.com/charliermarsh/ruff
[tool.ruff]
line-length = 88
Expand Down
Loading

0 comments on commit bc31487

Please sign in to comment.