From 887455c498a91ae31d620eeecfa2b65b42e3af07 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 21 Sep 2023 14:24:51 -0400 Subject: [PATCH] Use `u8` to represent version segments (#7578) --- .../pyupgrade/rules/outdated_version_block.rs | 6 +++--- crates/ruff_linter/src/rules/ruff/typing.rs | 17 ++++++----------- crates/ruff_linter/src/settings/types.rs | 6 +++--- crates/ruff_python_stdlib/src/sys.rs | 2 +- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs index 1e8c94abd7..3fdcd57245 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs @@ -165,7 +165,7 @@ fn compare_version(target_version: &[u32], py_version: PythonVersion, or_equal: let (py_major, py_minor) = py_version.as_tuple(); - match if_major.cmp(&py_major) { + match if_major.cmp(&py_major.into()) { Ordering::Less => true, Ordering::Greater => false, Ordering::Equal => { @@ -175,11 +175,11 @@ fn compare_version(target_version: &[u32], py_version: PythonVersion, or_equal: if or_equal { // Ex) `sys.version_info <= 3.8`. If Python 3.8 is the minimum supported version, // the condition won't always evaluate to `false`, so we want to return `false`. - *if_minor < py_minor + *if_minor < py_minor.into() } else { // Ex) `sys.version_info < 3.8`. If Python 3.8 is the minimum supported version, // the condition _will_ always evaluate to `false`, so we want to return `true`. - *if_minor <= py_minor + *if_minor <= py_minor.into() } } } diff --git a/crates/ruff_linter/src/rules/ruff/typing.rs b/crates/ruff_linter/src/rules/ruff/typing.rs index 54d510b8b3..6f8d40f053 100644 --- a/crates/ruff_linter/src/rules/ruff/typing.rs +++ b/crates/ruff_linter/src/rules/ruff/typing.rs @@ -11,7 +11,7 @@ use ruff_source_file::Locator; /// /// A known type is either a builtin type, any object from the standard library, /// or a type from the `typing_extensions` module. -fn is_known_type(call_path: &CallPath, minor_version: u32) -> bool { +fn is_known_type(call_path: &CallPath, minor_version: u8) -> bool { match call_path.as_slice() { ["" | "typing_extensions", ..] => true, [module, ..] => is_known_standard_library(minor_version, module), @@ -72,7 +72,7 @@ impl<'a> TypingTarget<'a> { expr: &'a Expr, semantic: &SemanticModel, locator: &Locator, - minor_version: u32, + minor_version: u8, ) -> Option { match expr { Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { @@ -141,7 +141,7 @@ impl<'a> TypingTarget<'a> { &self, semantic: &SemanticModel, locator: &Locator, - minor_version: u32, + minor_version: u8, ) -> bool { match self { TypingTarget::None @@ -189,12 +189,7 @@ impl<'a> TypingTarget<'a> { } /// Check if the [`TypingTarget`] explicitly allows `Any`. - fn contains_any( - &self, - semantic: &SemanticModel, - locator: &Locator, - minor_version: u32, - ) -> bool { + fn contains_any(&self, semantic: &SemanticModel, locator: &Locator, minor_version: u8) -> bool { match self { TypingTarget::Any => true, // `Literal` cannot contain `Any` as it's a dynamic value. @@ -242,7 +237,7 @@ pub(crate) fn type_hint_explicitly_allows_none<'a>( annotation: &'a Expr, semantic: &SemanticModel, locator: &Locator, - minor_version: u32, + minor_version: u8, ) -> Option<&'a Expr> { match TypingTarget::try_from_expr(annotation, semantic, locator, minor_version) { None | @@ -272,7 +267,7 @@ pub(crate) fn type_hint_resolves_to_any( annotation: &Expr, semantic: &SemanticModel, locator: &Locator, - minor_version: u32, + minor_version: u8, ) -> bool { match TypingTarget::try_from_expr(annotation, semantic, locator, minor_version) { None | diff --git a/crates/ruff_linter/src/settings/types.rs b/crates/ruff_linter/src/settings/types.rs index 2971ca30fc..1f5c073918 100644 --- a/crates/ruff_linter/src/settings/types.rs +++ b/crates/ruff_linter/src/settings/types.rs @@ -58,7 +58,7 @@ impl PythonVersion { Self::Py312 } - pub const fn as_tuple(&self) -> (u32, u32) { + pub const fn as_tuple(&self) -> (u8, u8) { match self { Self::Py37 => (3, 7), Self::Py38 => (3, 8), @@ -69,11 +69,11 @@ impl PythonVersion { } } - pub const fn major(&self) -> u32 { + pub const fn major(&self) -> u8 { self.as_tuple().0 } - pub const fn minor(&self) -> u32 { + pub const fn minor(&self) -> u8 { self.as_tuple().1 } diff --git a/crates/ruff_python_stdlib/src/sys.rs b/crates/ruff_python_stdlib/src/sys.rs index c6d28db289..455c5446ff 100644 --- a/crates/ruff_python_stdlib/src/sys.rs +++ b/crates/ruff_python_stdlib/src/sys.rs @@ -1,6 +1,6 @@ //! This file is generated by `scripts/generate_known_standard_library.py` -pub fn is_known_standard_library(minor_version: u32, module: &str) -> bool { +pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool { matches!( (minor_version, module), (