Skip to content

Commit 46827a1

Browse files
76 add type annotations in the python bindings (#86)
* Add python stub generation in CI. * Switch to a pure rust binding with a single module to avoid build weird issue.
1 parent 76d1a35 commit 46827a1

File tree

15 files changed

+203
-276
lines changed

15 files changed

+203
-276
lines changed

.github/workflows/python-upload.yml

Lines changed: 0 additions & 177 deletions
This file was deleted.

.github/workflows/python.yml

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
#
44
# maturin generate-ci github
55
#
6-
name: python-bindings-build-ci
6+
name: python-bindings
77

8-
on: [pull_request]
8+
on:
9+
push:
10+
branches:
11+
- main
12+
- master
13+
tags:
14+
- '*'
15+
pull_request:
16+
workflow_dispatch:
917

1018
permissions:
1119
contents: read
@@ -29,13 +37,21 @@ jobs:
2937
- uses: actions/setup-python@v5
3038
with:
3139
python-version: 3.x
40+
- name: Generate pyo3 stub
41+
run: |
42+
cargo run --bin stub_gen --features python
3243
- name: Build wheels
3344
uses: PyO3/maturin-action@v1
3445
with:
3546
target: ${{ matrix.platform.target }}
36-
args: --release --features python --out dist --find-interpreter
47+
args: --release --features python --features pyo3/extension-module --out dist --find-interpreter
3748
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
3849
manylinux: auto
50+
- name: Upload wheels
51+
uses: actions/upload-artifact@v4
52+
with:
53+
name: wheels-linux-${{ matrix.platform.target }}
54+
path: dist
3955

4056
musllinux:
4157
runs-on: ${{ matrix.platform.runner }}
@@ -55,13 +71,21 @@ jobs:
5571
- uses: actions/setup-python@v5
5672
with:
5773
python-version: 3.x
74+
- name: Generate pyo3 stub
75+
run: |
76+
cargo run --bin stub_gen --features python
5877
- name: Build wheels
5978
uses: PyO3/maturin-action@v1
6079
with:
6180
target: ${{ matrix.platform.target }}
62-
args: --release --features python --out dist --find-interpreter
81+
args: --release --features python --features pyo3/extension-module --out dist --find-interpreter
6382
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
6483
manylinux: musllinux_1_2
84+
- name: Upload wheels
85+
uses: actions/upload-artifact@v4
86+
with:
87+
name: wheels-musllinux-${{ matrix.platform.target }}
88+
path: dist
6589

6690
windows:
6791
runs-on: ${{ matrix.platform.runner }}
@@ -82,8 +106,13 @@ jobs:
82106
uses: PyO3/maturin-action@v1
83107
with:
84108
target: ${{ matrix.platform.target }}
85-
args: --release --features python --out dist --find-interpreter
109+
args: --release --features python --features pyo3/extension-module --out dist --find-interpreter
86110
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
111+
- name: Upload wheels
112+
uses: actions/upload-artifact@v4
113+
with:
114+
name: wheels-windows-${{ matrix.platform.target }}
115+
path: dist
87116

88117
macos:
89118
runs-on: ${{ matrix.platform.runner }}
@@ -99,19 +128,62 @@ jobs:
99128
- uses: actions/setup-python@v5
100129
with:
101130
python-version: 3.x
131+
- name: Generate pyo3 stub
132+
run: |
133+
cargo run --bin stub_gen --features python
102134
- name: Build wheels
103135
uses: PyO3/maturin-action@v1
104136
with:
105137
target: ${{ matrix.platform.target }}
106-
args: --release --features python --out dist --find-interpreter
138+
args: --release --features python --features pyo3/extension-module --out dist --find-interpreter
107139
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
140+
- name: Upload wheels
141+
uses: actions/upload-artifact@v4
142+
with:
143+
name: wheels-macos-${{ matrix.platform.target }}
144+
path: dist
108145

109146
sdist:
110147
runs-on: ubuntu-latest
111148
steps:
112149
- uses: actions/checkout@v4
150+
- name: Generate pyo3 stub
151+
run: |
152+
cargo run --bin stub_gen --features python
113153
- name: Build sdist
114154
uses: PyO3/maturin-action@v1
115155
with:
116156
command: sdist
117157
args: --out dist
158+
- name: Upload sdist
159+
uses: actions/upload-artifact@v4
160+
with:
161+
name: wheels-sdist
162+
path: dist
163+
164+
release:
165+
name: Release
166+
runs-on: ubuntu-latest
167+
if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }}
168+
needs: [linux, musllinux, windows, macos, sdist]
169+
permissions:
170+
# Use to sign the release artifacts
171+
id-token: write
172+
# Used to upload release artifacts
173+
contents: write
174+
# Used to generate artifact attestation
175+
attestations: write
176+
steps:
177+
- uses: actions/download-artifact@v4
178+
- name: Generate artifact attestation
179+
uses: actions/attest-build-provenance@v1
180+
with:
181+
subject-path: 'wheels-*/*'
182+
- name: Publish to PyPI
183+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
184+
uses: PyO3/maturin-action@v1
185+
env:
186+
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
187+
with:
188+
command: upload
189+
args: --non-interactive --skip-existing wheels-*/*

Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ crate-type = ["lib", "cdylib"]
1616

1717
[features]
1818
default = []
19-
python = ["dep:pyo3", "dep:pyo3-log"]
19+
python = ["dep:pyo3", "dep:pyo3-log", "dep:pyo3-stub-gen"]
2020

2121
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2222

@@ -28,8 +28,11 @@ clap = { version = "4.0.32", features = ["derive"] }
2828
proc-macro2 = { version = "1.0", features=["default", "proc-macro"] }
2929
signal-hook = "0.3.4"
3030
num_enum = "0.7.3"
31-
pyo3 = { version = "0.24.1", optional = true, features = ["multiple-pymethods", "extension-module"] }
31+
pyo3 = { version = "0.24.1", optional = true, features = ["multiple-pymethods"] }
3232
pyo3-log = { version = "0.12.3", optional = true }
33-
34-
[dev-dependencies]
33+
pyo3-stub-gen = { version = "0.9.0", optional = true }
3534
env_logger = "0.10.0"
35+
36+
[[bin]]
37+
name = "stub_gen"
38+
required-features = ["python"]

0 commit comments

Comments
 (0)