use of com.jetbrains.python.psi.types.PyType in project intellij-community by JetBrains.
the class PyTypeTest method doTest.
private static void doTest(final String expectedType, final PyExpression expr, final TypeEvalContext context) {
PyType actual = context.getType(expr);
final String actualType = PythonDocumentationProvider.getTypeName(actual, context);
assertEquals(expectedType, actualType);
}
use of com.jetbrains.python.psi.types.PyType in project intellij-community by JetBrains.
the class PyStubsTest method doTestNamedTuple.
private void doTestNamedTuple(@NotNull String expectedName, @NotNull List<String> expectedFields, @NotNull QualifiedName expectedCalleeName) {
final PyFile file = getTestFile();
final PyTargetExpression attribute = file.findTopLevelAttribute("nt");
assertNotNull(attribute);
final PyNamedTupleStub stub = attribute.getStub().getCustomStub(PyNamedTupleStub.class);
assertNotNull(stub);
assertEquals(expectedCalleeName, stub.getCalleeName());
final PyType typeFromStub = TypeEvalContext.codeInsightFallback(myFixture.getProject()).getType(attribute);
doTestNamedTuple(expectedName, expectedFields, typeFromStub);
assertNotParsed(file);
final FileASTNode astNode = file.getNode();
assertNotNull(astNode);
final PyType typeFromAst = TypeEvalContext.userInitiated(myFixture.getProject(), file).getType(attribute);
doTestNamedTuple(expectedName, expectedFields, typeFromAst);
}
use of com.jetbrains.python.psi.types.PyType in project intellij-community by JetBrains.
the class PyJavaTypeProvider method getParameterType.
public Ref<PyType> getParameterType(@NotNull final PyNamedParameter param, @NotNull final PyFunction func, @NotNull TypeEvalContext context) {
if (!(param.getParent() instanceof PyParameterList))
return null;
List<PyNamedParameter> params = ParamHelper.collectNamedParameters((PyParameterList) param.getParent());
final int index = params.indexOf(param);
if (index < 0)
return null;
final List<PyType> superMethodParameterTypes = new ArrayList<>();
PySuperMethodsSearch.search(func, context).forEach(psiElement -> {
if (psiElement instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) psiElement;
final PsiParameter[] psiParameters = method.getParameterList().getParameters();
int javaIndex = method.hasModifierProperty(PsiModifier.STATIC) ? index : index - 1;
if (javaIndex < psiParameters.length) {
PsiType paramType = psiParameters[javaIndex].getType();
if (paramType instanceof PsiClassType) {
final PsiClass psiClass = ((PsiClassType) paramType).resolve();
if (psiClass != null) {
superMethodParameterTypes.add(new PyJavaClassType(psiClass, false));
}
}
}
}
return true;
});
if (superMethodParameterTypes.size() > 0) {
final PyType type = superMethodParameterTypes.get(0);
if (type != null) {
return Ref.create(type);
}
}
return null;
}
use of com.jetbrains.python.psi.types.PyType in project intellij-community by JetBrains.
the class DocStringReferenceProvider method parseTypeReferences.
@NotNull
private static List<PsiReference> parseTypeReferences(@NotNull PsiElement anchor, @NotNull Substring s, int offset) {
final List<PsiReference> result = new ArrayList<>();
final PyTypeParser.ParseResult parseResult = PyTypeParser.parse(anchor, s.toString());
final Map<TextRange, ? extends PyType> types = parseResult.getTypes();
if (types.isEmpty()) {
result.add(new DocStringTypeReference(anchor, s.getTextRange().shiftRight(offset), s.getTextRange().shiftRight(offset), null, null));
}
offset = s.getTextRange().getStartOffset() + offset;
final Map<? extends PyType, TextRange> fullRanges = parseResult.getFullRanges();
for (Map.Entry<TextRange, ? extends PyType> pair : types.entrySet()) {
final PyType t = pair.getValue();
final TextRange range = pair.getKey().shiftRight(offset);
final TextRange fullRange = fullRanges.containsKey(t) ? fullRanges.get(t).shiftRight(offset) : range;
final PyImportElement importElement = parseResult.getImports().get(t);
result.add(new DocStringTypeReference(anchor, range, fullRange, t, importElement));
}
return result;
}
use of com.jetbrains.python.psi.types.PyType in project intellij-community by JetBrains.
the class NumpyDocStringTypeProvider method parseNumpyDocType.
@Nullable
private static PyType parseNumpyDocType(@NotNull PsiElement anchor, @NotNull String typeString) {
final String withoutOptional = cleanupOptional(typeString);
final Set<PyType> types = new LinkedHashSet<>();
if (withoutOptional != null) {
typeString = withoutOptional;
}
for (String typeName : getNumpyUnionType(typeString)) {
PyType parsedType = parseSingleNumpyDocType(anchor, typeName);
if (parsedType != null) {
types.add(parsedType);
}
}
if (!types.isEmpty() && withoutOptional != null) {
types.add(PyNoneType.INSTANCE);
}
return getPsiFacade(anchor).createUnionType(types);
}
Aggregations