Search in sources :

Example 46 with PyExpression

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

the class PyAssignmentMappingTest method testMultiple.

public void testMultiple() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int TARGET_NUM = 3;
    Assert.assertEquals(TARGET_NUM + 1, marks.size());
    // const -> expr;
    PsiElement src = marks.get("<src>").getParent();
    PsiElement[] dsts = new PsiElement[TARGET_NUM];
    for (int i = 0; i < TARGET_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
    }
    PyAssignmentStatement stmt = (PyAssignmentStatement) src.getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(TARGET_NUM, mapping.size());
    for (int i = 0; i < TARGET_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(src, pair.getSecond());
    }
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyAssignmentStatement(com.jetbrains.python.psi.PyAssignmentStatement) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 47 with PyExpression

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

the class PyTypeTest method testParameterFromUsages.

public void testParameterFromUsages() {
    final String text = "def foo(bar):\n" + "    expr = bar\n" + "def use_foo(x):\n" + "    foo(x)\n" + "    foo(3)\n" + "    foo('bar')\n";
    final PyExpression expr = parseExpr(text);
    assertNotNull(expr);
    doTest("Union[Union[int, str], Any]", expr, TypeEvalContext.codeCompletion(expr.getProject(), expr.getContainingFile()));
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression)

Example 48 with PyExpression

use of com.jetbrains.python.psi.PyExpression 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)

Aggregations

PyExpression (com.jetbrains.python.psi.PyExpression)48 PsiElement (com.intellij.psi.PsiElement)22 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)9 Pair (com.intellij.openapi.util.Pair)8 PyAssignmentStatement (com.jetbrains.python.psi.PyAssignmentStatement)8 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)7 PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)4 Nullable (org.jetbrains.annotations.Nullable)4 PyFunctionTypeAnnotation (com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyType (com.jetbrains.python.psi.types.PyType)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 PyArgumentList (com.jetbrains.python.psi.PyArgumentList)2 PyCallExpression (com.jetbrains.python.psi.PyCallExpression)2 PyPathEvaluator (com.jetbrains.python.psi.impl.PyPathEvaluator)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 NotNull (org.jetbrains.annotations.NotNull)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 Editor (com.intellij.openapi.editor.Editor)1