Skip to content

Commit

Permalink
Avoids unnecessary overhead for TC004, when TC001-003 are disabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
Daverball authored Nov 28, 2024
1 parent 3f6c65e commit d9cbf2f
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,13 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
// Identify any valid runtime imports. If a module is imported at runtime, and
// used at runtime, then by default, we avoid flagging any other
// imports from that model as typing-only.
// FIXME: This does not seem quite right, if only TC004 is enabled
// then we don't need to collect the runtime imports
let enforce_typing_imports = !checker.source_type.is_stub()
let enforce_typing_only_imports = !checker.source_type.is_stub()
&& checker.any_enabled(&[
Rule::RuntimeImportInTypeCheckingBlock,
Rule::TypingOnlyFirstPartyImport,
Rule::TypingOnlyStandardLibraryImport,
Rule::TypingOnlyThirdPartyImport,
]);
let runtime_imports: Vec<Vec<&Binding>> = if enforce_typing_imports {
let runtime_imports: Vec<Vec<&Binding>> = if enforce_typing_only_imports {
checker
.semantic
.scopes
Expand Down Expand Up @@ -377,9 +374,16 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
}

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

if checker.enabled(Rule::RuntimeImportInTypeCheckingBlock) {
flake8_type_checking::rules::runtime_import_in_type_checking_block(
checker,
scope,
&mut diagnostics,
);
}

if checker.any_enabled(&[
Rule::TypingOnlyFirstPartyImport,
Rule::TypingOnlyStandardLibraryImport,
Rule::TypingOnlyThirdPartyImport,
]) {
flake8_type_checking::rules::typing_only_runtime_import(
checker,
scope,
&runtime_imports,
&mut diagnostics,
);
}
flake8_type_checking::rules::typing_only_runtime_import(
checker,
scope,
&runtime_imports,
&mut diagnostics,
);
}

if checker.enabled(Rule::UnusedImport) {
Expand Down

0 comments on commit d9cbf2f

Please sign in to comment.