Skip to content

Commit 5183865

Browse files
committed
fix: auto generate python files and fix module refs
1 parent 724bad0 commit 5183865

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

pyo3-stub-gen-derive/src/gen_stub/stub_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl ToTokens for StubType {
2121
#[automatically_derived]
2222
impl ::pyo3_stub_gen::PyStubType for #ty {
2323
fn type_output() -> ::pyo3_stub_gen::TypeInfo {
24-
::pyo3_stub_gen::TypeInfo::with_module_fix(#name, #module_tt, true)
24+
::pyo3_stub_gen::TypeInfo::with_type(#name, #module_tt)
2525
}
2626
}
2727
})

pyo3-stub-gen/src/stub_type.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ mod numpy;
77

88
use maplit::hashset;
99
use std::{collections::HashSet, fmt, ops};
10-
11-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
10+
/**
11+
* Indicates the dependent module or type;
12+
* dependent module('import module', eg builtins): name="" , module = module name;
13+
* dependent type(eg class enum)('from module import type'): name=type name , module = module name(which type defined).
14+
*/
15+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
1216
pub struct ModuleRef {
1317
pub name: String,
1418
pub module: String,
1519
}
16-
20+
/// From the dependent module
1721
impl From<&str> for ModuleRef {
1822
fn from(m: &str) -> Self {
1923
Self{name:"".to_string(), module:m.to_string()}
@@ -124,23 +128,32 @@ impl TypeInfo {
124128
/// ```
125129
/// pyo3_stub_gen::TypeInfo::with_module("pathlib.Path", "pathlib".into());
126130
/// ```
127-
pub fn with_module_fix(name: &str, module: ModuleRef, rust_flag: bool) -> Self {
131+
pub fn with_module(name: &str, module: ModuleRef) -> Self
132+
{
128133
let mut import = HashSet::new();
129-
//println!("{}", format!("name: {}, module: {}", name.to_string(), module));
130-
let mut module_fix = module.clone();
131-
if rust_flag {
132-
module_fix.name = name.to_string();
133-
}
134-
import.insert(module_fix);
134+
import.insert(module);
135135
Self {
136136
name: name.to_string(),
137137
import,
138138
}
139139
}
140140

141-
pub fn with_module(name: &str, module: ModuleRef) -> Self
142-
{
143-
TypeInfo::with_module_fix(name, module, false)
141+
/// A type annotation of a type that must be imported.
142+
///
143+
/// ```
144+
/// ClassA defined in ModuleA
145+
/// pyo3_stub_gen::TypeInfo::with_type("ClassA", "ModuleA");
146+
/// ```
147+
pub fn with_type(type_name: &str, module: ModuleRef) -> Self {
148+
let mut import = HashSet::new();
149+
let mut type_ref: ModuleRef = module.clone();
150+
type_ref.name = type_name.to_string();
151+
import.insert(type_ref);
152+
153+
Self {
154+
name: type_name.to_string(),
155+
import,
156+
}
144157
}
145158
}
146159

0 commit comments

Comments
 (0)