Skip to content

Commit

Permalink
Create py.typed files during uv init --lib (#7232)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Sep 10, 2024
1 parent c7ff70b commit b5cc913
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/concepts/projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ example-lib
├── pyproject.toml
└── src
└── example_lib
├── py.typed
└── __init__.py
```

Expand Down

0 comments on commit b5cc913

Please sign in to comment.