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