Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-pie] Allow cast(SomeType, ...) (PIE796) #15141

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ class FakeEnum10(enum.Enum):
A = ...
B = ... # PIE796
C = ... # PIE796


from typing import cast

class FakeEnum11(enum.Enum):
A = cast(SomeType, ...)
B = cast(SomeType, ...) # PIE796
C = cast(SomeType, ...) # PIE796
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ class FakeEnum1(enum.Enum):
A = ...
B = ...
C = ...


from typing import cast

class FakeEnum2(enum.Enum):
A = cast(SomeType, ...)
B = cast(SomeType, ...)
C = cast(SomeType, ...)
39 changes: 31 additions & 8 deletions crates/ruff_linter/src/rules/flake8_pie/rules/non_unique_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::{self as ast, Expr, PySourceType, Stmt};
use ruff_python_ast::{self as ast, Expr, ExprCall, Stmt};
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -55,13 +55,14 @@ impl Violation for NonUniqueEnums {

/// PIE796
pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stmt]) {
let semantic = checker.semantic();

let Stmt::ClassDef(parent) = parent else {
return;
};

if !parent.bases().iter().any(|expr| {
checker
.semantic()
semantic
.resolve_qualified_name(expr)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "Enum"]))
}) {
Expand All @@ -84,14 +85,12 @@ pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stm
}
}

let comparable = ComparableExpr::from(value);

if checker.source_type == PySourceType::Stub
&& comparable == ComparableExpr::EllipsisLiteral
{
if checker.source_type.is_stub() && member_has_unknown_value(checker, value) {
continue;
}

let comparable = ComparableExpr::from(value);

if !seen_targets.insert(comparable) {
let diagnostic = Diagnostic::new(
NonUniqueEnums {
Expand All @@ -103,3 +102,27 @@ pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stm
}
}
}

/// Whether the value is a bare ellipsis literal (`A = ...`)
/// or a casted one (`A = cast(SomeType, ...)`).
fn member_has_unknown_value(checker: &Checker, expr: &Expr) -> bool {
match expr {
Expr::EllipsisLiteral(_) => true,

Expr::Call(ExprCall {
func, arguments, ..
}) => {
if !checker.semantic().match_typing_expr(func, "cast") {
return false;
}

if !arguments.keywords.is_empty() {
return false;
}

matches!(arguments.args.as_ref(), [_, Expr::EllipsisLiteral(_)])
}

_ => false,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,20 @@ PIE796.py:72:5: PIE796 Enum contains duplicate value: `...`
72 | C = ... # PIE796
| ^^^^^^^ PIE796
|

PIE796.py:79:5: PIE796 Enum contains duplicate value: `cast(SomeType, ...)`
|
77 | class FakeEnum11(enum.Enum):
78 | A = cast(SomeType, ...)
79 | B = cast(SomeType, ...) # PIE796
| ^^^^^^^^^^^^^^^^^^^^^^^ PIE796
80 | C = cast(SomeType, ...) # PIE796
|

PIE796.py:80:5: PIE796 Enum contains duplicate value: `cast(SomeType, ...)`
|
78 | A = cast(SomeType, ...)
79 | B = cast(SomeType, ...) # PIE796
80 | C = cast(SomeType, ...) # PIE796
| ^^^^^^^^^^^^^^^^^^^^^^^ PIE796
|
Loading