[ty] Fix a bug with "all_submodule_names_for_package" API

The names of the submodules returned should be *complete*. This
is the contract of `Module::name`. However, we were previously
only returning the basename of the submodule.
This commit is contained in:
Andrew Gallant 2025-09-16 13:05:33 -04:00 committed by Andrew Gallant
parent cf16fc4aa4
commit 6ec52991cb
3 changed files with 21 additions and 14 deletions

View file

@ -1912,7 +1912,7 @@ fn submodule_cache_invalidation_created() -> anyhow::Result<()> {
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@"foo", @"bar.foo",
); );
std::fs::write(case.project_path("bar/wazoo.py").as_std_path(), "")?; std::fs::write(case.project_path("bar/wazoo.py").as_std_path(), "")?;
@ -1922,8 +1922,8 @@ fn submodule_cache_invalidation_created() -> anyhow::Result<()> {
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@r" @r"
foo bar.foo
wazoo bar.wazoo
", ",
); );
@ -1944,8 +1944,8 @@ fn submodule_cache_invalidation_deleted() -> anyhow::Result<()> {
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@r" @r"
foo bar.foo
wazoo bar.wazoo
", ",
); );
@ -1955,7 +1955,7 @@ fn submodule_cache_invalidation_deleted() -> anyhow::Result<()> {
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@"foo", @"bar.foo",
); );
Ok(()) Ok(())
@ -1969,7 +1969,7 @@ fn submodule_cache_invalidation_created_then_deleted() -> anyhow::Result<()> {
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@"foo", @"bar.foo",
); );
std::fs::write(case.project_path("bar/wazoo.py").as_std_path(), "")?; std::fs::write(case.project_path("bar/wazoo.py").as_std_path(), "")?;
@ -1982,7 +1982,7 @@ fn submodule_cache_invalidation_created_then_deleted() -> anyhow::Result<()> {
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@"foo", @"bar.foo",
); );
Ok(()) Ok(())
@ -1997,7 +1997,7 @@ fn submodule_cache_invalidation_after_pyproject_created() -> anyhow::Result<()>
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@"foo", @"bar.foo",
); );
case.update_options(Options::default())?; case.update_options(Options::default())?;
@ -2009,8 +2009,8 @@ fn submodule_cache_invalidation_after_pyproject_created() -> anyhow::Result<()>
insta::assert_snapshot!( insta::assert_snapshot!(
case.sorted_submodule_names("bar").join("\n"), case.sorted_submodule_names("bar").join("\n"),
@r" @r"
foo bar.foo
wazoo bar.wazoo
", ",
); );

View file

@ -203,7 +203,9 @@ fn all_submodule_names_for_package<'db>(
}) })
.filter_map(|entry| { .filter_map(|entry| {
let stem = entry.path().file_stem()?; let stem = entry.path().file_stem()?;
let name = ModuleName::new(stem)?; let mut name = module.name(db).clone();
name.extend(&ModuleName::new(stem)?);
let (kind, file) = if entry.file_type().is_directory() { let (kind, file) = if entry.file_type().is_directory() {
( (
ModuleKind::Package, ModuleKind::Package,
@ -239,7 +241,9 @@ fn all_submodule_names_for_package<'db>(
}) })
.filter_map(|entry| { .filter_map(|entry| {
let stem = entry.path().file_stem()?; let stem = entry.path().file_stem()?;
let name = ModuleName::new(stem)?; let mut name = module.name(db).clone();
name.extend(&ModuleName::new(stem)?);
let (kind, file) = if entry.file_type().is_directory() { let (kind, file) = if entry.file_type().is_directory() {
( (
ModuleKind::Package, ModuleKind::Package,

View file

@ -181,8 +181,11 @@ impl<'db> SemanticModel<'db> {
let mut completions = vec![]; let mut completions = vec![];
for submodule in module.all_submodules(self.db) { for submodule in module.all_submodules(self.db) {
let ty = Type::module_literal(self.db, self.file, *submodule); let ty = Type::module_literal(self.db, self.file, *submodule);
let Some(base) = submodule.name(self.db).components().next_back() else {
continue;
};
completions.push(Completion { completions.push(Completion {
name: Name::new(submodule.name(self.db).as_str()), name: Name::new(base),
ty: Some(ty), ty: Some(ty),
kind: None, kind: None,
builtin, builtin,