Search in sources :

Example 6 with PyExpression

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

the class Py3TypeTest method doTest.

private void doTest(final String expectedType, final String text) {
    myFixture.configureByText(PythonFileType.INSTANCE, text);
    final PyExpression expr = myFixture.findElementByText("expr", PyExpression.class);
    final Project project = expr.getProject();
    final PsiFile containingFile = expr.getContainingFile();
    assertType(expectedType, expr, TypeEvalContext.codeAnalysis(project, containingFile));
    assertType(expectedType, expr, TypeEvalContext.userInitiated(project, containingFile));
}
Also used : Project(com.intellij.openapi.project.Project) PyExpression(com.jetbrains.python.psi.PyExpression) PsiFile(com.intellij.psi.PsiFile)

Example 7 with PyExpression

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

the class PyAssignmentMappingTest method testTupleMapped.

public void testTupleMapped() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int PAIR_NUM = 2;
    Assert.assertEquals(PAIR_NUM * 2, marks.size());
    PsiElement[] srcs = new PsiElement[PAIR_NUM];
    PsiElement[] dsts = new PsiElement[PAIR_NUM];
    for (int i = 0; i < PAIR_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // tuple expr -> assignment
    PyAssignmentStatement stmt = (PyAssignmentStatement) srcs[0].getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(PAIR_NUM, mapping.size());
    for (int i = 0; i < PAIR_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i], 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 8 with PyExpression

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

the class PyAssignmentMappingTest method testTuplePack.

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

Example 9 with PyExpression

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

the class PyAssignmentMappingTest method testParenthesizedTuple.

public void testParenthesizedTuple() throws Exception {
    //PY-2648
    Map<String, PsiElement> marks = loadTest();
    final int PAIR_NUM = 2;
    Assert.assertEquals(PAIR_NUM * 2, marks.size());
    PsiElement[] srcs = new PsiElement[PAIR_NUM];
    PsiElement[] dsts = new PsiElement[PAIR_NUM];
    for (int i = 0; i < PAIR_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // tuple expr -> assignment
    PyAssignmentStatement stmt = (PyAssignmentStatement) srcs[0].getParent().getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(PAIR_NUM, mapping.size());
    for (int i = 0; i < PAIR_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i], 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 10 with PyExpression

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

the class PyAssignmentMappingTest method testTupleUnpack.

public void testTupleUnpack() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int DST_NUM = 2;
    Assert.assertEquals(DST_NUM + 3, marks.size());
    PsiElement[] dsts = new PsiElement[DST_NUM];
    for (int i = 0; i < DST_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
    }
    PsiElement[] srcs = new PsiElement[DST_NUM];
    for (int i = 0; i < DST_NUM; i += 1) {
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent().getParent().getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // ident -> target expr
    PsiElement src = marks.get("<src>").getParent();
    PyAssignmentStatement stmt = (PyAssignmentStatement) src.getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(DST_NUM, mapping.size());
    for (int i = 0; i < DST_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i].getText(), pair.getSecond().getText());
    }
}
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)

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