Skip to content

Commit 911c57b

Browse files
committed
Pull __version__ from the Cargo.toml in pyoo3 projects
1 parent cada1e7 commit 911c57b

8 files changed

+17
-69
lines changed

src/python_files.rs

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::project_info::{ProjectInfo, ProjectManager};
77
use crate::utils::is_python_312_or_greater;
88

99
fn create_dunder_main_file(module: &str, is_async_project: bool) -> String {
10-
let mut file = "from __future__ import annotations\n\n".to_string();
10+
let mut file = "from __future__ import annotations # pragma: no cover\n\n".to_string();
1111

1212
if is_async_project {
1313
file.push_str("import asyncio\n\n");
@@ -139,44 +139,12 @@ fn save_pyo3_test_file(project_info: &ProjectInfo) -> Result<()> {
139139
fn create_project_init_file(module: &str, project_manager: &ProjectManager) -> String {
140140
match project_manager {
141141
ProjectManager::Maturin => {
142-
// 118 = the letter v
143-
let v_ascii: u8 = 118;
144-
if let Some(first_char) = module.chars().next() {
145-
if (first_char as u8) < v_ascii {
146-
format!(
147-
r#"from {module}._{module} import sum_as_string
148-
from {module}._version import VERSION
149-
150-
__version__ = VERSION
151-
152-
153-
__all__ = ["sum_as_string"]
154-
"#
155-
)
156-
} else {
157-
format!(
158-
r#"from {module}._version import VERSION
159-
from {module}._{module} import sum_as_string
160-
161-
__version__ = VERSION
162-
163-
164-
__all__ = ["sum_as_string"]
165-
"#
166-
)
167-
}
168-
} else {
169-
format!(
170-
r#"from {module}._{module} import sum_as_string
171-
r#"from {module}._version import VERSION
172-
173-
__version__ = VERSION
174-
142+
format!(
143+
r#"from {module}._{module} import __version__, sum_as_string
175144
176-
__all__ = ["sum_as_string"]
145+
__all__ = ["__version__", "sum_as_string"]
177146
"#
178-
)
179-
}
147+
)
180148
}
181149
_ => {
182150
format!(
@@ -211,6 +179,8 @@ fn save_project_init_file(project_info: &ProjectInfo) -> Result<()> {
211179
fn create_pyi_file() -> String {
212180
r#"from __future__ import annotations
213181
182+
__version__: str
183+
214184
def sum_as_string(a: int, b: int) -> str: ...
215185
"#
216186
.to_string()
@@ -250,15 +220,6 @@ fn create_version_test_file(
250220
min_python_version: &str,
251221
) -> Result<Option<String>> {
252222
let version_test: Option<&str> = match project_manager {
253-
ProjectManager::Maturin => Some(
254-
r#"def test_versions_match():
255-
cargo = Path().absolute() / "Cargo.toml"
256-
with open(cargo, "rb") as f:
257-
data = tomllib.load(f)
258-
cargo_version = data["package"]["version"]
259-
260-
assert VERSION == cargo_version"#,
261-
),
262223
ProjectManager::Poetry => Some(
263224
r#"def test_versions_match():
264225
pyproject = Path().absolute() / "pyproject.toml"
@@ -340,7 +301,9 @@ pub fn generate_python_files(project_info: &ProjectInfo) -> Result<()> {
340301
}
341302
}
342303

343-
if save_version_file(project_info).is_err() {
304+
if project_info.project_manager != ProjectManager::Maturin
305+
&& save_version_file(project_info).is_err()
306+
{
344307
bail!("Error creating version file");
345308
}
346309

@@ -627,22 +590,6 @@ mod tests {
627590
assert_yaml_snapshot!(content);
628591
}
629592

630-
#[test]
631-
fn test_save_version_test_file_pyo3() {
632-
let mut project_info = project_info_dummy();
633-
project_info.project_manager = ProjectManager::Maturin;
634-
let base = project_info.base_dir();
635-
create_dir_all(base.join("tests")).unwrap();
636-
let expected_file = base.join("tests/test_version.py");
637-
save_version_test_file(&project_info).unwrap();
638-
639-
assert!(expected_file.is_file());
640-
641-
let content = std::fs::read_to_string(expected_file).unwrap();
642-
643-
assert_yaml_snapshot!(content);
644-
}
645-
646593
#[test]
647594
fn test_save_version_test_file_setuptools() {
648595
let mut project_info = project_info_dummy();

src/rust_files.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {{
110110
#[pymodule]
111111
fn _{module}(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {{
112112
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
113+
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
113114
Ok(())
114115
}}
115116
"#

src/snapshots/python_project__python_files__tests__save_main_files.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/python_files.rs
33
expression: dunder_main_content
44
---
5-
"from __future__ import annotations\n\nfrom my_project.main import main # pragma: no cover\n\nif __name__ == \"__main__\":\n raise SystemExit(main())\n"
5+
"from __future__ import annotations # pragma: no cover\n\nfrom my_project.main import main # pragma: no cover\n\nif __name__ == \"__main__\":\n raise SystemExit(main())\n"

src/snapshots/python_project__python_files__tests__save_main_files_is_async_project.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/python_files.rs
33
expression: dunder_main_content
44
---
5-
"from __future__ import annotations\n\nimport asyncio\n\nfrom my_project.main import main # pragma: no cover\n\nif __name__ == \"__main__\":\n raise SystemExit(asyncio.run(main()))\n"
5+
"from __future__ import annotations # pragma: no cover\n\nimport asyncio\n\nfrom my_project.main import main # pragma: no cover\n\nif __name__ == \"__main__\":\n raise SystemExit(asyncio.run(main()))\n"

src/snapshots/python_project__python_files__tests__save_project_init_file_pyo3_first.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/python_files.rs
33
expression: content
44
---
5-
"from my_project._my_project import sum_as_string\nfrom my_project._version import VERSION\n\n__version__ = VERSION\n\n\n__all__ = [\"sum_as_string\"]\n"
5+
"from my_project._my_project import __version__, sum_as_string\n\n__all__ = [\"__version__\", \"sum_as_string\"]\n"

src/snapshots/python_project__python_files__tests__save_project_init_file_pyo3_last.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/python_files.rs
33
expression: content
44
---
5-
"from z_my_project._version import VERSION\nfrom z_my_project._z_my_project import sum_as_string\n\n__version__ = VERSION\n\n\n__all__ = [\"sum_as_string\"]\n"
5+
"from z_my_project._z_my_project import __version__, sum_as_string\n\n__all__ = [\"__version__\", \"sum_as_string\"]\n"

src/snapshots/python_project__python_files__tests__save_pyi_file.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/python_files.rs
33
expression: content
44
---
5-
"from __future__ import annotations\n\ndef sum_as_string(a: int, b: int) -> str: ...\n"
5+
"from __future__ import annotations\n\n__version__: str\n\ndef sum_as_string(a: int, b: int) -> str: ...\n"

src/snapshots/python_project__rust_files__tests__save_lib_file.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/rust_files.rs
33
expression: content
44
---
5-
"use pyo3::prelude::*;\n\n#[pyfunction]\nfn sum_as_string(a: usize, b: usize) -> PyResult<String> {\n Ok((a + b).to_string())\n}\n\n#[pymodule]\nfn _my_project(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {\n m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;\n Ok(())\n}\n"
5+
"use pyo3::prelude::*;\n\n#[pyfunction]\nfn sum_as_string(a: usize, b: usize) -> PyResult<String> {\n Ok((a + b).to_string())\n}\n\n#[pymodule]\nfn _my_project(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {\n m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;\n m.add(\"__version__\", env!(\"CARGO_PKG_VERSION\"))?;\n Ok(())\n}\n"

0 commit comments

Comments
 (0)