Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF048.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib.metadata

__version__ = (0, 1, 0)


Expand All @@ -15,3 +17,5 @@
# Multiple arguments
tuple(map(int, __version__.split(".", 1)))
list(map(int, __version__.split(".", maxsplit=2)))

tuple(map(int, importlib.metadata.version("ruff").split(".")))
37 changes: 20 additions & 17 deletions crates/ruff_linter/src/rules/ruff/rules/map_int_version_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn map_int_version_parsing(checker: &mut Checker, call: &ast::ExprCal
return;
};

if is_dunder_version_split_dot(second) && semantic.match_builtin_expr(first, "int") {
if is_version_split_dot(second, semantic) && semantic.match_builtin_expr(first, "int") {
checker
.diagnostics
.push(Diagnostic::new(MapIntVersionParsing, call.range()));
Expand Down Expand Up @@ -89,8 +89,9 @@ fn map_call_with_two_arguments<'a>(
Some((first, second))
}

/// Whether `expr` has the form `__version__.split(".")` or `something.__version__.split(".")`.
fn is_dunder_version_split_dot(expr: &ast::Expr) -> bool {
/// Whether `expr` has the form `__version__.split(".")`, `something.__version__.split(".")`
/// or `importlib.metadata.version("something").split(".")`.
fn is_version_split_dot(expr: &ast::Expr, semantic: &SemanticModel) -> bool {
let ast::Expr::Call(ast::ExprCall {
func, arguments, ..
}) = expr
Expand All @@ -112,10 +113,10 @@ fn is_dunder_version_split_dot(expr: &ast::Expr) -> bool {
return false;
}

is_dunder_version_split(func)
is_version_split(func, semantic)
}

fn is_dunder_version_split(func: &ast::Expr) -> bool {
fn is_version_split(func: &ast::Expr, semantic: &SemanticModel) -> bool {
// foo.__version__.split(".")
// ---- value ---- ^^^^^ attr
let ast::Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func else {
Expand All @@ -124,19 +125,21 @@ fn is_dunder_version_split(func: &ast::Expr) -> bool {
if attr != "split" {
return false;
}
is_dunder_version(value)
is_version(value, semantic)
}

fn is_dunder_version(expr: &ast::Expr) -> bool {
if let ast::Expr::Name(ast::ExprName { id, .. }) = expr {
return id == "__version__";
fn is_version(expr: &ast::Expr, semantic: &SemanticModel) -> bool {
match expr {
// __version__.split(".")
ast::Expr::Name(ast::ExprName { id, .. }) => id == "__version__",
// foo.__version__.split(".")
ast::Expr::Attribute(ast::ExprAttribute { attr, .. }) => attr == "__version__",
// importlib.metadata.version("foo").split(".")
ast::Expr::Call(ast::ExprCall { func, .. }) => semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
qualified_name.segments() == ["importlib", "metadata", "version"]
}),
_ => false,
}

// foo.__version__.split(".")
// ^^^^^^^^^^^ attr
let ast::Expr::Attribute(ast::ExprAttribute { attr, .. }) = expr else {
return false;
};

attr == "__version__"
}
Loading