mirror of
https://github.com/microsoft/pyright.git
synced 2024-10-03 19:37:39 +03:00
Fixed a bug that led to incorrect type evaluation when a constrained TypeVar includes literal types as constraints. This addresses #6917. (#6929)
This commit is contained in:
parent
9b64d81c94
commit
064f02e15e
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -12,4 +12,5 @@
|
||||
"source.organizeImports": "explicit"
|
||||
},
|
||||
"typescript.tsdk": "node_modules/typescript/lib",
|
||||
"python.languageServer": "None",
|
||||
}
|
||||
|
@ -52,6 +52,7 @@ import {
|
||||
getTypeCondition,
|
||||
getTypeVarScopeId,
|
||||
isEffectivelyInstantiable,
|
||||
isLiteralTypeOrUnion,
|
||||
isPartlyUnknown,
|
||||
mapSubtypes,
|
||||
sortTypes,
|
||||
@ -652,6 +653,7 @@ function assignTypeToConstrainedTypeVar(
|
||||
|
||||
const curWideTypeBound = curEntry?.wideBound;
|
||||
const curNarrowTypeBound = curEntry?.narrowBound;
|
||||
let forceRetainLiterals = false;
|
||||
|
||||
if (isTypeVar(srcType)) {
|
||||
if (
|
||||
@ -785,6 +787,8 @@ function assignTypeToConstrainedTypeVar(
|
||||
})
|
||||
);
|
||||
return false;
|
||||
} else if (isLiteralTypeOrUnion(constrainedType)) {
|
||||
forceRetainLiterals = true;
|
||||
}
|
||||
|
||||
if (curNarrowTypeBound && !isAnyOrUnknown(curNarrowTypeBound)) {
|
||||
@ -829,7 +833,14 @@ function assignTypeToConstrainedTypeVar(
|
||||
} else {
|
||||
// Assign the type to the type var.
|
||||
if (!typeVarContext.isLocked() && isTypeVarInScope) {
|
||||
updateTypeVarType(evaluator, typeVarContext, destType, constrainedType, curWideTypeBound);
|
||||
updateTypeVarType(
|
||||
evaluator,
|
||||
typeVarContext,
|
||||
destType,
|
||||
constrainedType,
|
||||
curWideTypeBound,
|
||||
forceRetainLiterals
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,18 @@
|
||||
# This tests the handling of a constrained TypeVar with literal types
|
||||
# in the constraints.
|
||||
|
||||
from typing import TypeVar, Literal, Generic
|
||||
|
||||
T = TypeVar("T", Literal[True], Literal[False])
|
||||
|
||||
|
||||
class A(Generic[T]):
|
||||
def __init__(self, null: T = False) -> None:
|
||||
pass
|
||||
|
||||
|
||||
A(null=bool()) # Type error
|
||||
|
||||
reveal_type(A(null=False), expected_text="A[Literal[False]]")
|
||||
reveal_type(A(), expected_text="A[Literal[False]]")
|
||||
reveal_type(A(null=True), expected_text="A[Literal[True]]")
|
@ -518,6 +518,12 @@ test('ConstrainedTypeVar18', () => {
|
||||
TestUtils.validateResults(analysisResults, 0);
|
||||
});
|
||||
|
||||
test('ConstrainedTypeVar19', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['constrainedTypeVar19.py']);
|
||||
|
||||
TestUtils.validateResults(analysisResults, 1);
|
||||
});
|
||||
|
||||
test('MissingTypeArg1', () => {
|
||||
const configOptions = new ConfigOptions(Uri.empty());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user