diff --git a/crates/ty_python_semantic/resources/mdtest/import/nonstandard_conventions.md b/crates/ty_python_semantic/resources/mdtest/import/nonstandard_conventions.md index e17a026e32..3af8ec3a34 100644 --- a/crates/ty_python_semantic/resources/mdtest/import/nonstandard_conventions.md +++ b/crates/ty_python_semantic/resources/mdtest/import/nonstandard_conventions.md @@ -647,8 +647,8 @@ reveal_type(mypackage.imported.X) # revealed: int ## `from` Import of Other Package's Submodule -`from mypackage import submodule` from outside the package is not modeled as a side-effect on -`mypackage`, even in the importing file (this could be changed!). +`from mypackage import submodule` and `from mypackage.submodule import not_a_submodule` from outside +the package are both modeled as a side-effects on `mypackage`. ### In Stub @@ -663,18 +663,28 @@ reveal_type(mypackage.imported.X) # revealed: int X: int = 42 ``` +`package2/__init__.pyi`: + +```pyi +``` + +`package2/submodule.pyi`: + +```pyi +not_a_submodule: int +``` + `main.py`: ```py import mypackage +import package2 from mypackage import imported +from package2.submodule import not_a_submodule reveal_type(imported.X) # revealed: int - -# TODO: this would be nice to support, but it's dangerous with available_submodule_attributes -# for details, see: https://github.com/astral-sh/ty/issues/1488 -# error: [possibly-missing-attribute] "Submodule `imported` may not be available" -reveal_type(mypackage.imported.X) # revealed: Unknown +reveal_type(mypackage.imported.X) # revealed: int +reveal_type(package2.submodule.not_a_submodule) # revealed: int ``` ### In Non-Stub @@ -690,17 +700,28 @@ reveal_type(mypackage.imported.X) # revealed: Unknown X: int = 42 ``` +`package2/__init__.py`: + +```py +``` + +`package2/submodule.py`: + +```py +not_a_submodule: int +``` + `main.py`: ```py import mypackage +import package2 from mypackage import imported +from package2.submodule import not_a_submodule reveal_type(imported.X) # revealed: int - -# TODO: this would be nice to support, as it works at runtime -# error: [possibly-missing-attribute] "Submodule `imported` may not be available" -reveal_type(mypackage.imported.X) # revealed: Unknown +reveal_type(mypackage.imported.X) # revealed: int +reveal_type(package2.submodule.not_a_submodule) # revealed: int ``` ## `from` Import of Sibling Module diff --git a/crates/ty_python_semantic/src/semantic_index/builder.rs b/crates/ty_python_semantic/src/semantic_index/builder.rs index ff1fbda5c1..62aeab245d 100644 --- a/crates/ty_python_semantic/src/semantic_index/builder.rs +++ b/crates/ty_python_semantic/src/semantic_index/builder.rs @@ -1556,6 +1556,16 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> { .entry(name) .or_insert(ImportKind::ImportFrom); } + for name in &node.names { + let Some(relative_name) = ModuleName::new(&name.name) else { + continue; + }; + let mut full_name = module_name.clone(); + full_name.extend(&relative_name); + self.imported_modules + .entry(full_name) + .or_insert(ImportKind::ImportFrom); + } } } }