Repository: astral-sh/uv
File: crates/uv/src/commands/project/mod.rs (Rust)

Code before:

impl WorkspacePython {
    /// Determine the [`WorkspacePython`] for the current [`Workspace`].
    pub(crate) async fn from_request(
        python_request: Option<PythonRequest>,
        workspace: Option<&Workspace>,
        groups: &DependencyGroupsWithDefaults,
        project_dir: &Path,
        no_config: bool,
    ) -> Result<Self, ProjectError> {
        // Helper to parse a concrete version from the request (non-range) for compatibility check
        fn pep440_version_from_request(request: &PythonRequest) -> Option<uv_pep440::Version> {
            let version_request = match request {
                PythonRequest::Version(version)
                | PythonRequest::ImplementationVersion(_, version) => version,
                PythonRequest::Key(download_request) => download_request.version()?,
                _ => {
                    return None;
                }
            };
            if matches!(version_request, uv_python::VersionRequest::Range(_, _)) {
                return None;
            }
            // Remove Python variant and parse to PEP 440 version
            let s = version_request.clone().without_python_variant().to_string();
            Some(uv_pep440::Version::from_str(&s).unwrap())
        }

        let requires_python = workspace
            .map(|workspace| find_requires_python(workspace, groups))
            .transpose()?
            .flatten();

        let workspace_root = workspace.map(Workspace::install_path);

        let (source, python_request) = if let Some(request) = python_request {
            // (1) Explicit request from user
            let source = PythonRequestSource::UserRequest;
            let request = Some(request);
            (source, request)
        } else if let Some(file) = PythonVersionFile::discover(
            project_dir,
            &VersionFileDiscoveryOptions::default()
                .with_stop_discovery_at(workspace_root.map(PathBuf::as_ref))
                .with_no_config(no_config),
        )
        .await?
        {
            // (2) Request from `.python-version`
            // If the discovered version file is a GLOBAL pin and it conflicts with the
            // project's `requires-python`, ignore the pin and fall back to the project

Review comment (refactor):
Do we need to roundtrip to a string and parse? Can't we just construct a `Version` from the u8s? I'd probably make this a `VersionRequest::as_version` utility too.

Code after:
    pub(crate) async fn lock(workspace: &Workspace) -> Result<LockedFile, LockedFileError> {
        LockedFile::acquire(
            std::env::temp_dir().join(format!(
                "uv-{}.lock",
                cache_digest(workspace.install_path())
            )),
            LockedFileMode::Exclusive,
            workspace.install_path().simplified_display(),
        )
        .await
    }
}

/// The source of a `Requires-Python` specifier.
#[derive(Debug, Clone)]
pub(crate) enum RequiresPythonSource {
    /// From the PEP 723 inline script metadata.
    Script,
    /// From a `pyproject.toml` in a workspace.
    Project,
}

#[derive(Debug, Clone)]
pub(crate) enum PythonRequestSource {
    /// The request was provided by the user.
    UserRequest,
    /// The request was inferred from a `.python-version` or `.python-versions` file.
    DotPythonVersion(PythonVersionFile),
    /// The request was inferred from a `pyproject.toml` file.
    RequiresPython,
}

impl std::fmt::Display for PythonRequestSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UserRequest => write!(f, "explicit request"),
            Self::DotPythonVersion(file) => {
                write!(f, "version file at `{}`", file.path().user_display())
            }
            Self::RequiresPython => write!(f, "`requires-python` metadata"),
        }
    }
}

/// The resolved Python request and requirement for a [`Workspace`].
#[derive(Debug, Clone)]
pub(crate) struct WorkspacePython {
    /// The source of the Python request.
    pub(crate) source: PythonRequestSource,
    /// The resolved Python request, computed by considering (1) any explicit request from the user
    /// via `--python`, (2) any implicit request from the user via `.python-version`, and (3) any