From 5030b10163d67319979a0709f59636d631ea66c6 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 9 Sep 2024 16:45:39 -0400 Subject: [PATCH] Create `py.typed` files during `uv init --lib` --- crates/uv/src/commands/project/init.rs | 7 ++++++- crates/uv/tests/init.rs | 10 ++++++++++ docs/concepts/projects.md | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/uv/src/commands/project/init.rs b/crates/uv/src/commands/project/init.rs index fb0a2bd8b462..2186ff430ea2 100644 --- a/crates/uv/src/commands/project/init.rs +++ b/crates/uv/src/commands/project/init.rs @@ -537,9 +537,10 @@ impl InitProjectKind { // Create `src/{name}/__init__.py`, if it doesn't exist already. let src_dir = path.join("src").join(&*name.as_dist_info_name()); + fs_err::create_dir_all(&src_dir)?; + let init_py = src_dir.join("__init__.py"); if !init_py.try_exists()? { - fs_err::create_dir_all(&src_dir)?; fs_err::write( init_py, indoc::formatdoc! {r#" @@ -549,6 +550,10 @@ impl InitProjectKind { )?; } + // Create a `py.typed` file + let py_typed = src_dir.join("py.typed"); + fs_err::write(py_typed, "")?; + // Write .python-version if it doesn't exist. if let Some(python_request) = python_request { if PythonVersionFile::discover(path, false, false) diff --git a/crates/uv/tests/init.rs b/crates/uv/tests/init.rs index dc75c7a2e971..115371971f77 100644 --- a/crates/uv/tests/init.rs +++ b/crates/uv/tests/init.rs @@ -329,6 +329,7 @@ fn init_library() -> Result<()> { let pyproject_toml = child.join("pyproject.toml"); let init_py = child.join("src").join("foo").join("__init__.py"); + let py_typed = child.join("src").join("foo").join("py.typed"); uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--lib"), @r###" success: true @@ -372,6 +373,15 @@ fn init_library() -> Result<()> { ); }); + let py_typed = fs_err::read_to_string(py_typed)?; + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + py_typed, @"" + ); + }); + uv_snapshot!(context.filters(), context.run().current_dir(&child).arg("python").arg("-c").arg("import foo; print(foo.hello())"), @r###" success: true exit_code: 0 diff --git a/docs/concepts/projects.md b/docs/concepts/projects.md index 7f9924702dc2..541fc670f7c7 100644 --- a/docs/concepts/projects.md +++ b/docs/concepts/projects.md @@ -161,6 +161,7 @@ example-lib ├── pyproject.toml └── src └── example_lib + ├── py.typed └── __init__.py ```