PY-34818 Suggest auto importing symbols referenced in f-strings

GitOrigin-RevId: 433bb6a1247620e994b103d0a5348b23ed642813
This commit is contained in:
Mikhail Golubev
2020-03-05 18:33:22 +00:00
committed by intellij-monorepo-bot
parent de78f2ee5d
commit 0fbb674a91
5 changed files with 16 additions and 4 deletions
@@ -50,12 +50,12 @@ public final class PythonImportUtils {
return PyImportCollectorFactory.getInstance().create(node, reference, refText, asName).addCandidates();
}
public static boolean isImportable(PsiElement ref_element) {
PyStatement parentStatement = PsiTreeUtil.getParentOfType(ref_element, PyStatement.class);
public static boolean isImportable(PsiElement refElement) {
PyStatement parentStatement = PsiTreeUtil.getParentOfType(refElement, PyStatement.class);
if (parentStatement instanceof PyGlobalStatement || parentStatement instanceof PyNonlocalStatement ||
parentStatement instanceof PyImportStatementBase) {
parentStatement instanceof PyImportStatementBase) {
return false;
}
return PsiTreeUtil.getParentOfType(ref_element, PyStringLiteralExpression.class, false, PyStatement.class) == null;
return PsiTreeUtil.getParentOfType(refElement, PyPlainStringElement.class, false, PyStatement.class) == null;
}
}
@@ -0,0 +1 @@
print(f'{<error descr="Unresolved reference 'lib'">l<caret>ib</error>}')
@@ -0,0 +1,3 @@
import lib
print(f'{lib}')
@@ -28,6 +28,7 @@ import com.jetbrains.python.codeInsight.imports.ImportCandidateHolder;
import com.jetbrains.python.codeInsight.imports.PythonImportUtils;
import com.jetbrains.python.formatter.PyCodeStyleSettings;
import com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyReferenceExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -215,6 +216,13 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
);
}
// PY-34818
public void testReferenceInsideFString() {
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> {
doMultiFileAutoImportTest("Import");
});
}
private void doTestProposedImportsOrdering(@NotNull String text, String @NotNull ... expected) {
doMultiFileAutoImportTest("Import", fix -> {
final List<String> candidates = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText(text));