Search in sources :

Example 16 with PyExpression

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

the class PyForStatementNavigator method getPyForStatementByIterable.

@Nullable
public static PyForStatement getPyForStatementByIterable(final PsiElement element) {
    final PyForStatement forStatement = PsiTreeUtil.getParentOfType(element, PyForStatement.class, false);
    if (forStatement == null) {
        return null;
    }
    final PyExpression target = forStatement.getForPart().getTarget();
    if (target != null && PsiTreeUtil.isAncestor(target, element, false)) {
        return forStatement;
    }
    return null;
}
Also used : PyForStatement(com.jetbrains.python.psi.PyForStatement) PyExpression(com.jetbrains.python.psi.PyExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with PyExpression

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

the class PyKeyValueExpressionImpl method getType.

public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
    final PyType keyType = context.getType(getKey());
    final PyExpression value = getValue();
    PyType valueType = null;
    if (value != null) {
        valueType = context.getType(value);
    }
    return PyTupleType.create(this, Arrays.asList(keyType, valueType));
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) PyExpression(com.jetbrains.python.psi.PyExpression)

Example 18 with PyExpression

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

the class PyListCompExpressionImpl method getType.

@Nullable
@Override
public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
    final PyExpression resultExpr = getResultExpression();
    final PyBuiltinCache cache = PyBuiltinCache.getInstance(this);
    final PyClass list = cache.getClass("list");
    if (resultExpr != null && list != null) {
        final PyType elementType = context.getType(resultExpr);
        return new PyCollectionTypeImpl(list, false, Collections.singletonList(elementType));
    }
    return cache.getListType();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyType(com.jetbrains.python.psi.types.PyType) PyExpression(com.jetbrains.python.psi.PyExpression) PyCollectionTypeImpl(com.jetbrains.python.psi.types.PyCollectionTypeImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with PyExpression

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

the class PyTypeTest method testInheritedNamedTupleReplace.

// PY-19967
public void testInheritedNamedTupleReplace() {
    PyExpression expr = parseExpr("from collections import namedtuple\n" + "class MyClass(namedtuple('T', 'a b c')):\n" + "    def get_foo(self):\n" + "        return self.a\n" + "\n" + "inst = MyClass(1,2,3)\n" + "expr = inst._replace(a=2)\n");
    doTest("MyClass", expr, TypeEvalContext.userInitiated(expr.getProject(), expr.getContainingFile()));
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression)

Example 20 with PyExpression

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

the class PyTypeTest method testReturnTypeOfTypeForInstance.

// PY-7058
public void testReturnTypeOfTypeForInstance() {
    PyExpression expr = parseExpr("class C(object):\n" + "    pass\n" + "\n" + "x = C()\n" + "expr = type(x)\n");
    assertNotNull(expr);
    for (TypeEvalContext context : getTypeEvalContexts(expr)) {
        PyType type = context.getType(expr);
        assertInstanceOf(type, PyClassType.class);
        assertTrue("Got instance type instead of class type", ((PyClassType) type).isDefinition());
    }
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) 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