mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
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:
parent
42962e796f
commit
582a93e3fe
@ -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() };
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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",
|
||||
|
@ -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")
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user