Skip to content

Commit bc5b7ab

Browse files
authored
Fix path of the stub file for the root module (#30)
1 parent 1a65bad commit bc5b7ab

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pyo3-stub-gen/src/generate.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{pyproject::PyProject, type_info::*};
44

5-
use anyhow::{anyhow, bail, Result};
5+
use anyhow::{anyhow, bail, ensure, Result};
66
use itertools::Itertools;
77
use pyo3::inspect::types::TypeInfo;
88
use std::{
@@ -462,10 +462,11 @@ impl StubInfo {
462462

463463
for (name, module) in self.modules.iter() {
464464
let path: Vec<&str> = name.split('.').collect();
465+
ensure!(!path.is_empty(), "Empty module name");
465466
let dest = if path.len() > 1 {
466467
python_source.join(format!("{}.pyi", path.join("/")))
467468
} else {
468-
python_source.join("__init__.pyi")
469+
python_source.join(path[0]).join("__init__.pyi")
469470
};
470471

471472
if let Some(dir) = dest.parent() {

pyo3-stub-gen/src/pyproject.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use std::{fs, path::*};
1818
pub struct PyProject {
1919
pub project: Project,
2020
pub tool: Option<Tool>,
21+
22+
#[serde(skip)]
23+
toml_path: PathBuf,
2124
}
2225

2326
impl PyProject {
@@ -26,7 +29,8 @@ impl PyProject {
2629
if path.file_name() != Some("pyproject.toml".as_ref()) {
2730
bail!("{} is not a pyproject.toml", path.display())
2831
}
29-
let out = toml::de::from_str(&fs::read_to_string(path)?)?;
32+
let mut out: PyProject = toml::de::from_str(&fs::read_to_string(path)?)?;
33+
out.toml_path = path.to_path_buf();
3034
Ok(out)
3135
}
3236

@@ -42,11 +46,15 @@ impl PyProject {
4246
}
4347

4448
/// Return `tool.maturin.python_source` if it exists, which means the project is a mixed Rust/Python project.
45-
pub fn python_source(&self) -> Option<&Path> {
49+
pub fn python_source(&self) -> Option<PathBuf> {
4650
if let Some(tool) = &self.tool {
4751
if let Some(maturin) = &tool.maturin {
4852
if let Some(python_source) = &maturin.python_source {
49-
return Some(Path::new(python_source));
53+
if let Some(base) = self.toml_path.parent() {
54+
return Some(base.join(python_source));
55+
} else {
56+
return Some(PathBuf::from(python_source));
57+
}
5058
}
5159
}
5260
}

0 commit comments

Comments
 (0)