Skip to content

Commit c363629

Browse files
committed
fix: run lint
1 parent a78be21 commit c363629

File tree

1 file changed

+66
-31
lines changed
  • crates/uv/src/commands/project

1 file changed

+66
-31
lines changed

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

+66-31
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,111 @@ 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| version.is_empty() || is_valid_python_version(version))
15681567
}
15691568

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

15831581
/// Helper function for asserting test cases.
15841582
/// - If `expected_result` is `true`, it expects the function to return `true` (valid cases).
15851583
/// - 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) {
1584+
fn assert_cases<F: Fn(&str) -> bool>(
1585+
cases: &[&str],
1586+
func: F,
1587+
test_name: &str,
1588+
expected_result: bool,
1589+
) {
15871590
for &case in cases {
1591+
let result = func(case);
15881592
assert_eq!(
1589-
func(case),
1590-
expected_result,
1591-
"{}: Expected {} but failed on case: {}",
1592-
test_name,
1593-
expected_result,
1594-
case
1593+
result, expected_result,
1594+
"{test_name}: Expected `{expected_result}`, but got `{result}` for case `{case}`"
15951595
);
15961596
}
15971597
}
15981598

15991599
#[test]
16001600
fn valid_is_python_executable() {
16011601
let valid_cases = [
1602-
"python3", "python3.9", "python3.10", "python4", "python",
1602+
"python3",
1603+
"python3.9",
1604+
"python3.10",
1605+
"python4",
1606+
"python",
16031607
"python3.11.3",
16041608
"python39", // Still a valid executable, although likely a typo
16051609
];
1606-
assert_cases(&valid_cases, is_python_executable, "valid_is_python_executable", true);
1610+
assert_cases(
1611+
&valid_cases,
1612+
is_python_executable,
1613+
"valid_is_python_executable",
1614+
true,
1615+
);
16071616
}
16081617

16091618
#[test]
16101619
fn invalid_is_python_executable() {
16111620
let invalid_cases = [
1612-
"python-foo", "python3abc", "python3.12b3",
1613-
"pyth0n3", "", "Python3.9", "python.3.9"
1621+
"python-foo",
1622+
"python3abc",
1623+
"python3.12b3",
1624+
"pyth0n3",
1625+
"",
1626+
"Python3.9",
1627+
"python.3.9",
16141628
];
1615-
assert_cases(&invalid_cases, is_python_executable, "invalid_is_python_executable", false);
1629+
assert_cases(
1630+
&invalid_cases,
1631+
is_python_executable,
1632+
"invalid_is_python_executable",
1633+
false,
1634+
);
16161635
}
16171636

16181637
#[test]
16191638
fn valid_python_versions() {
16201639
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);
1640+
assert_cases(
1641+
&valid_cases,
1642+
is_valid_python_version,
1643+
"valid_python_versions",
1644+
true,
1645+
);
16221646
}
16231647

16241648
#[test]
16251649
fn invalid_python_versions() {
16261650
let invalid_cases = [
1627-
"3.12b3", "3.12rc1", "3.12a1",
1628-
"3.12.post1", "3.12.1-foo", "3abc", "..", ""
1651+
"3.12b3",
1652+
"3.12rc1",
1653+
"3.12a1",
1654+
"3.12.post1",
1655+
"3.12.1-foo",
1656+
"3abc",
1657+
"..",
1658+
"",
16291659
];
1630-
assert_cases(&invalid_cases, is_valid_python_version, "invalid_python_versions", false);
1660+
assert_cases(
1661+
&invalid_cases,
1662+
is_valid_python_version,
1663+
"invalid_python_versions",
1664+
false,
1665+
);
16311666
}
16321667
}

0 commit comments

Comments
 (0)