Skip to content

Commit

Permalink
report partial Anys as well
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Feb 12, 2024
1 parent 3c5ac6a commit 0992c57
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 147 deletions.
128 changes: 90 additions & 38 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ import {
isLiteralType,
isLiteralTypeOrUnion,
isNoneInstance,
isPartlyAny,
isPartlyUnknown,
isProperty,
isTupleClass,
Expand Down Expand Up @@ -473,26 +474,34 @@ export class Checker extends ParseTreeWalker {
LocMessage.paramTypeUnknown().format({ paramName: param.name.value }),
param.name
);
} else if (isPartlyUnknown(paramType)) {
const diagAddendum = new DiagnosticAddendum();
diagAddendum.addMessage(
LocAddendum.paramType().format({
paramType: this._evaluator.printType(paramType, { expandTypeAlias: true }),
})
);
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownParameterType,
LocMessage.paramTypePartiallyUnknown().format({
paramName: param.name.value,
}) + diagAddendum.getString(),
param.name
);
} else if (isAny(paramType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.paramTypeAny().format({ paramName: param.name.value }),
param.name
);
} else {
const partlyUnknown = isPartlyUnknown(paramType);
if (partlyUnknown || isPartlyAny(paramType)) {
const diagAddendum = new DiagnosticAddendum();
diagAddendum.addMessage(
LocAddendum.paramType().format({
paramType: this._evaluator.printType(paramType, { expandTypeAlias: true }),
})
);
this._evaluator.addDiagnostic(
partlyUnknown
? DiagnosticRule.reportUnknownParameterType
: DiagnosticRule.reportAny,
(partlyUnknown
? LocMessage.paramTypePartiallyUnknown()
: LocMessage.paramTypePartiallyAny()
).format({
paramName: param.name.value,
}) + diagAddendum.getString(),
param.name
);
}
}
}

Expand Down Expand Up @@ -763,6 +772,12 @@ export class Checker extends ParseTreeWalker {
LocMessage.paramTypeAny().format({ paramName: param.name.value }),
param.name
);
} else if (isPartlyAny(paramType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.paramTypePartiallyAny().format({ paramName: param.name.value }),
param.name
);
}
}
}
Expand All @@ -776,20 +791,32 @@ export class Checker extends ParseTreeWalker {
LocMessage.lambdaReturnTypeUnknown(),
node.expression
);
} else if (isPartlyUnknown(returnType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownLambdaType,
LocMessage.lambdaReturnTypePartiallyUnknown().format({
returnType: this._evaluator.printType(returnType, { expandTypeAlias: true }),
}),
node.expression
);
} else if (isAny(returnType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.lambdaReturnTypeAny(),
node.expression
);
} else {
const partlyUnknown = isPartlyUnknown(returnType);
if (partlyUnknown || isPartlyAny(returnType)) {
const formatter = {
returnType: this._evaluator.printType(returnType, { expandTypeAlias: true }),
};
if (partlyUnknown) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownLambdaType,
LocMessage.lambdaReturnTypePartiallyUnknown().format(formatter),
node.expression
);
} else {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.lambdaReturnTypePartiallyAny().format(formatter),
node.expression
);
}
}
}
}

Expand Down Expand Up @@ -1057,20 +1084,33 @@ export class Checker extends ParseTreeWalker {
LocMessage.returnTypeUnknown(),
node.returnExpression ?? node
);
} else if (isPartlyUnknown(returnType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownVariableType,
LocMessage.returnTypePartiallyUnknown().format({
returnType: this._evaluator.printType(returnType, { expandTypeAlias: true }),
}),
node.returnExpression ?? node
);
} else if (isAny(returnType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.returnTypeAny(),
node.returnExpression ?? node
);
} else {
const partlyUnknown = isPartlyUnknown(returnType);
if (partlyUnknown || isPartlyAny(returnType)) {
const formatter = {
returnType: this._evaluator.printType(returnType, { expandTypeAlias: true }),
};
const returnNode = node.returnExpression ?? node;
if (partlyUnknown) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownVariableType,
LocMessage.returnTypePartiallyUnknown().format(formatter),
returnNode
);
} else {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.returnTypePartiallyAny().format(formatter),
returnNode
);
}
}
}
}

Expand Down Expand Up @@ -4769,16 +4809,28 @@ export class Checker extends ParseTreeWalker {
LocMessage.returnTypeUnknown(),
node.name
);
} else if (isPartlyUnknown(returnType)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownParameterType,
LocMessage.returnTypePartiallyUnknown().format({
returnType: this._evaluator.printType(returnType, { expandTypeAlias: true }),
}),
node.name
);
} else if (isAny(returnType)) {
this._evaluator.addDiagnostic(DiagnosticRule.reportAny, LocMessage.returnTypeAny(), node.name);
} else {
const partlyUnknown = isPartlyUnknown(returnType);
if (partlyUnknown || isPartlyAny(returnType)) {
const formatter = {
returnType: this._evaluator.printType(returnType, { expandTypeAlias: true }),
};
if (partlyUnknown) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportUnknownParameterType,
LocMessage.returnTypePartiallyUnknown().format(formatter),
node.name
);
} else {
this._evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.returnTypePartiallyAny().format(formatter),
node.name
);
}
}
}
}

