mirror of https://github.com/astral-sh/ruff
Avoids unnecessary overhead for `TC004`, when `TC001-003` are disabled (#14657)
This commit is contained in:
parent
3f6c65e78c
commit
d9cbf2fe44
|
|
@ -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
|
// Identify any valid runtime imports. If a module is imported at runtime, and
|
||||||
// used at runtime, then by default, we avoid flagging any other
|
// used at runtime, then by default, we avoid flagging any other
|
||||||
// imports from that model as typing-only.
|
// imports from that model as typing-only.
|
||||||
// FIXME: This does not seem quite right, if only TC004 is enabled
|
let enforce_typing_only_imports = !checker.source_type.is_stub()
|
||||||
// then we don't need to collect the runtime imports
|
|
||||||
let enforce_typing_imports = !checker.source_type.is_stub()
|
|
||||||
&& checker.any_enabled(&[
|
&& checker.any_enabled(&[
|
||||||
Rule::RuntimeImportInTypeCheckingBlock,
|
|
||||||
Rule::TypingOnlyFirstPartyImport,
|
Rule::TypingOnlyFirstPartyImport,
|
||||||
Rule::TypingOnlyStandardLibraryImport,
|
Rule::TypingOnlyStandardLibraryImport,
|
||||||
Rule::TypingOnlyThirdPartyImport,
|
Rule::TypingOnlyThirdPartyImport,
|
||||||
]);
|
]);
|
||||||
let runtime_imports: Vec<Vec<&Binding>> = if enforce_typing_imports {
|
let runtime_imports: Vec<Vec<&Binding>> = if enforce_typing_only_imports {
|
||||||
checker
|
checker
|
||||||
.semantic
|
.semantic
|
||||||
.scopes
|
.scopes
|
||||||
|
|
@ -377,9 +374,16 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Module) {
|
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Module) {
|
||||||
// FIXME: This does not seem quite right, if only TC004 is enabled
|
if !checker.source_type.is_stub()
|
||||||
// then we don't need to collect the runtime imports
|
&& checker.enabled(Rule::RuntimeImportInTypeCheckingBlock)
|
||||||
if enforce_typing_imports {
|
{
|
||||||
|
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
|
let runtime_imports: Vec<&Binding> = checker
|
||||||
.semantic
|
.semantic
|
||||||
.scopes
|
.scopes
|
||||||
|
|
@ -388,26 +392,12 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
|
||||||
.copied()
|
.copied()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if checker.enabled(Rule::RuntimeImportInTypeCheckingBlock) {
|
flake8_type_checking::rules::typing_only_runtime_import(
|
||||||
flake8_type_checking::rules::runtime_import_in_type_checking_block(
|
checker,
|
||||||
checker,
|
scope,
|
||||||
scope,
|
&runtime_imports,
|
||||||
&mut diagnostics,
|
&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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if checker.enabled(Rule::UnusedImport) {
|
if checker.enabled(Rule::UnusedImport) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue