diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/rules/typing_only_runtime_import.rs b/crates/ruff_linter/src/rules/flake8_type_checking/rules/typing_only_runtime_import.rs index 75a77a1ce9..184da19fbc 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/rules/typing_only_runtime_import.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/rules/typing_only_runtime_import.rs @@ -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::>(), - ); + .collect::>(), + ) + }; let fix = if add_future_import { let import = &NameImport::ImportFrom(MemberNameImport::member( diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__add_future_import__typing-only-first-party-import-typing-only-third-party-import-typing-only-standard-library-import_TC001-3_future.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__add_future_import__typing-only-first-party-import-typing-only-third-party-import-typing-only-standard-library-import_TC001-3_future.py.snap index 87a5323391..9db86ba0f6 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__add_future_import__typing-only-first-party-import-typing-only-third-party-import-typing-only-standard-library-import_TC001-3_future.py.snap +++ b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__add_future_import__typing-only-first-party-import-typing-only-third-party-import-typing-only-standard-library-import_TC001-3_future.py.snap @@ -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): ...