Skip to main content

uv_python_discovery/
selection_error.rs

1//! Python request and compatibility failures.
2
3use uv_distribution_types::RequiresPython;
4use uv_pep440::Version;
5use uv_workspace::{RequiresPythonSources, dependency_groups::DependencyGroupError};
6
7use crate::{PythonRequirementConflicts, format_requires_python_sources};
8
9/// A failure while discovering or validating a project or script interpreter.
10#[derive(Debug, thiserror::Error)]
11pub enum PythonSelectionError {
12    #[error(
13        "The requested interpreter resolved to Python {_0}, which is incompatible with the project's Python requirement: `{_1}`{_2}"
14    )]
15    RequestedPythonProjectIncompatibility(Version, RequiresPython, Box<PythonRequirementConflicts>),
16
17    #[error(
18        "The Python request from `{python_request}` resolved to Python {version}, which is incompatible with the project's Python requirement: `{requires_python}`{requires_python_sources}\nUse `uv python pin` to update the `.python-version` file to a compatible version"
19    )]
20    DotPythonVersionProjectIncompatibility {
21        python_request: String,
22        version: Version,
23        requires_python: RequiresPython,
24        requires_python_sources: Box<PythonRequirementConflicts>,
25    },
26
27    #[error(
28        "The resolved Python interpreter (Python {_0}) is incompatible with the project's Python requirement: `{_1}`{_2}"
29    )]
30    RequiresPythonProjectIncompatibility(Version, RequiresPython, Box<PythonRequirementConflicts>),
31
32    #[error(
33        "The requested interpreter resolved to Python {0}, which is incompatible with the script's Python requirement: `{1}`"
34    )]
35    RequestedPythonScriptIncompatibility(Version, RequiresPython),
36
37    #[error(
38        "The Python request from `{0}` resolved to Python {1}, which is incompatible with the script's Python requirement: `{2}`"
39    )]
40    DotPythonVersionScriptIncompatibility(String, Version, RequiresPython),
41
42    #[error(
43        "The resolved Python interpreter (Python {0}) is incompatible with the script's Python requirement: `{1}`"
44    )]
45    RequiresPythonScriptIncompatibility(Version, RequiresPython),
46
47    #[error(
48        "Found conflicting Python requirements:\n{}",
49        format_requires_python_sources(_0)
50    )]
51    DisjointRequiresPython(RequiresPythonSources),
52
53    #[error(transparent)]
54    Python(#[from] crate::Error),
55
56    #[error(transparent)]
57    DependencyGroup(#[from] DependencyGroupError),
58
59    #[error(transparent)]
60    Io(#[from] std::io::Error),
61}
62
63impl uv_errors::Hinted for PythonSelectionError {
64    fn hints(&self) -> uv_errors::Hints<'_> {
65        match self {
66            Self::Python(error) => error.hints(),
67            Self::RequestedPythonProjectIncompatibility(..)
68            | Self::DotPythonVersionProjectIncompatibility { .. }
69            | Self::RequiresPythonProjectIncompatibility(..)
70            | Self::RequestedPythonScriptIncompatibility(..)
71            | Self::DotPythonVersionScriptIncompatibility(..)
72            | Self::RequiresPythonScriptIncompatibility(..)
73            | Self::DisjointRequiresPython(..)
74            | Self::DependencyGroup(..)
75            | Self::Io(..) => uv_errors::Hints::none(),
76        }
77    }
78}