Fixed a bug that results in a false negative when an Unpack is used in a union. This isn't allowed. (#7805)

This commit is contained in:
Eric Traut 2024-04-30 01:46:43 -07:00 committed by GitHub
parent 42962e796f
commit 582a93e3fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 17 deletions

View File

@ -622,6 +622,7 @@ export function getTypeOfBinaryOperation(
fileInfo.isStubFile ||
(flags & EvaluatorFlags.AllowForwardReferences) !== 0 ||
fileInfo.executionEnvironment.pythonVersion.isGreaterOrEqualTo(pythonVersion3_10);
if (!unionNotationSupported) {
// If the left type is Any, we can't say for sure whether this
// is an illegal syntax or a valid application of the "|" operator.
@ -635,16 +636,10 @@ export function getTypeOfBinaryOperation(
}
}
if (
!evaluator.validateTypeArg(
{ ...leftTypeResult, node: leftExpression },
{ allowVariadicTypeVar: true, allowUnpackedTuples: true }
) ||
!evaluator.validateTypeArg(
{ ...rightTypeResult, node: rightExpression },
{ allowVariadicTypeVar: true, allowUnpackedTuples: true }
)
) {
const isLeftTypeArgValid = evaluator.validateTypeArg({ ...leftTypeResult, node: leftExpression });
const isRightTypeArgValid = evaluator.validateTypeArg({ ...rightTypeResult, node: rightExpression });
if (!isLeftTypeArgValid || !isRightTypeArgValid) {
return { type: UnknownType.create() };
}

View File

@ -15408,7 +15408,11 @@ export function createTypeEvaluator(
for (const typeArg of typeArgs) {
let typeArgType = typeArg.type;
if (!validateTypeArg(typeArg, { allowVariadicTypeVar: true, allowUnpackedTuples: true })) {
if (
!validateTypeArg(typeArg, {
allowVariadicTypeVar: fileInfo.diagnosticRuleSet.enableExperimentalFeatures,
})
) {
typeArgType = UnknownType.create();
} else if (!isEffectivelyInstantiable(typeArgType)) {
addExpectedClassDiagnostic(typeArgType, typeArg.node);

View File

@ -574,7 +574,7 @@
"unnecessaryPyrightIgnoreRule": "Unnecessary \"# pyright: ignore\" rule: \"{name}\"",
"unnecessaryTypeIgnore": "Unnecessary \"# type: ignore\" comment",
"unpackArgCount": "Expected a single type argument after \"Unpack\"",
"unpackedArgInTypeArgument": "Unpacked arguments cannot be used in type argument lists",
"unpackedArgInTypeArgument": "Unpacked arguments cannot be used in this context",
"unpackedArgWithVariadicParam": "Unpacked argument cannot be used for TypeVarTuple parameter",
"unpackedDictArgumentNotMapping": "Argument expression after ** must be a mapping with a \"str\" key type",
"unpackedDictSubscriptIllegal": "Dictionary unpack operator in subscript is not allowed",

View File

@ -4,10 +4,7 @@
# pyright: reportMissingModuleSource=false
from typing import Generic, TypeVar, Union
from typing_extensions import ( # pyright: ignore[reportMissingModuleSource]
TypeVarTuple,
Unpack,
)
from typing_extensions import TypeVarTuple, Unpack
_T = TypeVar("_T")

View File

@ -169,7 +169,7 @@ test('VariadicTypeVar1', () => {
configOptions.defaultPythonVersion = pythonVersion3_11;
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['variadicTypeVar1.py'], configOptions);
TestUtils.validateResults(analysisResults, 19);
TestUtils.validateResults(analysisResults, 18);
});
test('VariadicTypeVar2', () => {