diff --git a/src/fastapi/fastapi_files.rs b/src/fastapi/fastapi_files.rs index ae0e00d0..14f07744 100644 --- a/src/fastapi/fastapi_files.rs +++ b/src/fastapi/fastapi_files.rs @@ -34,6 +34,7 @@ use crate::{ }, file_manager::save_file_with_content, project_info::{DatabaseManager, ProjectInfo}, + utils::is_python_version_or_greater, }; pub fn generate_fastapi(project_info: &ProjectInfo) -> Result<()> { @@ -279,19 +280,28 @@ fn save_main_file(project_info: &ProjectInfo) -> Result<()> { Ok(()) } -fn create_types_file() -> String { - r#"from typing import Any, Literal, TypeAlias +fn create_types_file(project_info: &ProjectInfo) -> Result { + if is_python_version_or_greater(&project_info.min_python_version, 12)? { + Ok(r#"from typing import Any, Literal + +type ActiveFilter = Literal["all", "active", "inactive"] +type Json = dict[str, Any] +"# + .to_string()) + } else { + Ok(r#"from typing import Any, Literal, TypeAlias ActiveFilter: TypeAlias = Literal["all", "active", "inactive"] Json: TypeAlias = dict[str, Any] "# - .to_string() + .to_string()) + } } fn save_types_file(project_info: &ProjectInfo) -> Result<()> { let base = project_info.source_dir_path(); let file_path = base.join("types.py"); - let file_content = create_types_file(); + let file_content = create_types_file(project_info)?; save_file_with_content(&file_path, &file_content)?; diff --git a/src/project_generator.rs b/src/project_generator.rs index 39d12288..cccf8dde 100644 --- a/src/project_generator.rs +++ b/src/project_generator.rs @@ -17,7 +17,7 @@ use crate::{ project_info::{LicenseType, ProjectInfo, ProjectManager, Pyo3PythonManager}, python_files::generate_python_files, rust_files::{save_cargo_toml_file, save_lib_file}, - utils::is_python_311_or_greater, + utils::is_python_version_or_greater, }; #[cfg(feature = "fastapi")] @@ -302,7 +302,7 @@ fn build_latest_dev_dependencies(project_info: &ProjectInfo) -> Result { packages.push(PythonPackageVersion::new(PythonPackage::PytestCov)); packages.push(PythonPackageVersion::new(PythonPackage::Ruff)); - if !is_python_311_or_greater(&project_info.min_python_version)? + if !is_python_version_or_greater(&project_info.min_python_version, 11)? && matches!(project_info.project_manager, ProjectManager::Poetry) { packages.push(PythonPackageVersion::new(PythonPackage::Tomli)); diff --git a/src/python_files.rs b/src/python_files.rs index da1b1534..21486543 100644 --- a/src/python_files.rs +++ b/src/python_files.rs @@ -5,7 +5,7 @@ use anyhow::{bail, Result}; use crate::{ file_manager::save_file_with_content, project_info::{ProjectInfo, ProjectManager}, - utils::is_python_311_or_greater, + utils::is_python_version_or_greater, }; fn create_dunder_main_file(module: &str, is_async_project: bool) -> String { @@ -235,7 +235,7 @@ fn create_version_test_file( }; if let Some(v) = version_test { - if is_python_311_or_greater(min_python_version)? { + if is_python_version_or_greater(min_python_version, 11)? { Ok(Some(format!( r#"import tomllib from pathlib import Path diff --git a/src/utils.rs b/src/utils.rs index bb1642b8..8c3a111a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,9 @@ use anyhow::{bail, Result}; -pub fn is_python_311_or_greater(version: &str) -> Result { +pub fn is_python_version_or_greater(version: &str, min_minor_version: i32) -> Result { let version_parts = split_version(version)?; - if version_parts.1 >= 11 { + if version_parts.1 >= min_minor_version { Ok(true) } else { Ok(false) @@ -39,19 +39,19 @@ mod tests { #[test] fn test_python_312() { - let result = is_python_311_or_greater("3.12").unwrap(); + let result = is_python_version_or_greater("3.12", 11).unwrap(); assert!(result); } #[test] fn test_python_311_311() { - let result = is_python_311_or_greater("3.11").unwrap(); + let result = is_python_version_or_greater("3.11", 11).unwrap(); assert!(result); } #[test] fn test_python_311_310() { - let result = is_python_311_or_greater("3.10").unwrap(); + let result = is_python_version_or_greater("3.10", 11).unwrap(); assert!(!result); }