From 0589700ca18e2f6db565858946889ec7b0aba960 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 12 Dec 2025 10:04:05 -0500 Subject: [PATCH] [ty] Prefer unqualified imports over qualified imports It seems like this is perhaps a better default: https://github.com/astral-sh/ty/issues/1274#issuecomment-3352233790 For me personally, I think I'd prefer the qualified variant. But I can easily see this changing based on the specific scenario. I think the thing that pushes me toward prioritizing the unqualified variant is that the user could have typed the qualified variant themselves, but they didn't. So we should perhaps prioritize the form they typed, which is unqualified. --- crates/ty_ide/src/completion.rs | 4 ++-- crates/ty_ide/src/importer.rs | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/ty_ide/src/completion.rs b/crates/ty_ide/src/completion.rs index 376b907b41..0dfd7af8c2 100644 --- a/crates/ty_ide/src/completion.rs +++ b/crates/ty_ide/src/completion.rs @@ -6717,8 +6717,8 @@ TypedDi assert_snapshot!( builder.imports().build().snapshot(), @r" - typing.TypedDict :: - typing.is_typeddict :: + TypedDict :: , TypedDict + is_typeddict :: , is_typeddict _FilterConfigurationTypedDict :: from logging.config import _FilterConfigurationTypedDict _FormatterConfigurationTypedDict :: from logging.config import _FormatterConfigurationTypedDict diff --git a/crates/ty_ide/src/importer.rs b/crates/ty_ide/src/importer.rs index 1dff46bcaf..8a203b5324 100644 --- a/crates/ty_ide/src/importer.rs +++ b/crates/ty_ide/src/importer.rs @@ -745,8 +745,17 @@ impl ImportResponseKind<'_> { fn priority(&self) -> usize { match *self { ImportResponseKind::Unqualified { .. } => 0, - ImportResponseKind::Qualified { .. } => 1, - ImportResponseKind::Partial(_) => 2, + ImportResponseKind::Partial(_) => 1, + // N.B. When given the choice between adding a + // name to an existing `from ... import ...` + // statement and using an existing `import ...` + // in a qualified manner, we currently choose + // the former. Originally we preferred qualification, + // but there is some evidence that this violates + // expectations. + // + // Ref: https://github.com/astral-sh/ty/issues/1274#issuecomment-3352233790 + ImportResponseKind::Qualified { .. } => 2, } } } @@ -1332,9 +1341,9 @@ import collections ); assert_snapshot!( test.import("collections", "defaultdict"), @r" - from collections import OrderedDict + from collections import OrderedDict, defaultdict import collections - collections.defaultdict + defaultdict "); }