Added new diagnostic rule reportCallIssue that covers issues relating to call expressions and arguments. This partially addresses #6973. (#7076)

This commit is contained in:
Eric Traut 2024-01-21 01:47:43 -08:00 committed by GitHub
parent 63637459ca
commit 6ac1a7eebf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 69 additions and 35 deletions

View File

@ -96,6 +96,8 @@ The following settings control pyrights diagnostic output (warnings or errors
<a name="reportAttributeAccessIssue"></a> **reportAttributeAccessIssue** [boolean or string, optional]: Generate or suppress diagnostics related to attribute accesses. The default value for this setting is `"error"`.
<a name="reportCallIssue"></a> **reportCallIssue** [boolean or string, optional]: Generate or suppress diagnostics related to call expressions and arguments passed to a call target. The default value for this setting is `"error"`.
<a name="reportInconsistentOverload"></a> **reportInconsistentOverload** [boolean or string, optional]: Generate or suppress diagnostics for an overloaded function that has overload signatures that are inconsistent with each other or with the implementation. The default value for this setting is `"error"`.
<a name="reportIndexIssue"></a> **reportIndexIssue** [boolean or string, optional]: Generate or suppress diagnostics related to index operations and expressions. The default value for this setting is `"error"`.
@ -348,6 +350,7 @@ The following table lists the default severity levels for each diagnostic rule w
| reportAbstractUsage | "none" | "error" | "error" | "error" |
| reportAssertTypeFailure | "none" | "error" | "error" | "error" |
| reportAttributeAccessIssue | "none" | "error" | "error" | "error" |
| reportCallIssue | "none" | "error" | "error" | "error" |
| reportGeneralTypeIssues | "none" | "error" | "error" | "error" |
| reportInconsistentOverload | "none" | "error" | "error" | "error" |
| reportIndexIssue | "none" | "error" | "error" | "error" |

View File

@ -170,7 +170,7 @@ function applyPartialTransform(
if (applicableOverloads.length === 0) {
if (sawArgErrors) {
evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.noOverload().format({
name: origFunctionType.overloads[0].details.name,
}),
@ -275,7 +275,7 @@ function applyPartialTransformToFunction(
if (!reportedPositionalError) {
if (errorNode) {
evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
paramListDetails.positionParamCount === 1
? LocMessage.argPositionalExpectedOne()
: LocMessage.argPositionalExpectedCount().format({
@ -331,7 +331,7 @@ function applyPartialTransformToFunction(
if (paramListDetails.kwargsIndex === undefined) {
if (errorNode) {
evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.paramNameMissing().format({ name: arg.name.value }),
arg.name
);
@ -374,7 +374,7 @@ function applyPartialTransformToFunction(
if (paramMap.has(paramName)) {
if (errorNode) {
evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.paramAlreadyAssigned().format({ name: arg.name.value }),
arg.name
);

View File

@ -1020,7 +1020,7 @@ export function validateDataClassTransformDecorator(
node.arguments.forEach((arg) => {
if (!arg.name || arg.argumentCategory !== ArgumentCategory.Simple) {
evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.dataClassTransformPositionalParam(),
arg
);

View File

@ -82,7 +82,7 @@ export function createNamedTupleType(
}
if (argList.length === 0) {
evaluator.addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.namedTupleFirstArg(), errorNode);
evaluator.addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.namedTupleFirstArg(), errorNode);
} else {
const nameArg = argList[0];
if (nameArg.argumentCategory !== ArgumentCategory.Simple) {
@ -162,7 +162,7 @@ export function createNamedTupleType(
const entryTypes: Type[] = [];
if (argList.length < 2) {
evaluator.addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.namedTupleSecondArg(), errorNode);
evaluator.addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.namedTupleSecondArg(), errorNode);
addGenericGetAttribute = true;
} else {
const entriesArg = argList[1];

View File

@ -8898,7 +8898,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
diagAddendum.addMessage(LocAddendum.argumentTypes().format({ types: argTypes.join(', ') }));
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.noOverload().format({ name: functionName }) + diagAddendum.getString(),
errorNode
);
@ -8925,7 +8925,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (emitNoOverloadFoundError) {
const functionName = bestMatch.overload.details.name || '<anonymous function>';
const diagnostic = addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.noOverload().format({ name: functionName }),
errorNode
);
@ -9116,7 +9116,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (callTypeResult.type.specialForm) {
const exprNode = errorNode.nodeType === ParseNodeType.Call ? errorNode.leftExpression : errorNode;
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.objectNotCallable().format({
type: printType(callTypeResult.type, { expandTypeAlias: true }),
}),
@ -9284,7 +9284,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
}
case TypeCategory.Module: {
addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.moduleNotCallable(), errorNode);
addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.moduleNotCallable(), errorNode);
return { argumentErrors: true };
}
@ -9304,7 +9304,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
): CallResult {
if (TypeBase.isInstantiable(expandedCallType)) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.callableNotInstantiable().format({
type: printType(expandedCallType),
}),
@ -9544,7 +9544,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
inferenceContext: InferenceContext | undefined
): CallResult {
if (expandedCallType.literalValue !== undefined) {
addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.literalNotCallable(), errorNode);
addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.literalNotCallable(), errorNode);
return { returnType: UnknownType.create(), argumentErrors: true };
}
@ -9556,7 +9556,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (isInstantiableMetaclass(expandedCallType)) {
if (expandedCallType.typeArguments && expandedCallType.isTypeArgumentExplicit) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.objectNotCallable().format({
type: printType(expandedCallType),
}),
@ -9666,7 +9666,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (ClassType.isSpecialFormClass(expandedCallType)) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.typeNotIntantiable().format({ type: className }),
errorNode
);
@ -9837,7 +9837,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (!callMethodType) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.objectNotCallable().format({
type: printType(expandedCallType),
}),
@ -10102,7 +10102,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (argIndex < positionalOnlyLimitIndex && argList[argIndex].name) {
const nameNode = argList[argIndex].name;
if (nameNode) {
addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.argPositional(), nameNode);
addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.argPositional(), nameNode);
reportedArgError = true;
}
}
@ -10142,7 +10142,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (tooManyPositionals) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
positionParamLimitIndex === 1
? LocMessage.argPositionalExpectedOne()
: LocMessage.argPositionalExpectedCount().format({
@ -10187,7 +10187,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
positionParamLimitIndex === 1
? LocMessage.argPositionalExpectedOne()
: LocMessage.argPositionalExpectedCount().format({
@ -10266,7 +10266,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (isParamVariadic && !isArgCompatibleWithVariadic) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.unpackedArgWithVariadicParam(),
argList[argIndex].valueExpression || errorNode
);
@ -10340,7 +10340,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
// Have we run out of arguments and still have parameters left to fill?
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
remainingArgCount === 1
? LocMessage.argMorePositionalExpectedOne()
: LocMessage.argMorePositionalExpectedCount().format({
@ -10441,7 +10441,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (argsRemainingCount > 0) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
argsRemainingCount === 1
? LocMessage.argMorePositionalExpectedOne()
: LocMessage.argMorePositionalExpectedCount().format({
@ -10536,7 +10536,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (!diag.isEmpty()) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.unpackedTypedDictArgument() + diag.getString(),
argList[argIndex].valueExpression || errorNode
);
@ -10615,7 +10615,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (!isValidMappingType) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.unpackedDictArgumentNotMapping(),
argList[argIndex].valueExpression || errorNode
);
@ -10640,7 +10640,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (paramEntry.argsReceived > 0) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.paramAlreadyAssigned().format({ name: paramNameValue }),
paramName
);
@ -10692,7 +10692,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
} else {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.paramNameMissing().format({ name: paramName.value }),
paramName
);
@ -10705,7 +10705,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
} else {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
positionParamLimitIndex === 1
? LocMessage.argPositionalExpectedOne()
: LocMessage.argPositionalExpectedCount().format({
@ -10795,7 +10795,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
const missingParamNames = unassignedParams.map((p) => `"${p}"`).join(', ');
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
unassignedParams.length === 1
? LocMessage.argMissingForParam().format({ name: missingParamNames })
: LocMessage.argMissingForParams().format({ names: missingParamNames }),
@ -10882,7 +10882,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
) {
if (!isDiagnosticSuppressedForNode(errorNode) && !isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.typeVarTupleMustBeUnpacked(),
argParam.argument.valueExpression ?? errorNode
);
@ -11365,7 +11365,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (!sawParamSpecArgs || !sawParamSpecKwargs) {
if (!isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.paramSpecArgsMissing().format({ type: printType(type.details.paramSpec) }),
errorNode
);
@ -11760,7 +11760,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (argumentErrors) {
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.paramSpecArgsMissing().format({
type: printType(functionType.details.paramSpec),
}),
@ -12646,7 +12646,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
let className = '';
if (argList.length !== 2) {
addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.newTypeParamCount(), errorNode);
addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.newTypeParamCount(), errorNode);
return undefined;
}

View File

@ -79,7 +79,7 @@ export function createTypedDictType(
// Point2D = TypedDict('Point2D', x=int, y=int, label=str)
let className: string | undefined;
if (argList.length === 0) {
evaluator.addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.typedDictFirstArg(), errorNode);
evaluator.addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.typedDictFirstArg(), errorNode);
} else {
const nameArg = argList[0];
if (
@ -119,7 +119,7 @@ export function createTypedDictType(
let usingDictSyntax = false;
if (argList.length < 2) {
evaluator.addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, LocMessage.typedDictSecondArgDict(), errorNode);
evaluator.addDiagnostic(DiagnosticRule.reportCallIssue, LocMessage.typedDictSecondArgDict(), errorNode);
} else {
const entriesArg = argList[1];
@ -200,7 +200,7 @@ export function createTypedDictType(
}
} else {
evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportCallIssue,
LocMessage.typedDictExtraArgs(),
arg.valueExpression || errorNode
);

View File

@ -168,6 +168,9 @@ export interface DiagnosticRuleSet {
// Report issues related to attribute access expressions?
reportAttributeAccessIssue: DiagnosticLevel;
// Report issues related to call expressions?
reportCallIssue: DiagnosticLevel;
// Report inconsistencies with function overload signatures?
reportInconsistentOverload: DiagnosticLevel;
@ -411,6 +414,7 @@ export function getDiagLevelDiagnosticRules() {
DiagnosticRule.reportAbstractUsage,
DiagnosticRule.reportAssertTypeFailure,
DiagnosticRule.reportAttributeAccessIssue,
DiagnosticRule.reportCallIssue,
DiagnosticRule.reportInconsistentOverload,
DiagnosticRule.reportIndexIssue,
DiagnosticRule.reportInvalidTypeArguments,
@ -512,6 +516,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
reportAbstractUsage: 'none',
reportAssertTypeFailure: 'none',
reportAttributeAccessIssue: 'none',
reportCallIssue: 'none',
reportInconsistentOverload: 'none',
reportIndexIssue: 'none',
reportInvalidTypeArguments: 'none',
@ -609,6 +614,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
reportAbstractUsage: 'error',
reportAssertTypeFailure: 'error',
reportAttributeAccessIssue: 'error',
reportCallIssue: 'error',
reportInconsistentOverload: 'error',
reportIndexIssue: 'error',
reportInvalidTypeArguments: 'error',
@ -706,6 +712,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
reportAbstractUsage: 'error',
reportAssertTypeFailure: 'error',
reportAttributeAccessIssue: 'error',
reportCallIssue: 'error',
reportInconsistentOverload: 'error',
reportIndexIssue: 'error',
reportInvalidTypeArguments: 'error',
@ -803,6 +810,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
reportAbstractUsage: 'error',
reportAssertTypeFailure: 'error',
reportAttributeAccessIssue: 'error',
reportCallIssue: 'error',
reportInconsistentOverload: 'error',
reportIndexIssue: 'error',
reportInvalidTypeArguments: 'error',

View File

@ -38,6 +38,7 @@ export enum DiagnosticRule {
reportAbstractUsage = 'reportAbstractUsage',
reportAssertTypeFailure = 'reportAssertTypeFailure',
reportAttributeAccessIssue = 'reportAttributeAccessIssue',
reportCallIssue = 'reportCallIssue',
reportInconsistentOverload = 'reportInconsistentOverload',
reportIndexIssue = 'reportIndexIssue',
reportInvalidTypeArguments = 'reportInvalidTypeArguments',

View File

@ -463,6 +463,22 @@
false
]
},
"reportCallIssue": {
"type": [
"string",
"boolean"
],
"description": "Diagnostics for issues involving call expressions and arguments.",
"default": "error",
"enum": [
"none",
"information",
"warning",
"error",
true,
false
]
},
"reportInconsistentOverload": {
"type": [
"string",

View File

@ -269,6 +269,12 @@
"title": "Controls reporting of issues related to attribute accesses",
"default": "error"
},
"reportCallIssue": {
"$id": "#/properties/reportCallIssue",
"$ref": "#/definitions/diagnostic",
"title": "Controls reporting of issues related to call expressions and arguments",
"default": "error"
},
"reportInconsistentOverload": {
"$id": "#/properties/reportInconsistentOverload",
"$ref": "#/definitions/diagnostic",