mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-03-22 06:50:54 +07:00
PY-76642 Add correct imports when generating type hints containing TypedDict
(cherry picked from commit 5c7761bea54741d68a7137788a46785db61f4247) IJ-CR-149697 GitOrigin-RevId: 9eafefe6cef2bd599e6b84cf7d199f72c675b14f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1d8c4eebd6
commit
2864833fca
@@ -335,8 +335,17 @@ public final class PyTypeHintGenerationUtil {
|
||||
else if (type instanceof PyTupleType && useGenericAliasFromTyping) {
|
||||
typingTypes.add("Tuple");
|
||||
}
|
||||
else if (type instanceof PyTypedDictType) {
|
||||
typingTypes.add("Dict");
|
||||
else if (type instanceof PyTypedDictType typedDictType) {
|
||||
if (typedDictType.isInferred()) {
|
||||
if (useGenericAliasFromTyping) {
|
||||
typingTypes.add("Dict");
|
||||
}
|
||||
}
|
||||
else {
|
||||
symbols.add((PsiNamedElement)typedDictType.getDeclarationElement());
|
||||
// Don't go through its type arguments
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (PyType pyType : ((PyCollectionType)type).getElementTypes()) {
|
||||
collectImportTargetsFromType(pyType, context, symbols, typingTypes);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
from typing import TypedDict
|
||||
|
||||
|
||||
class Movie(TypedDict):
|
||||
name: str
|
||||
year: int
|
||||
|
||||
|
||||
def blockbuster() -> Movie:
|
||||
...
|
||||
@@ -0,0 +1,3 @@
|
||||
from lib import blockbuster
|
||||
|
||||
mo<caret>vie = blockbuster()
|
||||
@@ -0,0 +1,3 @@
|
||||
from lib import blockbuster, Movie
|
||||
|
||||
movie: [Movie] = blockbuster()
|
||||
@@ -0,0 +1 @@
|
||||
v<caret>ar = {"foo": 42, "bar": "baz"}
|
||||
@@ -0,0 +1 @@
|
||||
v<caret>ar = {"foo": 42, "bar": "baz"}
|
||||
@@ -0,0 +1,3 @@
|
||||
from typing import Dict, Union
|
||||
|
||||
var: [Dict[str, Union[str, int]]] = {"foo": 42, "bar": "baz"}
|
||||
@@ -0,0 +1 @@
|
||||
var: [dict[str, str | int]] = {"foo": 42, "bar": "baz"}
|
||||
@@ -289,6 +289,23 @@ public class PyAnnotateVariableTypeIntentionTest extends PyIntentionTestCase {
|
||||
doTest(LanguageLevel.getLatest());
|
||||
}
|
||||
|
||||
// PY-76642
|
||||
public void testAnnotationDeclaredTypedDict() {
|
||||
doMultiFileAnnotationTest();
|
||||
}
|
||||
|
||||
// PY-76642
|
||||
public void testAnnotationInferredTypedDict() {
|
||||
// builtin dict and "|" operator will be used
|
||||
doTest(LanguageLevel.getLatest());
|
||||
}
|
||||
|
||||
// PY-76642
|
||||
public void testAnnotationInferredTypedDictPre39() {
|
||||
// typing.Dict and typing.Union will be used
|
||||
doTest(LanguageLevel.PYTHON36);
|
||||
}
|
||||
|
||||
private void doAnnotationTest() {
|
||||
doTest(LanguageLevel.PYTHON36);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user