Remove red_knot_python_semantic::python_version::TargetVersion (#12790)

This commit is contained in:
Alex Waygood 2024-08-10 14:28:31 +01:00 committed by GitHub
parent 597c5f9124
commit cf1a57df5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 148 additions and 217 deletions

View file

@ -1,58 +1,9 @@
use std::fmt;
/// Enumeration of all supported Python versions
/// Representation of a Python version.
///
/// TODO: unify with the `PythonVersion` enum in the linter/formatter crates?
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum TargetVersion {
Py37,
#[default]
Py38,
Py39,
Py310,
Py311,
Py312,
Py313,
}
impl TargetVersion {
pub fn major_version(self) -> u8 {
PythonVersion::from(self).major
}
pub fn minor_version(self) -> u8 {
PythonVersion::from(self).minor
}
const fn as_display_str(self) -> &'static str {
match self {
Self::Py37 => "py37",
Self::Py38 => "py38",
Self::Py39 => "py39",
Self::Py310 => "py310",
Self::Py311 => "py311",
Self::Py312 => "py312",
Self::Py313 => "py313",
}
}
}
impl fmt::Display for TargetVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_display_str())
}
}
impl fmt::Debug for TargetVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
/// Generic representation for a Python version.
///
/// Unlike [`TargetVersion`], this does not necessarily represent
/// a Python version that we actually support.
/// Unlike the `TargetVersion` enums in the CLI crates,
/// this does not necessarily represent a Python version that we actually support.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PythonVersion {
pub major: u8,
@ -60,11 +11,34 @@ pub struct PythonVersion {
}
impl PythonVersion {
pub const PY37: PythonVersion = PythonVersion { major: 3, minor: 7 };
pub const PY38: PythonVersion = PythonVersion { major: 3, minor: 8 };
pub const PY39: PythonVersion = PythonVersion { major: 3, minor: 9 };
pub const PY310: PythonVersion = PythonVersion {
major: 3,
minor: 10,
};
pub const PY311: PythonVersion = PythonVersion {
major: 3,
minor: 11,
};
pub const PY312: PythonVersion = PythonVersion {
major: 3,
minor: 12,
};
pub const PY313: PythonVersion = PythonVersion {
major: 3,
minor: 13,
};
pub fn free_threaded_build_available(self) -> bool {
self >= PythonVersion {
major: 3,
minor: 13,
}
self >= PythonVersion::PY313
}
}
impl Default for PythonVersion {
fn default() -> Self {
Self::PY38
}
}
@ -86,60 +60,3 @@ impl fmt::Display for PythonVersion {
write!(f, "{major}.{minor}")
}
}
impl From<TargetVersion> for PythonVersion {
fn from(value: TargetVersion) -> Self {
match value {
TargetVersion::Py37 => PythonVersion { major: 3, minor: 7 },
TargetVersion::Py38 => PythonVersion { major: 3, minor: 8 },
TargetVersion::Py39 => PythonVersion { major: 3, minor: 9 },
TargetVersion::Py310 => PythonVersion {
major: 3,
minor: 10,
},
TargetVersion::Py311 => PythonVersion {
major: 3,
minor: 11,
},
TargetVersion::Py312 => PythonVersion {
major: 3,
minor: 12,
},
TargetVersion::Py313 => PythonVersion {
major: 3,
minor: 13,
},
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct UnsupportedPythonVersion(PythonVersion);
impl fmt::Display for UnsupportedPythonVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Python version {} is unsupported", self.0)
}
}
impl std::error::Error for UnsupportedPythonVersion {}
impl TryFrom<PythonVersion> for TargetVersion {
type Error = UnsupportedPythonVersion;
fn try_from(value: PythonVersion) -> Result<Self, Self::Error> {
let PythonVersion { major: 3, minor } = value else {
return Err(UnsupportedPythonVersion(value));
};
match minor {
7 => Ok(TargetVersion::Py37),
8 => Ok(TargetVersion::Py38),
9 => Ok(TargetVersion::Py39),
10 => Ok(TargetVersion::Py310),
11 => Ok(TargetVersion::Py311),
12 => Ok(TargetVersion::Py312),
13 => Ok(TargetVersion::Py313),
_ => Err(UnsupportedPythonVersion(value)),
}
}
}