mirror of https://github.com/astral-sh/ruff
[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.
This commit is contained in:
parent
1e2c81df80
commit
1b5389da57
|
|
@ -6625,8 +6625,8 @@ TypedDi<CURSOR>
|
|||
assert_snapshot!(
|
||||
builder.imports().build().snapshot(),
|
||||
@r"
|
||||
typing.TypedDict :: <no import edit>
|
||||
typing.is_typeddict :: <no import edit>
|
||||
TypedDict :: , TypedDict
|
||||
is_typeddict :: , is_typeddict
|
||||
_FilterConfigurationTypedDict :: from logging.config import _FilterConfigurationTypedDict
|
||||
|
||||
_FormatterConfigurationTypedDict :: from logging.config import _FormatterConfigurationTypedDict
|
||||
|
|
|
|||
|
|
@ -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
|
||||
");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue