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 1 commit
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
Prev Previous commit
Add StubInfo::from_project_root
  • Loading branch information
tvanbaak committed Nov 1, 2024
commit 2460bc07675b11bf33641d5f5c99aea21e38336d
24 changes: 20 additions & 4 deletions pyo3-stub-gen/src/generate/stub_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ pub struct StubInfo {
}

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::from_pyproject_toml(pyproject).build())
}

/// 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(".", "/");
Expand Down Expand Up @@ -47,12 +56,19 @@ struct StubInfoBuilder {

impl StubInfoBuilder {
fn from_pyproject_toml(pyproject: PyProject) -> Self {
Self {
modules: BTreeMap::new(),
default_module_name: pyproject.module_name().to_string(),
python_root: pyproject
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,
python_root: project_root,
}
}

Expand Down