avoid quotes when __future__ import is present

This commit is contained in:
Brent Westbrook 2025-07-02 15:09:18 -04:00
parent dbf8e2b5bf
commit e5fab3cdef
2 changed files with 27 additions and 31 deletions

View file

@ -559,28 +559,33 @@ fn fix_imports(
)?
.into_edits();
// Step 3) Quote any runtime usages of the referenced symbol.
let quote_reference_edits = filter_contained(
imports
.iter()
.flat_map(|ImportBinding { binding, .. }| {
binding.references.iter().filter_map(|reference_id| {
let reference = checker.semantic().reference(*reference_id);
if reference.in_runtime_context() {
Some(quote_annotation(
reference.expression_id()?,
checker.semantic(),
checker.stylist(),
checker.locator(),
checker.default_string_flags(),
))
} else {
None
}
// Step 3) Quote any runtime usages of the referenced symbol, if we're not adding a `__future__`
// import instead.
let quote_reference_edits = if add_future_import {
Vec::new()
} else {
filter_contained(
imports
.iter()
.flat_map(|ImportBinding { binding, .. }| {
binding.references.iter().filter_map(|reference_id| {
let reference = checker.semantic().reference(*reference_id);
if reference.in_runtime_context() {
Some(quote_annotation(
reference.expression_id()?,
checker.semantic(),
checker.stylist(),
checker.locator(),
checker.default_string_flags(),
))
} else {
None
}
})
})
})
.collect::<Vec<_>>(),
);
.collect::<Vec<_>>(),
)
};
let fix = if add_future_import {
let import = &NameImport::ImportFrom(MemberNameImport::member(

View file

@ -24,9 +24,6 @@ TC001-3_future.py:1:25: TC003 [*] Move standard library import `collections.Coun
6 10 |
7 11 |
8 12 | def f(x: first_party.foo): ...
9 13 | def g(x: third_party.bar): ...
10 |-def h(x: Counter): ...
14 |+def h(x: "Counter"): ...
TC001-3_future.py:3:23: TC002 [*] Move third-party import `elsewhere.third_party` into a type-checking block
|
@ -53,9 +50,6 @@ TC001-3_future.py:3:23: TC002 [*] Move third-party import `elsewhere.third_party
6 10 |
7 11 |
8 12 | def f(x: first_party.foo): ...
9 |-def g(x: third_party.bar): ...
13 |+def g(x: "third_party.bar"): ...
10 14 | def h(x: Counter): ...
TC001-3_future.py:5:15: TC001 [*] Move application import `.first_party` into a type-checking block
|
@ -79,7 +73,4 @@ TC001-3_future.py:5:15: TC001 [*] Move application import `.first_party` into a
9 |+ from . import first_party
6 10 |
7 11 |
8 |-def f(x: first_party.foo): ...
12 |+def f(x: "first_party.foo"): ...
9 13 | def g(x: third_party.bar): ...
10 14 | def h(x: Counter): ...
8 12 | def f(x: first_party.foo): ...