Skip to content

Commit 8b1e33a

Browse files
committed
python: generate py stubs
Signed-off-by: Tristram Gräbener <tristram+git@tristramg.eu>
1 parent 4a95795 commit 8b1e33a

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

python/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ pip install
3737
maturin develop
3838
```
3939

40-
Bindings with PyO3 to liblrs
40+
If you change the python interface, you will need to generate the subs file with:
41+
42+
```
43+
cargo run --bin stub_gen
44+
```
4145

4246
## Publishing
4347

python/src/bin/stub_gen.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use pyo3_stub_gen::Result;
2+
3+
fn main() -> Result<()> {
4+
// `stub_info` is a function defined by `define_stub_info_gatherer!` macro.
5+
let stub = liblrs_python::stub_info()?;
6+
stub.generate()?;
7+
Ok(())
8+
}

python/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use liblrs::lrs::LrmHandle;
77
use liblrs::lrs::{LrsBase, Properties};
88
use liblrs::lrs_ext::*;
99
use pyo3::{exceptions::PyTypeError, prelude::*};
10+
use pyo3_stub_gen::{define_stub_info_gatherer, derive::*};
1011

1112
/// Holds the whole Linear Referencing System.
13+
#[gen_stub_pyclass]
1214
#[pyclass]
1315
pub struct Lrs {
1416
lrs: ExtLrs,
@@ -30,6 +32,7 @@ fn liblrs_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
3032

3133
#[derive(Clone, Copy, Debug)]
3234
/// A geographical [`Point`], it can be either a projected or spherical coordinates.
35+
#[gen_stub_pyclass]
3336
#[pyclass]
3437
pub struct Point {
3538
/// Position on x-axis or `longitude`.
@@ -40,6 +43,7 @@ pub struct Point {
4043
pub y: f64,
4144
}
4245

46+
#[gen_stub_pymethods]
4347
#[pymethods]
4448
impl Point {
4549
#[new]
@@ -90,6 +94,7 @@ impl From<Point> for geo_types::Coord {
9094

9195
#[derive(Clone, Debug)]
9296
/// A Node is a topological element of the [`Lrs`] that represents a intersection (or an extremity) of an [`Lrm`]
97+
#[gen_stub_pyclass]
9398
#[pyclass]
9499
pub struct Node {
95100
/// Identifies this [`Node`].
@@ -117,6 +122,7 @@ impl From<&liblrs::lrs::Node> for Node {
117122
/// A segment is a topological element of the [`Lrs`] that represents a piece of the [`Curve`] of an [`Lrm`]
118123
///
119124
/// It has a start and end [`Node`].
125+
#[gen_stub_pyclass]
120126
#[pyclass]
121127
pub struct Segment {
122128
/// Identifies this [`Segment`]
@@ -146,6 +152,7 @@ impl From<&liblrs::lrs::Segment> for Segment {
146152

147153
#[pyclass]
148154
#[derive(Clone, Debug)]
155+
#[gen_stub_pyclass]
149156
/// Represent a position on an [`LrmScale`] relative as an `offset` to an [`Anchor`].
150157
pub struct LrmScaleMeasure {
151158
#[pyo3(get, set)]
@@ -174,6 +181,7 @@ impl From<&LrmScaleMeasure> for liblrs::lrm_scale::LrmScaleMeasure {
174181
}
175182
}
176183

184+
#[gen_stub_pymethods]
177185
#[pymethods]
178186
impl LrmScaleMeasure {
179187
#[new]
@@ -191,6 +199,7 @@ impl LrmScaleMeasure {
191199
}
192200

193201
#[derive(Clone, Copy)]
202+
#[gen_stub_pyclass]
194203
#[pyclass]
195204
/// A traversal is composed by segments
196205
pub struct SegmentOfTraversal {
@@ -202,6 +211,7 @@ pub struct SegmentOfTraversal {
202211
pub reversed: bool,
203212
}
204213

214+
#[gen_stub_pymethods]
205215
#[pymethods]
206216
impl SegmentOfTraversal {
207217
#[new]
@@ -223,6 +233,7 @@ impl From<SegmentOfTraversal> for liblrs::builder::SegmentOfTraversal {
223233
}
224234

225235
#[derive(Clone, Copy, Debug)]
236+
#[gen_stub_pyclass]
226237
#[pyclass]
227238
/// The linear position of an anchor doesn’t always match the measured distance
228239
/// For example if a road was transformed into a bypass, resulting in a longer road,
@@ -238,6 +249,7 @@ pub struct AnchorOnLrm {
238249
pub distance_along_lrm: f64,
239250
}
240251

252+
#[gen_stub_pymethods]
241253
#[pymethods]
242254
impl AnchorOnLrm {
243255
#[new]
@@ -263,6 +275,7 @@ impl From<AnchorOnLrm> for liblrs::builder::AnchorOnLrm {
263275
}
264276

265277
#[derive(Debug)]
278+
#[gen_stub_pyclass]
266279
#[pyclass]
267280
/// An `Anchor` is a reference point for a given [`Curve`].
268281
/// It can be a milestone, a bridge…
@@ -303,6 +316,7 @@ impl From<&liblrs::lrm_scale::Anchor> for Anchor {
303316
}
304317
}
305318

319+
#[gen_stub_pyclass]
306320
#[pyclass]
307321
/// The result of a projection onto an [`LrmScale`].
308322
pub struct LrmProjection {
@@ -330,11 +344,12 @@ impl From<&liblrs::lrs::LrmProjection> for LrmProjection {
330344
}
331345
}
332346

347+
#[gen_stub_pymethods]
333348
#[pymethods]
334349
impl Lrs {
335350
/// Load the data.
336351
#[new]
337-
pub fn load(data: &[u8]) -> PyResult<Lrs> {
352+
pub fn load(#[gen_stub(override_type(type_repr = "bytes"))] data: &[u8]) -> PyResult<Lrs> {
338353
ExtLrs::load(data)
339354
.map(|lrs| Self { lrs })
340355
.map_err(|e| PyTypeError::new_err(e.to_string()))
@@ -462,11 +477,13 @@ impl Lrs {
462477
}
463478
}
464479

480+
#[gen_stub_pyclass]
465481
#[pyclass]
466482
struct Builder {
467483
inner: liblrs::builder::Builder<'static>,
468484
}
469485

486+
#[gen_stub_pymethods]
470487
#[pymethods]
471488
impl Builder {
472489
#[new]
@@ -637,3 +654,5 @@ impl Builder {
637654
.unwrap()
638655
}
639656
}
657+
658+
define_stub_info_gatherer!(stub_info);

python/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)