Skip to content

Avoid writing empty requires-python to script blocks #12517

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

Merged
merged 1 commit into from
Mar 28, 2025
Merged
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
134 changes: 77 additions & 57 deletions crates/uv-scripts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,18 @@ impl Pep723Script {
requires_python: &VersionSpecifiers,
) -> Result<(String, Pep723Metadata, String), Pep723Error> {
// Define the default metadata.
let default_metadata = indoc::formatdoc! {r#"
requires-python = "{requires_python}"
dependencies = []
"#,
requires_python = requires_python,
let default_metadata = if requires_python.is_empty() {
indoc::formatdoc! {r"
dependencies = []
",
}
} else {
indoc::formatdoc! {r#"
requires-python = "{requires_python}"
dependencies = []
"#,
requires_python = requires_python,
}
};
let metadata = Pep723Metadata::from_str(&default_metadata)?;

Expand Down Expand Up @@ -581,6 +588,7 @@ fn serialize_metadata(metadata: &str) -> String {
#[cfg(test)]
mod tests {
use crate::{serialize_metadata, Pep723Error, Pep723Script, ScriptTag};
use std::str::FromStr;

#[test]
fn missing_space() {
Expand Down Expand Up @@ -750,24 +758,24 @@ mod tests {
#[test]
fn trailing_lines() {
let contents = indoc::indoc! {r"
# /// script
# requires-python = '>=3.11'
# dependencies = [
# 'requests<3',
# 'rich',
# ]
# ///
#
#
"};
# /// script
# requires-python = '>=3.11'
# dependencies = [
# 'requests<3',
# 'rich',
# ]
# ///
#
#
"};

let expected = indoc::indoc! {r"
requires-python = '>=3.11'
dependencies = [
'requests<3',
'rich',
]
"};
requires-python = '>=3.11'
dependencies = [
'requests<3',
'rich',
]
"};

let actual = ScriptTag::parse(contents.as_bytes())
.unwrap()
Expand All @@ -780,22 +788,22 @@ mod tests {
#[test]
fn serialize_metadata_formatting() {
let metadata = indoc::indoc! {r"
requires-python = '>=3.11'
dependencies = [
'requests<3',
'rich',
]
"};
requires-python = '>=3.11'
dependencies = [
'requests<3',
'rich',
]
"};

let expected_output = indoc::indoc! {r"
# /// script
# requires-python = '>=3.11'
# dependencies = [
# 'requests<3',
# 'rich',
# ]
# ///
"};
# /// script
# requires-python = '>=3.11'
# dependencies = [
# 'requests<3',
# 'rich',
# ]
# ///
"};

let result = serialize_metadata(metadata);
assert_eq!(result, expected_output);
Expand All @@ -817,10 +825,28 @@ mod tests {
Pep723Script::init_metadata(contents, &uv_pep440::VersionSpecifiers::default())
.unwrap();
assert_eq!(prelude, "");
assert_eq!(
metadata.raw,
indoc::indoc! {r"
dependencies = []
"}
);
assert_eq!(postlude, "");
}

#[test]
fn script_init_requires_python() {
let contents = "".as_bytes();
let (prelude, metadata, postlude) = Pep723Script::init_metadata(
contents,
&uv_pep440::VersionSpecifiers::from_str(">=3.8").unwrap(),
)
.unwrap();
assert_eq!(prelude, "");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
requires-python = ">=3.8"
dependencies = []
"#}
);
Expand All @@ -841,10 +867,9 @@ mod tests {
assert_eq!(prelude, "#!/usr/bin/env python3\n");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
indoc::indoc! {r"
dependencies = []
"#}
"}
);
assert_eq!(
postlude,
Expand Down Expand Up @@ -873,12 +898,11 @@ mod tests {
assert_eq!(prelude, "");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
indoc::indoc! {r"
dependencies = []
"#}
"}
);
// Note the extra line at the beginning
// Note the extra line at the beginning.
assert_eq!(
postlude,
indoc::indoc! {r#"
Expand Down Expand Up @@ -913,10 +937,9 @@ mod tests {
assert_eq!(prelude, "#!/usr/bin/env python3\n");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
indoc::indoc! {r"
dependencies = []
"#}
"}
);
// Note the extra line at the beginning.
assert_eq!(
Expand Down Expand Up @@ -952,12 +975,11 @@ mod tests {
assert_eq!(prelude, "");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
indoc::indoc! {r"
dependencies = []
"#}
"}
);
// Note the extra line at the beginning
// Note the extra line at the beginning.
assert_eq!(
postlude,
indoc::indoc! {r#"
Expand Down Expand Up @@ -991,12 +1013,11 @@ mod tests {
assert_eq!(prelude, "");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
indoc::indoc! {r"
dependencies = []
"#}
"}
);
// Note the extra line at the beginning
// Note the extra line at the beginning.
assert_eq!(
postlude,
indoc::indoc! {r#"
Expand Down Expand Up @@ -1030,10 +1051,9 @@ mod tests {
assert_eq!(prelude, "");
assert_eq!(
metadata.raw,
indoc::indoc! {r#"
requires-python = ""
indoc::indoc! {r"
dependencies = []
"#}
"}
);
assert_eq!(
postlude,
Expand Down
Loading