[ty] Limit shown import paths to at most 5 unless ty runs with -v (#20912)

This commit is contained in:
Micha Reiser 2025-10-16 13:18:09 +02:00 committed by GitHub
parent c8133104e8
commit 9393279f65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 155 additions and 12 deletions

View file

@ -98,6 +98,10 @@ impl Db for ModuleDb {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
default_lint_registry() default_lint_registry()
} }
fn verbose(&self) -> bool {
false
}
} }
#[salsa::db] #[salsa::db]

View file

@ -14,7 +14,7 @@ use anyhow::Result;
use std::sync::Mutex; use std::sync::Mutex;
use crate::args::{CheckCommand, Command, TerminalColor}; use crate::args::{CheckCommand, Command, TerminalColor};
use crate::logging::setup_tracing; use crate::logging::{VerbosityLevel, setup_tracing};
use crate::printer::Printer; use crate::printer::Printer;
use anyhow::{Context, anyhow}; use anyhow::{Context, anyhow};
use clap::{CommandFactory, Parser}; use clap::{CommandFactory, Parser};
@ -128,6 +128,8 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
let mut db = ProjectDatabase::new(project_metadata, system)?; let mut db = ProjectDatabase::new(project_metadata, system)?;
db.project()
.set_verbose(&mut db, verbosity >= VerbosityLevel::Verbose);
if !check_paths.is_empty() { if !check_paths.is_empty() {
db.project().set_included_paths(&mut db, check_paths); db.project().set_included_paths(&mut db, check_paths);
} }

View file

@ -782,6 +782,8 @@ impl CliTest {
let mut settings = insta::Settings::clone_current(); let mut settings = insta::Settings::clone_current();
settings.add_filter(&tempdir_filter(&project_dir), "<temp_dir>/"); settings.add_filter(&tempdir_filter(&project_dir), "<temp_dir>/");
settings.add_filter(r#"\\(\w\w|\s|\.|")"#, "/$1"); settings.add_filter(r#"\\(\w\w|\s|\.|")"#, "/$1");
// 0.003s
settings.add_filter(r"\d.\d\d\ds", "0.000s");
settings.add_filter( settings.add_filter(
r#"The system cannot find the file specified."#, r#"The system cannot find the file specified."#,
"No such file or directory", "No such file or directory",

View file

@ -421,6 +421,94 @@ fn lib64_site_packages_directory_on_unix() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
#[test]
fn many_search_paths() -> anyhow::Result<()> {
let case = CliTest::with_files([
("extra1/foo1.py", ""),
("extra2/foo2.py", ""),
("extra3/foo3.py", ""),
("extra4/foo4.py", ""),
("extra5/foo5.py", ""),
("extra6/foo6.py", ""),
("test.py", "import foo1, baz"),
])?;
assert_cmd_snapshot!(
case.command()
.arg("--python-platform").arg("linux")
.arg("--extra-search-path").arg("extra1")
.arg("--extra-search-path").arg("extra2")
.arg("--extra-search-path").arg("extra3")
.arg("--extra-search-path").arg("extra4")
.arg("--extra-search-path").arg("extra5")
.arg("--extra-search-path").arg("extra6"),
@r"
success: false
exit_code: 1
----- stdout -----
error[unresolved-import]: Cannot resolve imported module `baz`
--> test.py:1:14
|
1 | import foo1, baz
| ^^^
|
info: Searched in the following paths during module resolution:
info: 1. <temp_dir>/extra1 (extra search path specified on the CLI or in your config file)
info: 2. <temp_dir>/extra2 (extra search path specified on the CLI or in your config file)
info: 3. <temp_dir>/extra3 (extra search path specified on the CLI or in your config file)
info: 4. <temp_dir>/extra4 (extra search path specified on the CLI or in your config file)
info: 5. <temp_dir>/extra5 (extra search path specified on the CLI or in your config file)
info: ... and 3 more paths. Run with `-v` to see all paths.
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
info: rule `unresolved-import` is enabled by default
Found 1 diagnostic
----- stderr -----
");
// Shows all with `-v`
assert_cmd_snapshot!(
case.command()
.arg("--python-platform").arg("linux")
.arg("--extra-search-path").arg("extra1")
.arg("--extra-search-path").arg("extra2")
.arg("--extra-search-path").arg("extra3")
.arg("--extra-search-path").arg("extra4")
.arg("--extra-search-path").arg("extra5")
.arg("--extra-search-path").arg("extra6")
.arg("-v"),
@r"
success: false
exit_code: 1
----- stdout -----
error[unresolved-import]: Cannot resolve imported module `baz`
--> test.py:1:14
|
1 | import foo1, baz
| ^^^
|
info: Searched in the following paths during module resolution:
info: 1. <temp_dir>/extra1 (extra search path specified on the CLI or in your config file)
info: 2. <temp_dir>/extra2 (extra search path specified on the CLI or in your config file)
info: 3. <temp_dir>/extra3 (extra search path specified on the CLI or in your config file)
info: 4. <temp_dir>/extra4 (extra search path specified on the CLI or in your config file)
info: 5. <temp_dir>/extra5 (extra search path specified on the CLI or in your config file)
info: 6. <temp_dir>/extra6 (extra search path specified on the CLI or in your config file)
info: 7. <temp_dir>/ (first-party code)
info: 8. vendored://stdlib (stdlib typeshed stubs vendored by ty)
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
info: rule `unresolved-import` is enabled by default
Found 1 diagnostic
----- stderr -----
INFO Python version: Python 3.14, platform: linux
INFO Indexed 7 file(s) in 0.000s
");
Ok(())
}
#[test] #[test]
fn pyvenv_cfg_file_annotation_showing_where_python_version_set() -> anyhow::Result<()> { fn pyvenv_cfg_file_annotation_showing_where_python_version_set() -> anyhow::Result<()> {
let case = CliTest::with_files([ let case = CliTest::with_files([

View file

@ -457,6 +457,10 @@ impl SemanticDb for ProjectDatabase {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
ty_python_semantic::default_lint_registry() ty_python_semantic::default_lint_registry()
} }
fn verbose(&self) -> bool {
self.project().verbose(self)
}
} }
#[salsa::db] #[salsa::db]
@ -609,6 +613,10 @@ pub(crate) mod tests {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
ty_python_semantic::default_lint_registry() ty_python_semantic::default_lint_registry()
} }
fn verbose(&self) -> bool {
false
}
} }
#[salsa::db] #[salsa::db]

View file

@ -113,6 +113,9 @@ pub struct Project {
/// the project including the virtual files that might exists in the editor. /// the project including the virtual files that might exists in the editor.
#[default] #[default]
check_mode: CheckMode, check_mode: CheckMode,
#[default]
verbose_flag: bool,
} }
/// A progress reporter. /// A progress reporter.
@ -368,6 +371,16 @@ impl Project {
self.reload_files(db); self.reload_files(db);
} }
pub fn set_verbose(self, db: &mut dyn Db, verbose: bool) {
if self.verbose_flag(db) != verbose {
self.set_verbose_flag(db).to(verbose);
}
}
pub fn verbose(self, db: &dyn Db) -> bool {
self.verbose_flag(db)
}
/// Returns the paths that should be checked. /// Returns the paths that should be checked.
/// ///
/// The default is to check the entire project in which case this method returns /// The default is to check the entire project in which case this method returns

View file

@ -12,6 +12,9 @@ pub trait Db: SourceDb {
fn rule_selection(&self, file: File) -> &RuleSelection; fn rule_selection(&self, file: File) -> &RuleSelection;
fn lint_registry(&self) -> &LintRegistry; fn lint_registry(&self) -> &LintRegistry;
/// Whether ty is running with logging verbosity INFO or higher (`-v` or more).
fn verbose(&self) -> bool;
} }
#[cfg(test)] #[cfg(test)]
@ -126,6 +129,10 @@ pub(crate) mod tests {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
default_lint_registry() default_lint_registry()
} }
fn verbose(&self) -> bool {
false
}
} }
#[salsa::db] #[salsa::db]

View file

@ -4811,22 +4811,29 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
// Add search paths information to the diagnostic // Add search paths information to the diagnostic
// Use the same search paths function that is used in actual module resolution // Use the same search paths function that is used in actual module resolution
let mut search_paths = let verbose = self.db().verbose();
search_paths(self.db(), ModuleResolveMode::StubsAllowed).peekable(); let search_paths = search_paths(self.db(), ModuleResolveMode::StubsAllowed);
if search_paths.peek().is_some() { diagnostic.info(format_args!(
diagnostic.info(format_args!( "Searched in the following paths during module resolution:"
"Searched in the following paths during module resolution:" ));
));
for (index, path) in search_paths.enumerate() { let mut search_paths = search_paths.enumerate();
while let Some((index, path)) = search_paths.next() {
if index > 4 && !verbose {
let more = search_paths.count() + 1;
diagnostic.info(format_args!( diagnostic.info(format_args!(
" {}. {} ({})", " ... and {more} more paths. Run with `-v` to see all paths."
index + 1,
path,
path.describe_kind()
)); ));
break;
} }
diagnostic.info(format_args!(
" {}. {} ({})",
index + 1,
path,
path.describe_kind()
));
} }
diagnostic.info( diagnostic.info(

View file

@ -255,6 +255,10 @@ impl ty_python_semantic::Db for CorpusDb {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
default_lint_registry() default_lint_registry()
} }
fn verbose(&self) -> bool {
false
}
} }
#[salsa::db] #[salsa::db]

View file

@ -88,6 +88,10 @@ impl SemanticDb for Db {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
default_lint_registry() default_lint_registry()
} }
fn verbose(&self) -> bool {
false
}
} }
#[salsa::db] #[salsa::db]

View file

@ -93,6 +93,10 @@ impl SemanticDb for TestDb {
fn lint_registry(&self) -> &LintRegistry { fn lint_registry(&self) -> &LintRegistry {
default_lint_registry() default_lint_registry()
} }
fn verbose(&self) -> bool {
false
}
} }
#[salsa::db] #[salsa::db]