Skip to content

Commit 513e1af

Browse files
committed
fix: run lint
1 parent a78be21 commit 513e1af

File tree

1 file changed

+68
-30
lines changed
  • crates/uv/src/commands/project

1 file changed

+68
-30
lines changed

crates/uv/src/commands/project/run.rs

+68-30
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use uv_fs::{PythonExt, Simplified};
2626
use uv_installer::{SatisfiesResult, SitePackages};
2727
use uv_normalize::PackageName;
2828
use uv_python::{
29-
EnvironmentPreference, Interpreter, PyVenvConfiguration, PythonDownloads, PythonEnvironment, PythonInstallation, PythonPreference, PythonRequest, PythonVersion, PythonVersionFile, VersionFileDiscoveryOptions
29+
EnvironmentPreference, Interpreter, PyVenvConfiguration, PythonDownloads, PythonEnvironment,
30+
PythonInstallation, PythonPreference, PythonRequest, PythonVersion, PythonVersionFile,
31+
VersionFileDiscoveryOptions,
3032
};
3133
use uv_requirements::{RequirementsSource, RequirementsSpecification};
3234
use uv_resolver::Lock;
@@ -1070,14 +1072,14 @@ hint: If you are running a script with `{}` in the shebang, you may need to incl
10701072
if interpreter.is_virtualenv() {
10711073
process.env(EnvVars::VIRTUAL_ENV, interpreter.sys_prefix().as_os_str());
10721074
};
1073-
1075+
10741076
// Spawn and wait for completion
10751077
// Standard input, output, and error streams are all inherited
10761078
// TODO(zanieb): Throw a nicer error message if the command is not found
10771079
let handle = process
10781080
.spawn()
10791081
.map_err(|err| {
1080-
let executable: Cow<'_, str> = command.display_executable();
1082+
let executable: Cow<'_, str> = command.display_executable();
10811083
// Special case for providing meaningful error message when users
10821084
// attempt to invoke python. E.g. "python3.11".
10831085
// Will not work if patch version is provided. I.E. "python3.11.9"
@@ -1092,13 +1094,11 @@ hint: If you are running a script with `{}` in the shebang, you may need to incl
10921094
// Construct the message dynamically
10931095
let message_suffix = if project_found {
10941096
format!(
1095-
"Did you mean to change the environment to Python {} with `uv run -p {} python`?",
1096-
version_part, version_part
1097+
"Did you mean to change the environment to Python {version_part} with `uv run -p {version_part} python`?"
10971098
)
10981099
} else {
10991100
format!(
1100-
"Did you mean to search for a Python {} environment with `uv run -p {} python`?",
1101-
version_part, version_part
1101+
"Did you mean to search for a Python {version_part} environment with `uv run -p {version_part} python`?"
11021102
)
11031103
};
11041104
anyhow!(
@@ -1557,76 +1557,114 @@ fn read_recursion_depth_from_environment_variable() -> anyhow::Result<u32> {
15571557
.with_context(|| format!("invalid value for {}", EnvVars::UV_RUN_RECURSION_DEPTH))
15581558
}
15591559

1560-
15611560
/// Matches valid Python executable names:
15621561
/// - ✅ "python", "python3", "python3.9", "python4", "python3.10", "python3.13.3"
15631562
/// - ❌ "python39", "python3abc", "python3.12b3", "", "python-foo"
15641563
fn is_python_executable(executable_command: &str) -> bool {
15651564
executable_command
15661565
.strip_prefix("python")
1567-
.map_or(false, |version| version.len() == 0 || is_valid_python_version(version))
1566+
.is_some_and(|version| {
1567+
version.is_empty() || is_valid_python_version(version)
1568+
})
15681569
}
15691570

15701571
/// Checks if a version string is a valid Python major.minor.patch version.
15711572
fn is_valid_python_version(version: &str) -> bool {
1572-
PythonVersion::from_str(version)
1573-
.map_or(false,
1574-
|ver|
1575-
ver.is_stable() &&
1573+
PythonVersion::from_str(version).is_ok_and(|ver| {
1574+
ver.is_stable() &&
15761575
// Should not contain post info. E.g. "3.12b3"
15771576
!ver.is_post()
1578-
)
1579-
}#[cfg(test)]
1577+
})
1578+
}
1579+
#[cfg(test)]
15801580
mod tests {
15811581
use super::{is_python_executable, is_valid_python_version};
15821582

15831583
/// Helper function for asserting test cases.
15841584
/// - If `expected_result` is `true`, it expects the function to return `true` (valid cases).
15851585
/// - If `expected_result` is `false`, it expects the function to return `false` (invalid cases).
1586-
fn assert_cases<F: Fn(&str) -> bool>(cases: &[&str], func: F, test_name: &str, expected_result: bool) {
1586+
fn assert_cases<F: Fn(&str) -> bool>(
1587+
cases: &[&str],
1588+
func: F,
1589+
test_name: &str,
1590+
expected_result: bool,
1591+
) {
15871592
for &case in cases {
1593+
let result = func(case);
15881594
assert_eq!(
1589-
func(case),
1595+
result,
15901596
expected_result,
1591-
"{}: Expected {} but failed on case: {}",
1592-
test_name,
1593-
expected_result,
1594-
case
1597+
"{test_name}: Expected `{expected_result}`, but got `{result}` for case `{case}`"
15951598
);
15961599
}
15971600
}
15981601

15991602
#[test]
16001603
fn valid_is_python_executable() {
16011604
let valid_cases = [
1602-
"python3", "python3.9", "python3.10", "python4", "python",
1605+
"python3",
1606+
"python3.9",
1607+
"python3.10",
1608+
"python4",
1609+
"python",
16031610
"python3.11.3",
16041611
"python39", // Still a valid executable, although likely a typo
16051612
];
1606-
assert_cases(&valid_cases, is_python_executable, "valid_is_python_executable", true);
1613+
assert_cases(
1614+
&valid_cases,
1615+
is_python_executable,
1616+
"valid_is_python_executable",
1617+
true,
1618+
);
16071619
}
16081620

16091621
#[test]
16101622
fn invalid_is_python_executable() {
16111623
let invalid_cases = [
1612-
"python-foo", "python3abc", "python3.12b3",
1613-
"pyth0n3", "", "Python3.9", "python.3.9"
1624+
"python-foo",
1625+
"python3abc",
1626+
"python3.12b3",
1627+
"pyth0n3",
1628+
"",
1629+
"Python3.9",
1630+
"python.3.9",
16141631
];
1615-
assert_cases(&invalid_cases, is_python_executable, "invalid_is_python_executable", false);
1632+
assert_cases(
1633+
&invalid_cases,
1634+
is_python_executable,
1635+
"invalid_is_python_executable",
1636+
false,
1637+
);
16161638
}
16171639

16181640
#[test]
16191641
fn valid_python_versions() {
16201642
let valid_cases = ["3", "3.9", "4", "3.10", "49", "3.11.3"];
1621-
assert_cases(&valid_cases, is_valid_python_version, "valid_python_versions", true);
1643+
assert_cases(
1644+
&valid_cases,
1645+
is_valid_python_version,
1646+
"valid_python_versions",
1647+
true,
1648+
);
16221649
}
16231650

16241651
#[test]
16251652
fn invalid_python_versions() {
16261653
let invalid_cases = [
1627-
"3.12b3", "3.12rc1", "3.12a1",
1628-
"3.12.post1", "3.12.1-foo", "3abc", "..", ""
1654+
"3.12b3",
1655+
"3.12rc1",
1656+
"3.12a1",
1657+
"3.12.post1",
1658+
"3.12.1-foo",
1659+
"3abc",
1660+
"..",
1661+
"",
16291662
];
1630-
assert_cases(&invalid_cases, is_valid_python_version, "invalid_python_versions", false);
1663+
assert_cases(
1664+
&invalid_cases,
1665+
is_valid_python_version,
1666+
"invalid_python_versions",
1667+
false,
1668+
);
16311669
}
16321670
}

0 commit comments

Comments
 (0)