From 315f1905fd4288d19ba57c35b5546077ebe258d8 Mon Sep 17 00:00:00 2001 From: Lada Gagina Date: Wed, 3 Nov 2021 14:52:33 +0300 Subject: [PATCH] PY-46344 Import abstract base classes from collections.abc, not collections GitOrigin-RevId: d4b7165f09f47c7cca6c3eb47b1e254e5b855b40 --- .../stdlib/PyStdlibCanonicalPathProvider.kt | 3 ++- .../main.py | 1 + .../quickFixes/PyAddImportQuickFixTest.java | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 python/testData/quickFixes/PyAddImportQuickFixTest/importAbstractContainersFromCollectionsABC/main.py diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibCanonicalPathProvider.kt b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibCanonicalPathProvider.kt index bb83e6f43399..7c7f05f2463c 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibCanonicalPathProvider.kt +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibCanonicalPathProvider.kt @@ -48,7 +48,8 @@ fun restoreStdlibCanonicalPath(qName: QualifiedName): QualifiedName? { val head = components[0] return when (head) { - "_abcoll", "_collections", "_collections_abc" -> replace0("collections", components) + "_abcoll", "_collections" -> replace0("collections", components) + "_collections_abc" -> replace0("collections.abc", components) "posix", "nt" -> replace0("os", components) "_functools" -> replace0("functools", components) diff --git a/python/testData/quickFixes/PyAddImportQuickFixTest/importAbstractContainersFromCollectionsABC/main.py b/python/testData/quickFixes/PyAddImportQuickFixTest/importAbstractContainersFromCollectionsABC/main.py new file mode 100644 index 000000000000..254cf6b373a6 --- /dev/null +++ b/python/testData/quickFixes/PyAddImportQuickFixTest/importAbstractContainersFromCollectionsABC/main.py @@ -0,0 +1 @@ +my_collection: Sized = list() diff --git a/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java b/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java index e861098d5070..2ea9bb60c68d 100644 --- a/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java @@ -355,6 +355,27 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase { doMultiFileNegativeTest("Import"); } + // PY-46344 + public void testImportAbstractContainersFromCollectionsABC() { + Consumer fileConsumer = file -> { + doMultiFileAutoImportTest("Import", fix -> { + final List candidates = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText()); + assertNotNull(candidates); + assertContainsElements(candidates, "collections.abc.Sized"); + assertDoesntContain(candidates, "collections.Sized"); + return false; + }); + }; + runWithAdditionalFileInLibDir( + "_collections_abc.py", + "__all__ = [\"Sized\"]\n" + + "__name__ = \"collections.abc\"\n" + + "class Sized:\n" + + " pass\n", + fileConsumer + ); + } + private void doTestProposedImportsOrdering(String @NotNull ... expected) { doMultiFileAutoImportTest("Import", fix -> { final List candidates = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText());