Search in sources :

Example 41 with TypeEvalContext

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

the class PyTypeTest method checkTypes.

private static void checkTypes(@NotNull String expectedType, @Nullable PyExpression expr) {
    assertNotNull(expr);
    for (TypeEvalContext context : getTypeEvalContexts(expr)) {
        final PyType actual = context.getType(expr);
        final String actualType = PythonDocumentationProvider.getTypeName(actual, context);
        assertEquals("Failed in " + context, expectedType, actualType);
    }
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 42 with TypeEvalContext

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

the class PyTypingTest method doTest.

private void doTest(@NotNull String expectedType, @NotNull String text) {
    myFixture.copyDirectoryToProject("typing", "");
    myFixture.configureByText(PythonFileType.INSTANCE, text);
    final PyExpression expr = myFixture.findElementByText("expr", PyExpression.class);
    final TypeEvalContext codeAnalysis = TypeEvalContext.codeAnalysis(expr.getProject(), expr.getContainingFile());
    final TypeEvalContext userInitiated = TypeEvalContext.userInitiated(expr.getProject(), expr.getContainingFile()).withTracing();
    assertType(expectedType, expr, codeAnalysis, "code analysis");
    assertType(expectedType, expr, userInitiated, "user initiated");
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 43 with TypeEvalContext

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

the class PyStubsTest method testVariableAnnotationsInExternalFiles.

public void testVariableAnnotationsInExternalFiles() {
    runWithLanguageLevel(LanguageLevel.PYTHON36, () -> {
        final PyFile current = getTestFile(getTestName(true) + "/main.py");
        final PyFile external = getTestFile(getTestName(true) + "/lib.py");
        final PyTargetExpression attr = current.findTopLevelAttribute("x");
        assertNotNull(attr);
        final TypeEvalContext context = TypeEvalContext.codeAnalysis(myFixture.getProject(), current);
        // Will turn into concrete type when we start saving annotations in stubs 
        assertNull(context.getType(attr));
        assertNotParsed(external);
    });
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 44 with TypeEvalContext

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

the class PyStubsTest method testMetaClass.

public void testMetaClass() {
    final PyFile file = getTestFile();
    final PyClass c = file.findTopLevelClass("C");
    assertNotNull(c);
    final TypeEvalContext context = TypeEvalContext.codeInsightFallback(myFixture.getProject());
    assertNotNull(c.getMetaClassType(context));
    final PyClass d = file.findTopLevelClass("D");
    assertNotNull(d);
    assertNotNull(d.getMetaClassType(context));
    assertNotParsed(file);
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Aggregations

TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)44 PyType (com.jetbrains.python.psi.types.PyType)15 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)8 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)6 PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)5 ArrayList (java.util.ArrayList)5 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)4 com.jetbrains.python.psi (com.jetbrains.python.psi)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 XmlStringUtil (com.intellij.xml.util.XmlStringUtil)3 PyNames (com.jetbrains.python.PyNames)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 PyClassType (com.jetbrains.python.psi.types.PyClassType)3 Lists (com.google.common.collect.Lists)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 ASTNode (com.intellij.lang.ASTNode)2