Use u8 to represent version segments (#7578)

This commit is contained in:
Charlie Marsh 2023-09-21 14:24:51 -04:00 committed by GitHub
parent 4d6f5ff0a7
commit 887455c498
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 18 deletions

View file

@ -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()
}
}
}

View file

@ -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<Self> {
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 |

View file

@ -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
}

View file

@ -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),
(