File tree Expand file tree Collapse file tree 11 files changed +105
-1
lines changed
examples/test-dash-package
pyo3-stub-gen/src/generate Expand file tree Collapse file tree 11 files changed +105
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ members = [
6
6
" examples/mixed" ,
7
7
" examples/mixed_sub" ,
8
8
" examples/mixed_sub_multiple" ,
9
+ " examples/test-dash-package" ,
9
10
]
10
11
resolver = " 2"
11
12
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " test-dash-package"
3
+ version = " 0.1.0"
4
+ edition = " 2021"
5
+
6
+ [lib ]
7
+ name = " test_dash_package"
8
+ crate-type = [" cdylib" , " rlib" ]
9
+
10
+ [dependencies ]
11
+ pyo3 = { workspace = true }
12
+ pyo3-stub-gen = { path = " ../../pyo3-stub-gen" }
13
+
14
+ [[bin ]]
15
+ name = " stub_gen"
16
+ path = " src/bin/stub_gen.rs"
Original file line number Diff line number Diff line change
1
+ # https://taskfile.dev
2
+ # yaml-language-server: $schema=https://taskfile.dev/schema.json
3
+ version : " 3"
4
+
5
+ tasks :
6
+ stub-gen :
7
+ desc : Generate stub file
8
+ cmds :
9
+ - cargo run --bin stub_gen
10
+
11
+ test :
12
+ desc : Run tests
13
+ cmds :
14
+ - uv run pytest
15
+ - uv run pyright
16
+ - uvx ruff check
Original file line number Diff line number Diff line change
1
+ [project ]
2
+ # maturin will convert `-` to `_` in the package name automatically
3
+ # This module tests pyo3-stub-gen correctly handles package names with dashes
4
+ name = " test-dash-package"
5
+ version = " 0.1.0"
6
+
7
+ [build-system ]
8
+ requires = [" maturin>=1" ]
9
+ build-backend = " maturin"
10
+
11
+ [tool .maturin ]
12
+ # For cases where we define `module-name` explicitly here are tested in other examples.
Original file line number Diff line number Diff line change
1
+ use pyo3_stub_gen:: Result ;
2
+
3
+ fn main ( ) -> Result < ( ) > {
4
+ let stub = test_dash_package:: stub_info ( ) ?;
5
+ stub. generate ( ) ?;
6
+ Ok ( ( ) )
7
+ }
Original file line number Diff line number Diff line change
1
+ use pyo3:: prelude:: * ;
2
+ use pyo3_stub_gen:: { define_stub_info_gatherer, derive:: gen_stub_pyfunction} ;
3
+
4
+ #[ gen_stub_pyfunction]
5
+ #[ pyfunction]
6
+ fn test_function ( ) -> i32 {
7
+ 42
8
+ }
9
+
10
+ #[ pymodule]
11
+ fn test_dash_package ( _py : Python , m : & Bound < PyModule > ) -> PyResult < ( ) > {
12
+ m. add_function ( wrap_pyfunction ! ( test_function, m) ?) ?;
13
+ Ok ( ( ) )
14
+ }
15
+
16
+ define_stub_info_gatherer ! ( stub_info) ;
Original file line number Diff line number Diff line change
1
+ """Test that the module can be imported correctly despite having a dash in the package name."""
2
+
3
+ import test_dash_package
4
+ import pathlib
5
+
6
+
7
+ def test_test_function ():
8
+ assert test_dash_package .test_function () == 42
9
+
10
+
11
+ def test_stub_file_naming ():
12
+ """Test that the stub file was generated with underscores instead of dashes."""
13
+ # The stub file should have underscores, not dashes
14
+ stub_file = pathlib .Path (__file__ ).parent .parent / "test_dash_package.pyi"
15
+ assert stub_file .exists (), "Stub file with underscores should exist"
16
+
17
+ # The old dash version should not exist
18
+ dash_stub_file = pathlib .Path (__file__ ).parent .parent / "test-dash-package.pyi"
19
+ assert not dash_stub_file .exists (), "Stub file with dashes should not exist"
Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ impl StubInfo {
30
30
31
31
pub fn generate ( & self ) -> Result < ( ) > {
32
32
for ( name, module) in self . modules . iter ( ) {
33
- let path = name. replace ( "." , "/" ) ;
33
+ // Convert dashes to underscores for Python compatibility
34
+ let normalized_name = name. replace ( "-" , "_" ) ;
35
+ let path = normalized_name. replace ( "." , "/" ) ;
34
36
let dest = if module. submodules . is_empty ( ) {
35
37
self . python_root . join ( format ! ( "{path}.pyi" ) )
36
38
} else {
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ members = [
4
4
" examples/mixed" ,
5
5
" examples/mixed_sub" ,
6
6
" examples/mixed_sub_multiple" ,
7
+ " examples/test-dash-package" ,
7
8
]
8
9
9
10
[dependency-groups ]
You can’t perform that action at this time.
0 commit comments