Skip to content

Commit 3f7a54d

Browse files
authored
[flake8-tidy-imports] Prevent fix loop between TID254 and TID255 (#28262)
Summary -- I initially wanted to avoid emitting `TID254` when an import that was required to be lazy was immediately resolved. However, it became clear that that approach was much more complicated while reviewing #26027. This PR implements the easier fix of skipping `TID255` for imports that are required to be lazy by the user's configuration, even if those imports are immediately resolved. I think this is also a sensible route. A close alternative would be still emitting the `TID255` diagnostic and only suppressing the fix. I don't really feel strongly one way or the other. The diagnostic is possibly still helpful to let you know that you're defeating your desired lazy import, but especially in an example like the one from the issue, it would be pretty hard to avoid. This would just require moving the new check down a few lines to implement, though. Fixes #25418 Test Plan -- New mdtest based on the issue
1 parent 35656cf commit 3f7a54d

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# `lazy-import-immediately-resolved` (`TID255`)
2+
3+
```toml
4+
target-version = "py315"
5+
6+
[lint]
7+
preview = true
8+
select = ["TID254", "TID255"]
9+
flake8-tidy-imports.require-lazy = "all"
10+
```
11+
12+
## Required lazy imports
13+
14+
`TID255` ignores imports that are required to be lazy to avoid a conflict with `TID254`, even if the
15+
import is resolved immediately.
16+
17+
```py
18+
import foo # snapshot: lazy-import-mismatch
19+
20+
class Bar(foo.Base): ...
21+
```
22+
23+
```snapshot
24+
error[TID254]: Use a `lazy` import instead of an eager import
25+
--> src/mdtest_snippet.py:1:8
26+
|
27+
1 | import foo # snapshot: lazy-import-mismatch
28+
| ^^^
29+
help: Convert to a lazy import
30+
|
31+
- import foo # snapshot: lazy-import-mismatch
32+
1 + lazy import foo # snapshot: lazy-import-mismatch
33+
2 |
34+
|
35+
note: This is an unsafe fix and may change runtime behavior
36+
```
37+
38+
## Partially required lazy imports
39+
40+
`TID255` still reports immediately resolved names outside `require-lazy`, even when another name in
41+
the same import is required to be lazy.
42+
43+
```toml
44+
target-version = "py315"
45+
46+
[lint]
47+
preview = true
48+
select = ["TID254", "TID255"]
49+
flake8-tidy-imports.require-lazy = ["foo", "pkg.Base"]
50+
```
51+
52+
```py
53+
lazy import foo as required, bar
54+
lazy from pkg import Base as RequiredBase, OtherBase
55+
56+
required.value
57+
RequiredBase()
58+
bar.value # error: [lazy-import-immediately-resolved]
59+
OtherBase() # error: [lazy-import-immediately-resolved]
60+
```

‎crates/ruff_linter/src/rules/flake8_tidy_imports/rules/lazy_import_immediately_resolved.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ruff_text_size::{Ranged, TextRange};
88

99
use crate::checkers::ast::Checker;
1010
use crate::codes::Category;
11+
use crate::rules::flake8_tidy_imports::rules::BannedModuleImportPolicies;
1112
use crate::{Edit, Fix, FixAvailability, Violation};
1213

1314
/// ## What it does
@@ -46,6 +47,12 @@ use crate::{Edit, Fix, FixAvailability, Violation};
4647
/// The fix is only available when the lazy import statement imports a single
4748
/// member, since removing `lazy` from a multi-member import would make every
4849
/// imported member eager, including names that may not be resolved immediately.
50+
///
51+
/// ## Options
52+
///
53+
/// The rule ignores imports required to be lazy by the following setting:
54+
///
55+
/// - [`lint.flake8-tidy-imports.require-lazy`]
4956
#[derive(ViolationMetadata)]
5057
#[violation_metadata(preview_since = "0.15.13", category = Category::Correctness)]
5158
pub(crate) struct LazyImportImmediatelyResolved {
@@ -90,6 +97,19 @@ pub(crate) fn lazy_import_immediately_resolved(checker: &Checker, name: &ExprNam
9097
return;
9198
};
9299

100+
// Ignore imports that are required to be lazy.
101+
let require_lazy = &checker.settings().flake8_tidy_imports.require_lazy;
102+
for (policy, node) in &BannedModuleImportPolicies::new(import, checker) {
103+
// An `all` selector matches a `from` import's module, not its individual members.
104+
if require_lazy.includes_all() && import.is_import_from_stmt() && node.is_alias() {
105+
break;
106+
}
107+
108+
if node.range().contains_range(binding.range()) && require_lazy.find(&policy).is_some() {
109+
return;
110+
}
111+
}
112+
93113
let fix_range = if is_single_member_import(import) {
94114
lazy_import_prefix_range(import, checker.source_tokens())
95115
} else {

0 commit comments

Comments
 (0)