From 68e5c3cc1ecb43a40206be148987f8e36a8026ef Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Thu, 14 Mar 2024 21:48:52 +0000 Subject: [PATCH 1/2] Allow trailing ellipsis in `typing.TYPE_CHECKING` Trailing ellipses in objects defined in `typing.TYPE_CHECKING` might be meaningful (it might be declaring a stub). Thus, we should skip the `unnecessary-placeholder` (`PIE970`) rule in such contexts. --- .../resources/test/fixtures/flake8_pie/PIE790.py | 8 ++++++++ .../src/rules/flake8_pie/rules/unnecessary_placeholder.rs | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE790.py b/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE790.py index 267d8a3c3dd21..f1b3d25301806 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE790.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE790.py @@ -227,3 +227,11 @@ def func(self) -> str: def impl(self) -> str: """Docstring""" return self.func() + + +import typing + +if typing.TYPE_CHECKING: + def contains_meaningful_ellipsis() -> list[int]: + """Allow this in a TYPE_CHECKING block.""" + ... diff --git a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs index 576cb21e35d35..92f7003bfc0ce 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs +++ b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs @@ -83,6 +83,12 @@ pub(crate) fn unnecessary_placeholder(checker: &mut Checker, body: &[Stmt]) { return; } + // In a type-checking block, a trailing ellipsis might be meaningful. A + // user might be using the type-checking context to declare a stub. + if checker.semantic().in_type_checking_block() { + return; + } + for stmt in body { let kind = match stmt { Stmt::Pass(_) => Placeholder::Pass, From 7a4f03678f14340bf10ec8729b17cc3f2a285478 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 14 Mar 2024 23:48:54 -0400 Subject: [PATCH 2/2] Move down --- .../flake8_pie/rules/unnecessary_placeholder.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs index 92f7003bfc0ce..09a58552d6357 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs +++ b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_placeholder.rs @@ -83,16 +83,16 @@ pub(crate) fn unnecessary_placeholder(checker: &mut Checker, body: &[Stmt]) { return; } - // In a type-checking block, a trailing ellipsis might be meaningful. A - // user might be using the type-checking context to declare a stub. - if checker.semantic().in_type_checking_block() { - return; - } - for stmt in body { let kind = match stmt { Stmt::Pass(_) => Placeholder::Pass, Stmt::Expr(expr) if expr.value.is_ellipsis_literal_expr() => { + // In a type-checking block, a trailing ellipsis might be meaningful. A + // user might be using the type-checking context to declare a stub. + if checker.semantic().in_type_checking_block() { + return; + } + // Ellipses are significant in protocol methods and abstract methods. Specifically, // Pyright uses the presence of an ellipsis to indicate that a method is a stub, // rather than a default implementation.