Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hashlib
import http.client
import json
import keyword
import os
import re
import secrets
Expand Down Expand Up @@ -110,6 +111,8 @@
"while",
"yield",
}
# Top-level import packages the generated Python project depends on at runtime.
_PYTHON_TEMPLATE_DEPENDENCY_PACKAGES = frozenset({"google", "grpc"})
_RUST_RESERVED_IDENTIFIERS = _RUST_KEYWORDS | {
"alloc",
"build",
Expand Down Expand Up @@ -375,14 +378,21 @@ def _template_context(name: str, language: str, package_name: str | None) -> Tem
distribution_name = re.sub(r"[._]+", "-", normalized_name)
identifier = re.sub(r"[^a-z0-9]+", "_", normalized_name).strip("_")
derived_package = identifier
if not derived_package or not derived_package[0].isalpha():
if (
not derived_package
or not derived_package[0].isalpha()
or _python_package_conflict(derived_package) is not None
):
derived_package = f"middleware_{derived_package}".rstrip("_")
effective_package = package_name if package_name is not None else derived_package
if not _PYTHON_PACKAGE_PATTERN.fullmatch(effective_package):
raise ProjectError(
"Python package name must start with a lowercase letter and contain only "
"lowercase letters, digits, and underscores"
)
conflict = _python_package_conflict(effective_package)
if conflict is not None:
raise ProjectError(f"Python package name '{effective_package}' {conflict}")
service_name = normalized_name.replace("_", "-").replace(".", "-")
rust_lib_name = identifier
rust_crate_name = distribution_name
Expand All @@ -399,6 +409,17 @@ def _template_context(name: str, language: str, package_name: str | None) -> Tem
)


def _python_package_conflict(package: str) -> str | None:
"""Explain why a top-level package name cannot be imported from the project."""
if keyword.iskeyword(package):
return "is a Python keyword"
if package in sys.stdlib_module_names:
return "shadows a Python standard-library module"
if package in _PYTHON_TEMPLATE_DEPENDENCY_PACKAGES:
return "shadows a package the generated project depends on"
return None


def _normalize_version(requested: str) -> str:
value = requested.strip()
if value == "latest":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ dev = [
requires = ["uv_build>=0.11.8,<0.12.0"]
build-backend = "uv_build"

[tool.uv.build-backend]
module-name = "__PACKAGE_NAME__"

[tool.pytest.ini_options]
testpaths = ["tests"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ def test_python_package_name_can_be_overridden(tmp_path: Path) -> None:
)

assert (destination / "src/custom_package/server.py").is_file()
pyproject = (destination / "pyproject.toml").read_text()
assert '[tool.uv.build-backend]\nmodule-name = "custom_package"' in pyproject


def test_numeric_project_name_gets_importable_python_package(tmp_path: Path) -> None:
Expand All @@ -625,6 +627,54 @@ def test_numeric_project_name_gets_importable_python_package(tmp_path: Path) ->
)

assert (destination / "src/middleware_123/server.py").is_file()
pyproject = (destination / "pyproject.toml").read_text()
assert '[tool.uv.build-backend]\nmodule-name = "middleware_123"' in pyproject


@pytest.mark.parametrize("name", ["assert", "logging", "secrets", "http", "grpc", "google"])
def test_reserved_project_names_get_importable_python_package(tmp_path: Path, name: str) -> None:
destination = tmp_path / name

create_project(
name=name,
language="python",
requested_version="v0.0.86",
destination=destination,
download_proto=local_proto,
command_runner=no_op_runner,
)

assert (destination / f"src/middleware_{name}/server.py").is_file()
assert not (destination / f"src/{name}").exists()
pyproject = (destination / "pyproject.toml").read_text()
assert f'module-name = "middleware_{name}"' in pyproject


@pytest.mark.parametrize(
("package_name", "message"),
[
("class", "Python keyword"),
("logging", "standard-library module"),
("grpc", "generated project depends on"),
],
)
def test_rejects_unimportable_python_package_override(
tmp_path: Path, package_name: str, message: str
) -> None:
destination = tmp_path / "project"

with pytest.raises(ProjectError, match=message):
create_project(
name="project",
language="python",
requested_version="v0.0.86",
destination=destination,
package_name=package_name,
download_proto=local_proto,
command_runner=no_op_runner,
)

assert not destination.exists()


@pytest.mark.parametrize(
Expand Down
Loading