Add tests for python version compatibility (#14430)

This commit is contained in:
Micha Reiser 2024-11-18 13:26:55 +01:00 committed by GitHub
parent d81b6cd334
commit 1f07880d5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 80 additions and 2 deletions

View file

@ -4,8 +4,8 @@
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Default, clap::ValueEnum)]
pub enum TargetVersion {
Py37,
#[default]
Py38,
#[default]
Py39,
Py310,
Py311,
@ -46,3 +46,17 @@ impl From<TargetVersion> for red_knot_python_semantic::PythonVersion {
}
}
}
#[cfg(test)]
mod tests {
use crate::target_version::TargetVersion;
use red_knot_python_semantic::PythonVersion;
#[test]
fn same_default_as_python_version() {
assert_eq!(
PythonVersion::from(TargetVersion::default()),
PythonVersion::default()
);
}
}

View file

@ -184,8 +184,8 @@ impl Settings {
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum TargetVersion {
Py37,
#[default]
Py38,
#[default]
Py39,
Py310,
Py311,
@ -291,3 +291,17 @@ impl System for WasmSystem {
fn not_found() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::NotFound, "No such file or directory")
}
#[cfg(test)]
mod tests {
use crate::TargetVersion;
use red_knot_python_semantic::PythonVersion;
#[test]
fn same_default_as_python_version() {
assert_eq!(
PythonVersion::from(TargetVersion::default()),
PythonVersion::default()
);
}
}

View file

@ -68,6 +68,10 @@ impl PythonVersion {
Self::Py313
}
pub const fn minimal_supported() -> Self {
Self::Py37
}
pub const fn as_tuple(&self) -> (u8, u8) {
match self {
Self::Py37 => (3, 7),

View file

@ -475,4 +475,24 @@ impl PythonVersion {
pub fn supports_pep_701(self) -> bool {
self >= Self::Py312
}
pub fn as_tuple(self) -> (u8, u8) {
match self {
Self::Py37 => (3, 7),
Self::Py38 => (3, 8),
Self::Py39 => (3, 9),
Self::Py310 => (3, 10),
Self::Py311 => (3, 11),
Self::Py312 => (3, 12),
Self::Py313 => (3, 13),
}
}
pub fn latest() -> Self {
Self::Py313
}
pub fn minimal_supported() -> Self {
Self::Py37
}
}

View file

@ -3408,7 +3408,9 @@ pub struct AnalyzeOptions {
mod tests {
use crate::options::Flake8SelfOptions;
use ruff_linter::rules::flake8_self;
use ruff_linter::settings::types::PythonVersion as LinterPythonVersion;
use ruff_python_ast::name::Name;
use ruff_python_formatter::PythonVersion as FormatterPythonVersion;
#[test]
fn flake8_self_options() {
@ -3456,4 +3458,28 @@ mod tests {
vec![Name::new_static("_foo"), Name::new_static("_bar")]
);
}
#[test]
fn formatter_and_linter_target_version_have_same_default() {
assert_eq!(
FormatterPythonVersion::default().as_tuple(),
LinterPythonVersion::default().as_tuple()
);
}
#[test]
fn formatter_and_linter_target_version_have_same_latest() {
assert_eq!(
FormatterPythonVersion::latest().as_tuple(),
LinterPythonVersion::latest().as_tuple()
);
}
#[test]
fn formatter_and_linter_target_version_have_same_minimal_supported() {
assert_eq!(
FormatterPythonVersion::minimal_supported().as_tuple(),
LinterPythonVersion::minimal_supported().as_tuple()
);
}
}