Skip to content

Commit d262660

Browse files
authored
Fix - handling in package name (#242)
Resolve #109
1 parent a0ab679 commit d262660

File tree

11 files changed

+105
-1
lines changed

11 files changed

+105
-1
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ members = [
66
"examples/mixed",
77
"examples/mixed_sub",
88
"examples/mixed_sub_multiple",
9+
"examples/test-dash-package",
910
]
1011
resolver = "2"
1112

examples/test-dash-package/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

examples/test-dash-package/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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"

pyo3-stub-gen/src/generate/stub_info.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ impl StubInfo {
3030

3131
pub fn generate(&self) -> Result<()> {
3232
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(".", "/");
3436
let dest = if module.submodules.is_empty() {
3537
self.python_root.join(format!("{path}.pyi"))
3638
} else {

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"examples/mixed",
55
"examples/mixed_sub",
66
"examples/mixed_sub_multiple",
7+
"examples/test-dash-package",
78
]
89

910
[dependency-groups]

0 commit comments

Comments
 (0)