Search in sources :

Example 1 with PyTypedElement

use of com.jetbrains.python.psi.PyTypedElement in project intellij-community by JetBrains.

the class PyiTypeTest method doTest.

private void doTest(@NotNull String expectedType) {
    myFixture.copyDirectoryToProject("pyi/type/" + getTestName(true), "");
    myFixture.copyDirectoryToProject("typing", "");
    PsiDocumentManager.getInstance(myFixture.getProject()).commitAllDocuments();
    final String fileName = getTestName(false) + ".py";
    myFixture.configureByFile(fileName);
    final PsiElement element = myFixture.getElementAtCaret();
    assertNotNull("Could not find element at caret in: " + myFixture.getFile());
    assertInstanceOf(element, PyTypedElement.class);
    final PyTypedElement typedElement = (PyTypedElement) element;
    final Project project = element.getProject();
    final PsiFile containingFile = element.getContainingFile();
    assertType(expectedType, typedElement, TypeEvalContext.codeAnalysis(project, containingFile));
    assertType(expectedType, typedElement, TypeEvalContext.userInitiated(project, containingFile));
}
Also used : Project(com.intellij.openapi.project.Project) PyTypedElement(com.jetbrains.python.psi.PyTypedElement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyTypedElement

use of com.jetbrains.python.psi.PyTypedElement in project intellij-community by JetBrains.

the class PyIterableVariableMacro method getIterableElements.

@NotNull
protected List<PsiNamedElement> getIterableElements(@NotNull PsiElement element) {
    final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
    final List<PsiNamedElement> components = new ArrayList<>();
    for (PsiNamedElement namedElement : getVisibleNamedElements(element)) {
        if (namedElement instanceof PyTypedElement) {
            final PyType type = typeEvalContext.getType((PyTypedElement) namedElement);
            if (type != null && PyABCUtil.isSubtype(type, PyNames.ITERABLE, typeEvalContext)) {
                components.add(namedElement);
            }
        }
    }
    return components;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyType(com.jetbrains.python.psi.types.PyType) ArrayList(java.util.ArrayList) PyTypedElement(com.jetbrains.python.psi.PyTypedElement) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyTypedElement (com.jetbrains.python.psi.PyTypedElement)2 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 PyType (com.jetbrains.python.psi.types.PyType)1 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1