Skip to content

Commit 22c7823

Browse files
authored
[ty] Enable (but downrank) auto-import completion suggestions from stub-only modules (#27433)
1 parent 05160d5 commit 22c7823

24 files changed

Lines changed: 840 additions & 56 deletions

‎crates/ty_completion_eval/completion-evaluation-tasks.csv‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import-deprioritizes-type_check_only,main.py,1,1
2121
import-deprioritizes-type_check_only,main.py,2,1
2222
import-deprioritizes-type_check_only,main.py,3,2
2323
import-deprioritizes-type_check_only,main.py,4,3
24+
import-deprioritizes-type_check_only,main.pyi,0,1
25+
import-deprioritizes-type_check_only,main.pyi,1,1
2426
import-keyword-completion,main.py,0,1
2527
internal-typeshed-hidden,main.py,0,1
2628
local-over-auto-import,main.py,0,1
@@ -46,3 +48,12 @@ typing-gets-priority,main.py,1,1
4648
typing-gets-priority,main.py,2,1
4749
typing-gets-priority,main.py,3,1
4850
typing-gets-priority,main.py,4,1
51+
typing-only-auto-import-ranking,main.py,0,1
52+
typing-only-auto-import-ranking,main.py,1,1
53+
typing-only-auto-import-ranking,main.py,2,1
54+
typing-only-auto-import-ranking,main.py,3,2
55+
typing-only-auto-import-ranking,main.py,4,1
56+
typing-only-auto-import-ranking,main.py,5,1
57+
typing-only-auto-import-ranking,main.pyi,0,1
58+
typing-only-auto-import-ranking,main.pyi,1,1
59+
typing-only-auto-import-ranking,main.pyi,2,1

‎crates/ty_completion_eval/src/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ fn copy_project(src_dir: &SystemPath, dst_dir: &SystemPath) -> anyhow::Result<Ve
545545

546546
let mut cursors = vec![];
547547
let it = walkdir::WalkDir::new(src_dir.as_std_path())
548+
.sort_by_file_name()
548549
.into_iter()
549550
.filter_entry(|dent| {
550551
!dent
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Typing-only declarations are not penalized when completing a stub.
2+
from module import UniquePrefixA<CURSOR: UniquePrefixApple>
3+
from module import unique_prefix_<CURSOR: unique_prefix_apple>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[settings]
2+
auto-import = true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Runtime symbols outrank alternatives from typing-only modules in Python files.
2+
deprecated<CURSOR: warnings.deprecated>
3+
NoneTy<CURSOR: types.NoneType>
4+
Not<CURSOR: ast.Not>
5+
6+
# Typing-only symbols are included in auto-import suggestions.
7+
static_ass<CURSOR: ty_extensions.static_assert>
8+
is_equiv<CURSOR: ty_extensions._internal.is_equivalent_to>
9+
TypedDictFall<CURSOR: _typeshed._type_checker_internals.TypedDictFallback>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Typing-only symbols retain their usual ranking in stub files.
2+
deprecated<CURSOR: typing_extensions.deprecated>
3+
NoneTy<CURSOR: _typeshed.NoneType>
4+
static_ass<CURSOR: ty_extensions.static_assert>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[project]
2+
name = "test"
3+
version = "0.1.0"
4+
requires-python = ">=3.13"
5+
dependencies = []

‎crates/ty_completion_eval/truth/typing-only-auto-import-ranking/uv.lock‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/ty_ide/src/all_symbols.rs‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use compact_str::CompactString;
22
use rayon::prelude::*;
33
use ruff_db::files::File;
4-
use ty_module_resolver::{
5-
ImportingFile, Module, ModuleName, all_modules, resolve_real_shadowable_module,
6-
};
4+
use ty_module_resolver::{Module, all_modules};
75
use ty_project::{Db, parallel::ParallelIteratorExt};
86
use ty_python_core::ProgramFile;
97

@@ -29,12 +27,8 @@ pub fn all_symbols<'db>(
2927
let all_symbols_span = tracing::debug_span!("all_symbols");
3028
let _span = all_symbols_span.enter();
3129

32-
let typing_extensions = ModuleName::new_static("typing_extensions").unwrap();
3330
let program = importing_from.program(db);
3431
let resolver_environment = importing_from.resolver_environment(db);
35-
let importing_file = ImportingFile::File(importing_from.file(db), resolver_environment);
36-
let is_typing_extensions_available = importing_from.file(db).is_stub(db)
37-
|| resolve_real_shadowable_module(db, importing_file, &typing_extensions).is_some();
3832

3933
let results = all_modules(db, resolver_environment)
4034
.into_par_iter()
@@ -49,15 +43,11 @@ pub fn all_symbols<'db>(
4943
// namespace packages in auto-import anyway.)
5044
let is_non_first_party = module.search_path(db).is_none_or(|sp| !sp.is_first_party());
5145

52-
// Filter out non-first-party modules that are conventionally
53-
// regarded as private or tests.
54-
if is_non_first_party && (name.is_private() || name.is_test_module()) {
55-
return Vec::new();
56-
}
57-
58-
// TODO: also make it available in `TYPE_CHECKING` blocks
59-
// (we'd need https://github.com/astral-sh/ty/issues/1553 to do this well)
60-
if !is_typing_extensions_available && name == &typing_extensions {
46+
// Filter out non-first-party test and private modules, while retaining private
47+
// typeshed packages that are useful when writing type annotations.
48+
if is_non_first_party
49+
&& (name.is_test_module() || name.is_private() && !module.is_type_check_only(db))
50+
{
6151
return Vec::new();
6252
}
6353

‎crates/ty_ide/src/code_action.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,17 @@ mod tests {
664664
2 |
665665
|
666666
667+
info[code-action]: import typing_extensions.reveal_type
668+
--> main.py:2:1
669+
|
670+
2 | reveal_type(1)
671+
| ^^^^^^^^^^^
672+
help: This is a preferred code action
673+
|
674+
1 + from typing_extensions import reveal_type
675+
2 |
676+
|
677+
667678
info[code-action]: Ignore 'undefined-reveal' for this line
668679
--> main.py:2:1
669680
|
@@ -698,6 +709,17 @@ mod tests {
698709
2 |
699710
|
700711
712+
info[code-action]: import typing_extensions.deprecated
713+
--> main.py:2:2
714+
|
715+
2 | @deprecated("do not use")
716+
| ^^^^^^^^^^
717+
help: This is a preferred code action
718+
|
719+
1 + from typing_extensions import deprecated
720+
2 |
721+
|
722+
701723
info[code-action]: Ignore 'unresolved-reference' for this line
702724
--> main.py:2:2
703725
|
@@ -735,6 +757,17 @@ mod tests {
735757
2 |
736758
|
737759
760+
info[code-action]: import typing_extensions.deprecated
761+
--> main.py:4:2
762+
|
763+
4 | @deprecated("do not use")
764+
| ^^^^^^^^^^
765+
help: This is a preferred code action
766+
|
767+
1 + from typing_extensions import deprecated
768+
2 |
769+
|
770+
738771
info[code-action]: qualify warnings.deprecated
739772
--> main.py:4:2
740773
|

0 commit comments

Comments
 (0)