Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create py.typed files during uv init --lib #7232

Merged
merged 1 commit into from
Sep 10, 2024
Merged
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
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, "")?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be within the if !init_py.try_exists()?... Otherwise, we're creating src/py.typed even if the source is contained elsewhere in an existing project.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code isn't super clear because of that variable name, but it's actually src/<module>/py.typed and src/<module>/__init__.py. I think this is reasonable to create even if the __init__.py already exists since we're (in theory) initializing the metadata to publish the library.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any possibility that this might clobber a file that might already exist?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 🤦‍♀️ #7338


// 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
Loading