Search in sources :

Example 21 with PyType

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);
}
Also used : PyType(com.jetbrains.python.psi.types.PyType)

Example 22 with PyType

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);
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) PyType(com.jetbrains.python.psi.types.PyType) PyNamedTupleStub(com.jetbrains.python.psi.stubs.PyNamedTupleStub)

Example 23 with PyType

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;
}
Also used : ArrayList(java.util.ArrayList) PyType(com.jetbrains.python.psi.types.PyType) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) PyParameterList(com.jetbrains.python.psi.PyParameterList)

Example 24 with PyType

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;
}
Also used : PyImportElement(com.jetbrains.python.psi.PyImportElement) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) PyTypeParser(com.jetbrains.python.psi.types.PyTypeParser) PyType(com.jetbrains.python.psi.types.PyType) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with PyType

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);
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) NumpyDocString(com.jetbrains.python.documentation.docstrings.NumpyDocString) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyType (com.jetbrains.python.psi.types.PyType)42 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)13 PsiElement (com.intellij.psi.PsiElement)11 Nullable (org.jetbrains.annotations.Nullable)9 PyClassType (com.jetbrains.python.psi.types.PyClassType)6 NotNull (org.jetbrains.annotations.NotNull)6 PsiFile (com.intellij.psi.PsiFile)5 ArrayList (java.util.ArrayList)5 PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)4 PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)4 NumpyDocString (com.jetbrains.python.documentation.docstrings.NumpyDocString)3 PyExpression (com.jetbrains.python.psi.PyExpression)3 FileASTNode (com.intellij.lang.FileASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiReference (com.intellij.psi.PsiReference)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 PyClass (com.jetbrains.python.psi.PyClass)2 PyFunctionBuilder (com.jetbrains.python.psi.impl.PyFunctionBuilder)2 PyStructuralType (com.jetbrains.python.psi.types.PyStructuralType)2