Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create stubs without a pyproject.toml #99

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions pyo3-stub-gen/src/generate/stub_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ use std::{collections::BTreeMap, fs, io::Write, path::*};
#[derive(Debug, Clone, PartialEq)]
pub struct StubInfo {
pub modules: BTreeMap<String, Module>,
pub pyproject: PyProject,
pub python_root: PathBuf,
}

impl StubInfo {
/// Initialize [StubInfo] from a `pyproject.toml` file in `CARGO_MANIFEST_DIR`.
/// This is automatically set up by the [crate::define_stub_info_gatherer] macro.
pub fn from_pyproject_toml(path: impl AsRef<Path>) -> Result<Self> {
let pyproject = PyProject::parse_toml(path)?;
Ok(StubInfoBuilder::new(pyproject).build())
Ok(StubInfoBuilder::from_pyproject_toml(pyproject).build())
}

pub fn generate(&self) -> Result<()> {
let python_root = self
.pyproject
.python_source()
.unwrap_or(PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()));
/// Initialize [StubInfo] with a specific module name and project root.
/// This must be placed in your PyO3 library crate, i.e. the same crate where [inventory::submit]ted,
/// not in the `gen_stub` executables due to [inventory]'s mechanism.
pub fn from_project_root(default_module_name: String, project_root: PathBuf) -> Result<Self> {
Ok(StubInfoBuilder::from_project_root(default_module_name, project_root).build())
}

pub fn generate(&self) -> Result<()> {
for (name, module) in self.modules.iter() {
let path = name.replace(".", "/");
let dest = if module.submodules.is_empty() {
python_root.join(format!("{path}.pyi"))
self.python_root.join(format!("{path}.pyi"))
} else {
python_root.join(path).join("__init__.pyi")
self.python_root.join(path).join("__init__.pyi")
};

let dir = dest.parent().context("Cannot get parent directory")?;
Expand All @@ -47,15 +51,24 @@ impl StubInfo {
struct StubInfoBuilder {
modules: BTreeMap<String, Module>,
default_module_name: String,
pyproject: PyProject,
python_root: PathBuf,
}

impl StubInfoBuilder {
fn new(pyproject: PyProject) -> Self {
fn from_pyproject_toml(pyproject: PyProject) -> Self {
StubInfoBuilder::from_project_root(
pyproject.module_name().to_string(),
pyproject
.python_source()
.unwrap_or(PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())),
)
}

fn from_project_root(default_module_name: String, project_root: PathBuf) -> Self {
Self {
modules: BTreeMap::new(),
default_module_name: pyproject.module_name().to_string(),
pyproject,
default_module_name,
python_root: project_root,
}
}

Expand Down Expand Up @@ -158,7 +171,7 @@ impl StubInfoBuilder {
self.register_submodules();
StubInfo {
modules: self.modules,
pyproject: self.pyproject,
python_root: self.python_root,
}
}
}