Search in sources :

Example 1 with PyStructuralType

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

the class PyUsageTypeProvider method getUsageType.

public UsageType getUsageType(PsiElement element, @NotNull UsageTarget[] targets) {
    if (element instanceof PyElement) {
        if (PsiTreeUtil.getParentOfType(element, PyImportStatementBase.class) != null) {
            return IN_IMPORT;
        }
        if (element instanceof PyQualifiedExpression) {
            final PyExpression qualifier = ((PyQualifiedExpression) element).getQualifier();
            if (qualifier != null) {
                final TypeEvalContext context = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
                final PyType type = context.getType(qualifier);
                if (type == null || type instanceof PyStructuralType) {
                    return UNTYPED;
                }
            }
        }
        if (element instanceof PyReferenceExpression) {
            final PyCallExpression call = PsiTreeUtil.getParentOfType(element, PyCallExpression.class);
            if (call != null && call.isCalleeText(PyNames.ISINSTANCE)) {
                final PyExpression[] args = call.getArguments();
                if (args.length == 2) {
                    PyExpression typeExpression = args[1];
                    if (element == typeExpression) {
                        return USAGE_IN_ISINSTANCE;
                    }
                    typeExpression = PyPsiUtils.flattenParens(typeExpression);
                    if (typeExpression instanceof PySequenceExpression && element.getParent() == typeExpression) {
                        return USAGE_IN_ISINSTANCE;
                    }
                }
            }
            final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
            if (pyClass != null && PsiTreeUtil.isAncestor(pyClass.getSuperClassExpressionList(), element, true)) {
                return USAGE_IN_SUPERCLASS;
            }
        }
    }
    return null;
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) PyStructuralType(com.jetbrains.python.psi.types.PyStructuralType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 2 with PyStructuralType

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

the class PyTypeCheckerInspectionProblemRegistrar method getSingleCalleeProblemMessage.

@NotNull
private static String getSingleCalleeProblemMessage(@NotNull PyTypeCheckerInspection.AnalyzeArgumentResult argumentResult, @NotNull TypeEvalContext context) {
    final PyType actualType = argumentResult.getActualType();
    final PyType expectedType = argumentResult.getExpectedType();
    // see PyTypeCheckerInspection.Visitor.analyzeArgument()
    assert actualType != null;
    // see PyTypeCheckerInspection.Visitor.analyzeArgument()
    assert expectedType != null;
    final String actualTypeName = PythonDocumentationProvider.getTypeName(actualType, context);
    if (expectedType instanceof PyStructuralType) {
        final Set<String> expectedAttributes = ((PyStructuralType) expectedType).getAttributeNames();
        final Set<String> actualAttributes = getAttributes(actualType, context);
        if (actualAttributes != null) {
            final Sets.SetView<String> missingAttributes = Sets.difference(expectedAttributes, actualAttributes);
            if (missingAttributes.size() == 1) {
                return String.format("Type '%s' doesn't have expected attribute '%s'", actualTypeName, missingAttributes.iterator().next());
            } else {
                return String.format("Type '%s' doesn't have expected attributes %s", actualTypeName, StringUtil.join(missingAttributes, s -> String.format("'%s'", s), ", "));
            }
        }
    }
    final String expectedTypeRepresentation = getSingleCalleeExpectedTypeRepresentation(expectedType, argumentResult.getExpectedTypeAfterSubstitution(), context);
    return String.format("Expected type %s, got '%s' instead", expectedTypeRepresentation, actualTypeName);
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyNames(com.jetbrains.python.PyNames) PythonDocumentationProvider(com.jetbrains.python.documentation.PythonDocumentationProvider) PyType(com.jetbrains.python.psi.types.PyType) StringUtil(com.intellij.openapi.util.text.StringUtil) Predicate(java.util.function.Predicate) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) XmlStringUtil(com.intellij.xml.util.XmlStringUtil) com.jetbrains.python.psi(com.jetbrains.python.psi) Optional(java.util.Optional) ObjectUtils(com.intellij.util.ObjectUtils) PyStructuralType(com.jetbrains.python.psi.types.PyStructuralType) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull) Sets(com.google.common.collect.Sets) PyType(com.jetbrains.python.psi.types.PyType) PyStructuralType(com.jetbrains.python.psi.types.PyStructuralType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyStructuralType (com.jetbrains.python.psi.types.PyStructuralType)2 PyType (com.jetbrains.python.psi.types.PyType)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 Sets (com.google.common.collect.Sets)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiElement (com.intellij.psi.PsiElement)1 ObjectUtils (com.intellij.util.ObjectUtils)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 XmlStringUtil (com.intellij.xml.util.XmlStringUtil)1 PyNames (com.jetbrains.python.PyNames)1 PythonDocumentationProvider (com.jetbrains.python.documentation.PythonDocumentationProvider)1 com.jetbrains.python.psi (com.jetbrains.python.psi)1 PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 NotNull (org.jetbrains.annotations.NotNull)1