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;
}
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));
}
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();
}
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()));
}
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());
}
}
Aggregations