Skip to content

Commit

Permalink
[PIE796] don't report when using ellipses for enum values in stub fil…
Browse files Browse the repository at this point in the history
…es (#8825)

## Summary

Just ignores ellipses as enum values inside stub files.

Fixes #8818.
  • Loading branch information
sciyoshi authored Nov 24, 2023
1 parent 8365d2e commit 852a8f4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ class FakeEnum10(enum.Enum):
A = enum.auto()
B = enum.auto()
C = enum.auto()


class FakeEnum10(enum.Enum):
A = ...
B = ... # PIE796
C = ... # PIE796
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import enum


class FakeEnum1(enum.Enum):
A = ...
B = ...
C = ...
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_pie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod tests {
#[test_case(Rule::UnnecessarySpread, Path::new("PIE800.py"))]
#[test_case(Rule::ReimplementedContainerBuiltin, Path::new("PIE807.py"))]
#[test_case(Rule::NonUniqueEnums, Path::new("PIE796.py"))]
#[test_case(Rule::NonUniqueEnums, Path::new("PIE796.pyi"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Expand Down
12 changes: 10 additions & 2 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, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::{self as ast, Expr, Stmt};
use ruff_python_ast::{self as ast, Expr, PySourceType, Stmt};
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -84,7 +84,15 @@ pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stm
}
}

if !seen_targets.insert(ComparableExpr::from(value)) {
let comparable = ComparableExpr::from(value);

if checker.source_type == PySourceType::Stub
&& comparable == ComparableExpr::EllipsisLiteral
{
continue;
}

if !seen_targets.insert(comparable) {
let diagnostic = Diagnostic::new(
NonUniqueEnums {
value: checker.generator().expr(value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@ PIE796.py:54:5: PIE796 Enum contains duplicate value: `2`
| ^^^^^ PIE796
|

PIE796.py:71:5: PIE796 Enum contains duplicate value: `...`
|
69 | class FakeEnum10(enum.Enum):
70 | A = ...
71 | B = ... # PIE796
| ^^^^^^^ PIE796
72 | C = ... # PIE796
|

PIE796.py:72:5: PIE796 Enum contains duplicate value: `...`
|
70 | A = ...
71 | B = ... # PIE796
72 | C = ... # PIE796
| ^^^^^^^ PIE796
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/flake8_pie/mod.rs
---

4 changes: 2 additions & 2 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ pub enum ComparableExpr<'a> {
NumberLiteral(ExprNumberLiteral<'a>),
BoolLiteral(ExprBoolLiteral<'a>),
NoneLiteral,
EllispsisLiteral,
EllipsisLiteral,
Attribute(ExprAttribute<'a>),
Subscript(ExprSubscript<'a>),
Starred(ExprStarred<'a>),
Expand Down Expand Up @@ -964,7 +964,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
Self::BoolLiteral(ExprBoolLiteral { value })
}
ast::Expr::NoneLiteral(_) => Self::NoneLiteral,
ast::Expr::EllipsisLiteral(_) => Self::EllispsisLiteral,
ast::Expr::EllipsisLiteral(_) => Self::EllipsisLiteral,
ast::Expr::Attribute(ast::ExprAttribute {
value,
attr,
Expand Down

0 comments on commit 852a8f4

Please sign in to comment.