Expand Down
39 changes: 26 additions & 13 deletions packages/pyright-internal/src/analyzer/patternMatching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
isLiteralTypeOrUnion,
isMetaclassInstance,
isNoneInstance,
isPartlyAny,
isPartlyUnknown,
isTupleClass,
isUnboundedTupleClass,
Expand Down Expand Up @@ -1592,25 +1593,37 @@ export function assignTypeToPatternTargets(
LocMessage.wildcardPatternTypeUnknown(),
pattern.target
);
} else if (isPartlyUnknown(narrowedType)) {
const diagAddendum = new DiagnosticAddendum();
diagAddendum.addMessage(
LocAddendum.typeOfSymbol().format({
name: '_',
type: evaluator.printType(narrowedType, { expandTypeAlias: true }),
})
);
evaluator.addDiagnostic(
DiagnosticRule.reportUnknownVariableType,
LocMessage.wildcardPatternTypePartiallyUnknown() + diagAddendum.getString(),
pattern.target
);
} else if (isAny(narrowedType)) {
evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.wildcardPatternTypeAny(),
pattern.target
);
} else {
const partlyUnknown = isPartlyUnknown(narrowedType);
if (partlyUnknown || isPartlyAny(narrowedType)) {
const diagAddendum = new DiagnosticAddendum();
diagAddendum.addMessage(
LocAddendum.typeOfSymbol().format({
name: '_',
type: evaluator.printType(narrowedType, { expandTypeAlias: true }),
})
);
const addendum = diagAddendum.getString();
if (partlyUnknown) {
evaluator.addDiagnostic(
DiagnosticRule.reportUnknownVariableType,
LocMessage.wildcardPatternTypePartiallyUnknown() + addendum,
pattern.target
);
} else {
evaluator.addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.wildcardPatternTypePartiallyAny() + addendum,
pattern.target
);
}
}
}
}
} else {
Expand Down
92 changes: 57 additions & 35 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ import {
isNoneInstance,
isNoneTypeClass,
isOptionalType,
isPartlyAny,
isPartlyUnknown,
isProperty,
isTupleClass,
Expand Down Expand Up @@ -12073,30 +12074,42 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
LocMessage.argTypeUnknown() + diagAddendum.getString(),
argParam.errorNode
);
} else if (isPartlyUnknown(simplifiedType)) {
// If the parameter type is also partially unknown, don't report
// the error because it's likely that the partially-unknown type
// arose due to bidirectional type matching.
if (!isPartlyUnknown(argParam.paramType)) {
const diagAddendum = getDiagAddendum();
diagAddendum.addMessage(
LocAddendum.argumentType().format({
type: printType(simplifiedType, { expandTypeAlias: true }),
})
);
addDiagnostic(
DiagnosticRule.reportUnknownArgumentType,
LocMessage.argTypePartiallyUnknown() + diagAddendum.getString(),
argParam.errorNode
);
}
} else if (isAny(simplifiedType)) {
const diagAddendum = getDiagAddendum();
addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.argTypeAny() + diagAddendum.getString(),
argParam.errorNode
);
} else {
const partlyUnknown = isPartlyUnknown(simplifiedType);
if (partlyUnknown || isPartlyAny(simplifiedType)) {
// If the parameter type is also partially unknown, don't report
// the error because it's likely that the partially-unknown type
// arose due to bidirectional type matching.
if (!isPartlyUnknown(argParam.paramType)) {
const diagAddendum = getDiagAddendum();
diagAddendum.addMessage(
LocAddendum.argumentType().format({
type: printType(simplifiedType, { expandTypeAlias: true }),
})
);
const addendum = diagAddendum.getString();
if (partlyUnknown) {
addDiagnostic(
DiagnosticRule.reportUnknownArgumentType,
LocMessage.argTypePartiallyUnknown() + addendum,
argParam.errorNode
);
} else {
addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.argTypePartiallyAny() + addendum,
argParam.errorNode
);
}
}
}
}
}
}
Expand Down Expand Up @@ -14121,26 +14134,35 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions

if (isUnknown(simplifiedType)) {
addDiagnostic(rule, LocMessage.typeUnknown().format({ name: nameValue }), errorNode);
} else if (isPartlyUnknown(simplifiedType)) {
// If ignoreEmptyContainers is true, don't report the problem for
// empty containers (lists or dictionaries). We'll report the problem
// only if the assigned value is used later.
if (!ignoreEmptyContainers || !isClassInstance(type) || !type.isEmptyContainer) {
const diagAddendum = new DiagnosticAddendum();
diagAddendum.addMessage(
LocAddendum.typeOfSymbol().format({
name: nameValue,
type: printType(simplifiedType, { expandTypeAlias: true }),
})
);
addDiagnostic(
rule,
LocMessage.typePartiallyUnknown().format({ name: nameValue }) + diagAddendum.getString(),
errorNode
);
}
} else if (isAny(simplifiedType)) {
addDiagnostic(DiagnosticRule.reportAny, LocMessage.typeAny().format({ name: nameValue }), errorNode);
} else {
const partlyUnknown = isPartlyUnknown(simplifiedType);
if (partlyUnknown || isPartlyAny(simplifiedType)) {
// If ignoreEmptyContainers is true, don't report the problem for
// empty containers (lists or dictionaries). We'll report the problem
// only if the assigned value is used later.
if (!ignoreEmptyContainers || !isClassInstance(type) || !type.isEmptyContainer) {
const diagAddendum = new DiagnosticAddendum();
diagAddendum.addMessage(
LocAddendum.typeOfSymbol().format({
name: nameValue,
type: printType(simplifiedType, { expandTypeAlias: true }),
})
);
const formatter = { name: nameValue };
const addendum = diagAddendum.getString();
if (partlyUnknown) {
addDiagnostic(rule, LocMessage.typePartiallyUnknown().format(formatter) + addendum, errorNode);
} else {
addDiagnostic(
DiagnosticRule.reportAny,
LocMessage.typePartiallyAny().format(formatter) + addendum,
errorNode
);
}
}
}
}
}

Expand Down
Loading

0 comments on commit 0992c57

Please sign in to comment.