Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/fastapi/fastapi_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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<String> {
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)?;

Expand Down
4 changes: 2 additions & 2 deletions src/project_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -302,7 +302,7 @@ fn build_latest_dev_dependencies(project_info: &ProjectInfo) -> Result<String> {
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));
Expand Down
4 changes: 2 additions & 2 deletions src/python_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::{bail, Result};

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

if version_parts.1 >= 11 {
if version_parts.1 >= min_minor_version {
Ok(true)
} else {
Ok(false)
Expand Down Expand Up @@ -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);
}

Expand Down