Skip to content

Commit 1db0c36

Browse files
fliskybadboy
authored andcommitted
Fix python relative import
1 parent 6d46b3f commit 1db0c36

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

uniffi_bindgen/src/bindings/python/filters.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@ pub fn docstring(docstring: &Option<String>, indent: usize) -> Result<String> {
3030
let indented = textwrap::indent(&escaped, &indent);
3131
Ok(format!("\"\"\"\n{indented}\n\"\"\"\n{indent}"))
3232
}
33+
34+
/// Get the idiomatic Python import statement for a module
35+
pub fn import_statement(module: &str) -> Result<String> {
36+
Ok(if module.starts_with('.') {
37+
let Some((from, name)) = module.rsplit_once('.') else {
38+
unreachable!()
39+
};
40+
let from = if from.is_empty() { "." } else { from };
41+
format!("from {from} import {name}")
42+
} else {
43+
format!("import {module}")
44+
})
45+
}

uniffi_bindgen/src/bindings/python/templates/Module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
{%- endif %}
3232
import platform
3333
{%- for import in imports %}
34-
import {{ import }}
34+
{{ import|import_statement }}
3535
{%- endfor %}
3636

3737

0 commit comments

Comments
 (0)