Skip to content

Commit 762e471

Browse files
♻️ Use --bare and add explicit support for --python
1 parent a0c1c9d commit 762e471

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

src/fastapi_new/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
app = typer.Typer(rich_markup_mode="rich")
66

7-
app.command(
8-
context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
9-
)(new_command)
7+
app.command()(new_command)
108

119

1210
def main() -> None:

src/fastapi_new/new.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def _setup(toolkit: RichToolkit, config: ProjectConfig) -> None:
108108
# If config.name is provided, create in subdirectory; otherwise init in current dir
109109
# uv will infer the project name from the directory name
110110
if config.path == pathlib.Path.cwd():
111-
init_cmd = ["uv", "init"]
111+
init_cmd = ["uv", "init", "--bare"]
112112
else:
113-
init_cmd = ["uv", "init", config.name]
113+
init_cmd = ["uv", "init", "--bare", config.name]
114114

115115
if config.extra_args:
116116
init_cmd.extend(config.extra_args)
@@ -156,6 +156,14 @@ def new(
156156
help="The name of the new FastAPI Cloud project. If not provided, initializes in the current directory.",
157157
),
158158
] = None,
159+
python: Annotated[
160+
str | None,
161+
typer.Option(
162+
"--python",
163+
"-p",
164+
help="Specify the Python version for the new project (e.g., 3.9). Must be 3.8 or higher.",
165+
),
166+
] = None,
159167
) -> None:
160168
if project_name:
161169
name = project_name
@@ -167,9 +175,11 @@ def new(
167175
config = ProjectConfig(
168176
name=name,
169177
path=path,
170-
extra_args=ctx.args if hasattr(ctx, "args") else [],
171178
)
172179

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

tests/test_new.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ def temp_project_dir(tmp_path: Path, monkeypatch: Any) -> Path:
1919

2020

2121
class TestNewCommand:
22-
def _assert_project_created(
23-
self, project_path: Path, check_version_file: bool = False
24-
) -> None:
22+
def _assert_project_created(self, project_path: Path) -> None:
2523
assert (project_path / "main.py").exists()
2624
assert (project_path / "README.md").exists()
2725
assert (project_path / "pyproject.toml").exists()
28-
if check_version_file:
29-
assert (project_path / ".python-version").exists()
3026

3127
def test_creates_project_successfully(self, temp_project_dir: Path) -> None:
3228
result = runner.invoke(app, ["my_fastapi_project"])
@@ -42,22 +38,14 @@ def test_creates_project_with_python_version(self, temp_project_dir: Path) -> No
4238
result = runner.invoke(app, ["project_long", "--python", "3.12"])
4339
assert result.exit_code == 0
4440
project_path = temp_project_dir / "project_long"
45-
self._assert_project_created(project_path, check_version_file=True)
46-
assert "3.12" in (project_path / ".python-version").read_text()
41+
self._assert_project_created(project_path)
42+
assert "3.12" in (project_path / "pyproject.toml").read_text()
4743

4844
# Test short form
4945
result = runner.invoke(app, ["project_short", "-p", "3.11"])
5046
assert result.exit_code == 0
5147
project_path = temp_project_dir / "project_short"
52-
assert "3.11" in (project_path / ".python-version").read_text()
53-
54-
def test_creates_project_with_extra_uv_flags(self, temp_project_dir: Path) -> None:
55-
"""Test that extra flags are passed through to uv."""
56-
result = runner.invoke(app, ["my_fastapi_project", "--python", "3.12", "--lib"])
57-
58-
assert result.exit_code == 0
59-
project_path = temp_project_dir / "my_fastapi_project"
60-
self._assert_project_created(project_path)
48+
assert "3.11" in (project_path / "pyproject.toml").read_text()
6149

6250
def test_validates_template_file_contents(self, temp_project_dir: Path) -> None:
6351
result = runner.invoke(app, ["sample_project"])
@@ -122,14 +110,6 @@ def test_creates_project_without_python_flag(self, temp_project_dir: Path) -> No
122110
project_path = temp_project_dir / "test_project"
123111
self._assert_project_created(project_path)
124112

125-
def test_creates_project_with_other_uv_flags_no_python(
126-
self, temp_project_dir: Path
127-
) -> None:
128-
result = runner.invoke(app, ["test_project", "--lib"])
129-
assert result.exit_code == 0
130-
project_path = temp_project_dir / "test_project"
131-
self._assert_project_created(project_path)
132-
133113

134114
class TestNewCommandUvFailures:
135115
def test_failed_to_initialize_with_uv(self, monkeypatch: Any) -> None:

0 commit comments

Comments
 (0)