Skip to content

Commit 92edb7a

Browse files
committed
Fix upgrade lint error with python >= 3.12
1 parent c9c5b6f commit 92edb7a

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/fastapi/fastapi_files.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::{
3434
},
3535
file_manager::save_file_with_content,
3636
project_info::{DatabaseManager, ProjectInfo},
37+
utils::is_python_version_or_greater,
3738
};
3839

3940
pub fn generate_fastapi(project_info: &ProjectInfo) -> Result<()> {
@@ -279,19 +280,28 @@ fn save_main_file(project_info: &ProjectInfo) -> Result<()> {
279280
Ok(())
280281
}
281282

282-
fn create_types_file() -> String {
283-
r#"from typing import Any, Literal, TypeAlias
283+
fn create_types_file(project_info: &ProjectInfo) -> Result<String> {
284+
if is_python_version_or_greater(&project_info.min_python_version, 12)? {
285+
Ok(r#"from typing import Any, Literal
286+
287+
type ActiveFilter = Literal["all", "active", "inactive"]
288+
type Json = dict[str, Any]
289+
"#
290+
.to_string())
291+
} else {
292+
Ok(r#"from typing import Any, Literal, TypeAlias
284293
285294
ActiveFilter: TypeAlias = Literal["all", "active", "inactive"]
286295
Json: TypeAlias = dict[str, Any]
287296
"#
288-
.to_string()
297+
.to_string())
298+
}
289299
}
290300

291301
fn save_types_file(project_info: &ProjectInfo) -> Result<()> {
292302
let base = project_info.source_dir_path();
293303
let file_path = base.join("types.py");
294-
let file_content = create_types_file();
304+
let file_content = create_types_file(project_info)?;
295305

296306
save_file_with_content(&file_path, &file_content)?;
297307

src/project_generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
project_info::{LicenseType, ProjectInfo, ProjectManager, Pyo3PythonManager},
1818
python_files::generate_python_files,
1919
rust_files::{save_cargo_toml_file, save_lib_file},
20-
utils::is_python_311_or_greater,
20+
utils::is_python_version_or_greater,
2121
};
2222

2323
#[cfg(feature = "fastapi")]
@@ -302,7 +302,7 @@ fn build_latest_dev_dependencies(project_info: &ProjectInfo) -> Result<String> {
302302
packages.push(PythonPackageVersion::new(PythonPackage::PytestCov));
303303
packages.push(PythonPackageVersion::new(PythonPackage::Ruff));
304304

305-
if !is_python_311_or_greater(&project_info.min_python_version)?
305+
if !is_python_version_or_greater(&project_info.min_python_version, 11)?
306306
&& matches!(project_info.project_manager, ProjectManager::Poetry)
307307
{
308308
packages.push(PythonPackageVersion::new(PythonPackage::Tomli));

src/python_files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anyhow::{bail, Result};
55
use crate::{
66
file_manager::save_file_with_content,
77
project_info::{ProjectInfo, ProjectManager},
8-
utils::is_python_311_or_greater,
8+
utils::is_python_version_or_greater,
99
};
1010

1111
fn create_dunder_main_file(module: &str, is_async_project: bool) -> String {
@@ -235,7 +235,7 @@ fn create_version_test_file(
235235
};
236236

237237
if let Some(v) = version_test {
238-
if is_python_311_or_greater(min_python_version)? {
238+
if is_python_version_or_greater(min_python_version, 11)? {
239239
Ok(Some(format!(
240240
r#"import tomllib
241241
from pathlib import Path

src/utils.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use anyhow::{bail, Result};
22

3-
pub fn is_python_311_or_greater(version: &str) -> Result<bool> {
3+
pub fn is_python_version_or_greater(version: &str, min_minor_version: i32) -> Result<bool> {
44
let version_parts = split_version(version)?;
55

6-
if version_parts.1 >= 11 {
6+
if version_parts.1 >= min_minor_version {
77
Ok(true)
88
} else {
99
Ok(false)
@@ -39,19 +39,19 @@ mod tests {
3939

4040
#[test]
4141
fn test_python_312() {
42-
let result = is_python_311_or_greater("3.12").unwrap();
42+
let result = is_python_version_or_greater("3.12", 11).unwrap();
4343
assert!(result);
4444
}
4545

4646
#[test]
4747
fn test_python_311_311() {
48-
let result = is_python_311_or_greater("3.11").unwrap();
48+
let result = is_python_version_or_greater("3.11", 11).unwrap();
4949
assert!(result);
5050
}
5151

5252
#[test]
5353
fn test_python_311_310() {
54-
let result = is_python_311_or_greater("3.10").unwrap();
54+
let result = is_python_version_or_greater("3.10", 11).unwrap();
5555
assert!(!result);
5656
}
5757

0 commit comments

Comments
 (0)