Skip to content

Commit d9cbf2f

Browse files
authored
Avoids unnecessary overhead for TC004, when TC001-003 are disabled (#14657)
1 parent 3f6c65e commit d9cbf2f

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs

+18-28
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,13 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
5252
// Identify any valid runtime imports. If a module is imported at runtime, and
5353
// used at runtime, then by default, we avoid flagging any other
5454
// imports from that model as typing-only.
55-
// FIXME: This does not seem quite right, if only TC004 is enabled
56-
// then we don't need to collect the runtime imports
57-
let enforce_typing_imports = !checker.source_type.is_stub()
55+
let enforce_typing_only_imports = !checker.source_type.is_stub()
5856
&& checker.any_enabled(&[
59-
Rule::RuntimeImportInTypeCheckingBlock,
6057
Rule::TypingOnlyFirstPartyImport,
6158
Rule::TypingOnlyStandardLibraryImport,
6259
Rule::TypingOnlyThirdPartyImport,
6360
]);
64-
let runtime_imports: Vec<Vec<&Binding>> = if enforce_typing_imports {
61+
let runtime_imports: Vec<Vec<&Binding>> = if enforce_typing_only_imports {
6562
checker
6663
.semantic
6764
.scopes
@@ -377,9 +374,16 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
377374
}
378375

379376
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Module) {
380-
// FIXME: This does not seem quite right, if only TC004 is enabled
381-
// then we don't need to collect the runtime imports
382-
if enforce_typing_imports {
377+
if !checker.source_type.is_stub()
378+
&& checker.enabled(Rule::RuntimeImportInTypeCheckingBlock)
379+
{
380+
flake8_type_checking::rules::runtime_import_in_type_checking_block(
381+
checker,
382+
scope,
383+
&mut diagnostics,
384+
);
385+
}
386+
if enforce_typing_only_imports {
383387
let runtime_imports: Vec<&Binding> = checker
384388
.semantic
385389
.scopes
@@ -388,26 +392,12 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
388392
.copied()
389393
.collect();
390394

391-
if checker.enabled(Rule::RuntimeImportInTypeCheckingBlock) {
392-
flake8_type_checking::rules::runtime_import_in_type_checking_block(
393-
checker,
394-
scope,
395-
&mut diagnostics,
396-
);
397-
}
398-
399-
if checker.any_enabled(&[
400-
Rule::TypingOnlyFirstPartyImport,
401-
Rule::TypingOnlyStandardLibraryImport,
402-
Rule::TypingOnlyThirdPartyImport,
403-
]) {
404-
flake8_type_checking::rules::typing_only_runtime_import(
405-
checker,
406-
scope,
407-
&runtime_imports,
408-
&mut diagnostics,
409-
);
410-
}
395+
flake8_type_checking::rules::typing_only_runtime_import(
396+
checker,
397+
scope,
398+
&runtime_imports,
399+
&mut diagnostics,
400+
);
411401
}
412402

413403
if checker.enabled(Rule::UnusedImport) {

0 commit comments

Comments
 (0)