Skip to content

Commit 43f7416

Browse files
♻️ Refactor version validation to be simpler
1 parent 762e471 commit 43f7416

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/fastapi_new/new.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pathlib
22
import shutil
33
import subprocess
4-
from dataclasses import dataclass, field
4+
from dataclasses import dataclass
55
from typing import Annotated
66

77
import typer
@@ -22,7 +22,7 @@ def main():
2222
class ProjectConfig:
2323
name: str
2424
path: pathlib.Path
25-
extra_args: list[str] = field(default_factory=list)
25+
python: str | None = None
2626

2727

2828
def _generate_readme(project_name: str) -> str:
@@ -66,42 +66,39 @@ def _exit_with_error(toolkit: RichToolkit, error_msg: str) -> None:
6666
raise typer.Exit(code=1)
6767

6868

69-
def _validate_python_version_in_args(extra_args: list[str]) -> str | None:
69+
def _validate_python_version(python: str | None) -> str | None:
7070
"""
71-
Check if --python is specified in extra_args and validate it's >= 3.8.
71+
Validate Python version is >= 3.8.
7272
Returns error message if < 3.8, None otherwise.
7373
Let uv handle malformed versions or versions it can't find.
7474
"""
75-
if not extra_args:
75+
if not python:
7676
return None
7777

78-
for i, arg in enumerate(extra_args):
79-
if arg in ("--python", "-p") and i + 1 < len(extra_args):
80-
version_str = extra_args[i + 1]
81-
try:
82-
parts = version_str.split(".")
83-
if len(parts) < 2:
84-
return None # Let uv handle malformed version
85-
major, minor = int(parts[0]), int(parts[1])
86-
87-
if major < 3 or (major == 3 and minor < 8):
88-
return f"Python {version_str} is not supported. FastAPI requires Python 3.8 or higher."
89-
return None
90-
except (ValueError, IndexError):
91-
# Malformed version - let uv handle the error
92-
return None
78+
try:
79+
parts = python.split(".")
80+
if len(parts) < 2:
81+
return None # Let uv handle malformed version
82+
major, minor = int(parts[0]), int(parts[1])
83+
84+
if major < 3 or (major == 3 and minor < 8):
85+
return f"Python {python} is not supported. FastAPI requires Python 3.8 or higher."
86+
except (ValueError, IndexError):
87+
# Malformed version - let uv handle the error
88+
pass
89+
9390
return None
9491

9592

9693
def _setup(toolkit: RichToolkit, config: ProjectConfig) -> None:
97-
error = _validate_python_version_in_args(config.extra_args)
94+
error = _validate_python_version(config.python)
9895
if error:
9996
_exit_with_error(toolkit, error)
10097

10198
msg = "Setting up environment with uv"
10299

103-
if config.extra_args:
104-
msg += f" ({' '.join(config.extra_args)})"
100+
if config.python:
101+
msg += f" (Python {config.python})"
105102

106103
toolkit.print(msg, tag="env")
107104

@@ -112,8 +109,8 @@ def _setup(toolkit: RichToolkit, config: ProjectConfig) -> None:
112109
else:
113110
init_cmd = ["uv", "init", "--bare", config.name]
114111

115-
if config.extra_args:
116-
init_cmd.extend(config.extra_args)
112+
if config.python:
113+
init_cmd.extend(["--python", config.python])
117114

118115
try:
119116
subprocess.run(init_cmd, check=True, capture_output=True)
@@ -175,11 +172,9 @@ def new(
175172
config = ProjectConfig(
176173
name=name,
177174
path=path,
175+
python=python,
178176
)
179177

180-
if python:
181-
config.extra_args.extend(["--python", python])
182-
183178
with get_rich_toolkit() as toolkit:
184179
toolkit.print_title("Creating a new project 🚀", tag="FastAPI")
185180

0 commit comments

Comments
 (0)