mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-25 22:29:02 +00:00
Use u8 to represent version segments (#7578)
This commit is contained in:
parent
4d6f5ff0a7
commit
887455c498
4 changed files with 13 additions and 18 deletions
|
|
@ -165,7 +165,7 @@ fn compare_version(target_version: &[u32], py_version: PythonVersion, or_equal:
|
||||||
|
|
||||||
let (py_major, py_minor) = py_version.as_tuple();
|
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::Less => true,
|
||||||
Ordering::Greater => false,
|
Ordering::Greater => false,
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
|
|
@ -175,11 +175,11 @@ fn compare_version(target_version: &[u32], py_version: PythonVersion, or_equal:
|
||||||
if or_equal {
|
if or_equal {
|
||||||
// Ex) `sys.version_info <= 3.8`. If Python 3.8 is the minimum supported version,
|
// 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`.
|
// the condition won't always evaluate to `false`, so we want to return `false`.
|
||||||
*if_minor < py_minor
|
*if_minor < py_minor.into()
|
||||||
} else {
|
} else {
|
||||||
// Ex) `sys.version_info < 3.8`. If Python 3.8 is the minimum supported version,
|
// 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`.
|
// the condition _will_ always evaluate to `false`, so we want to return `true`.
|
||||||
*if_minor <= py_minor
|
*if_minor <= py_minor.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use ruff_source_file::Locator;
|
||||||
///
|
///
|
||||||
/// A known type is either a builtin type, any object from the standard library,
|
/// A known type is either a builtin type, any object from the standard library,
|
||||||
/// or a type from the `typing_extensions` module.
|
/// 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() {
|
match call_path.as_slice() {
|
||||||
["" | "typing_extensions", ..] => true,
|
["" | "typing_extensions", ..] => true,
|
||||||
[module, ..] => is_known_standard_library(minor_version, module),
|
[module, ..] => is_known_standard_library(minor_version, module),
|
||||||
|
|
@ -72,7 +72,7 @@ impl<'a> TypingTarget<'a> {
|
||||||
expr: &'a Expr,
|
expr: &'a Expr,
|
||||||
semantic: &SemanticModel,
|
semantic: &SemanticModel,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
minor_version: u32,
|
minor_version: u8,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||||
|
|
@ -141,7 +141,7 @@ impl<'a> TypingTarget<'a> {
|
||||||
&self,
|
&self,
|
||||||
semantic: &SemanticModel,
|
semantic: &SemanticModel,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
minor_version: u32,
|
minor_version: u8,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match self {
|
match self {
|
||||||
TypingTarget::None
|
TypingTarget::None
|
||||||
|
|
@ -189,12 +189,7 @@ impl<'a> TypingTarget<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the [`TypingTarget`] explicitly allows `Any`.
|
/// Check if the [`TypingTarget`] explicitly allows `Any`.
|
||||||
fn contains_any(
|
fn contains_any(&self, semantic: &SemanticModel, locator: &Locator, minor_version: u8) -> bool {
|
||||||
&self,
|
|
||||||
semantic: &SemanticModel,
|
|
||||||
locator: &Locator,
|
|
||||||
minor_version: u32,
|
|
||||||
) -> bool {
|
|
||||||
match self {
|
match self {
|
||||||
TypingTarget::Any => true,
|
TypingTarget::Any => true,
|
||||||
// `Literal` cannot contain `Any` as it's a dynamic value.
|
// `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,
|
annotation: &'a Expr,
|
||||||
semantic: &SemanticModel,
|
semantic: &SemanticModel,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
minor_version: u32,
|
minor_version: u8,
|
||||||
) -> Option<&'a Expr> {
|
) -> Option<&'a Expr> {
|
||||||
match TypingTarget::try_from_expr(annotation, semantic, locator, minor_version) {
|
match TypingTarget::try_from_expr(annotation, semantic, locator, minor_version) {
|
||||||
None |
|
None |
|
||||||
|
|
@ -272,7 +267,7 @@ pub(crate) fn type_hint_resolves_to_any(
|
||||||
annotation: &Expr,
|
annotation: &Expr,
|
||||||
semantic: &SemanticModel,
|
semantic: &SemanticModel,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
minor_version: u32,
|
minor_version: u8,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match TypingTarget::try_from_expr(annotation, semantic, locator, minor_version) {
|
match TypingTarget::try_from_expr(annotation, semantic, locator, minor_version) {
|
||||||
None |
|
None |
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ impl PythonVersion {
|
||||||
Self::Py312
|
Self::Py312
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn as_tuple(&self) -> (u32, u32) {
|
pub const fn as_tuple(&self) -> (u8, u8) {
|
||||||
match self {
|
match self {
|
||||||
Self::Py37 => (3, 7),
|
Self::Py37 => (3, 7),
|
||||||
Self::Py38 => (3, 8),
|
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
|
self.as_tuple().0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn minor(&self) -> u32 {
|
pub const fn minor(&self) -> u8 {
|
||||||
self.as_tuple().1
|
self.as_tuple().1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//! This file is generated by `scripts/generate_known_standard_library.py`
|
//! 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!(
|
matches!(
|
||||||
(minor_version, module),
|
(minor_version, module),
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue