Added missing check for inappropriate use of Final in a value expression. This addresses #7094.

This commit is contained in:
Eric Traut 2024-01-25 20:04:18 -08:00
parent 9c0c056b1f
commit ee4e30cf70
3 changed files with 17 additions and 6 deletions

View File

@ -11843,8 +11843,9 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
? EvaluatorFlags.AllowMissingTypeArgs |
EvaluatorFlags.EvaluateStringLiteralAsType |
EvaluatorFlags.DisallowParamSpec |
EvaluatorFlags.DisallowTypeVarTuple
: EvaluatorFlags.DoNotSpecialize;
EvaluatorFlags.DisallowTypeVarTuple |
EvaluatorFlags.DisallowFinal
: EvaluatorFlags.DoNotSpecialize | EvaluatorFlags.DisallowFinal;
const exprTypeResult = getTypeOfExpression(
argParam.argument.valueExpression,
flags,
@ -14929,7 +14930,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
): Type {
if (flags & EvaluatorFlags.DisallowFinal) {
addError(LocMessage.finalContext(), errorNode);
return AnyType.create();
return classType;
}
if (!typeArgs || typeArgs.length === 0) {
@ -14940,7 +14941,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
addError(LocMessage.finalTooManyArgs(), errorNode);
}
return typeArgs[0].type;
return TypeBase.cloneAsSpecialForm(typeArgs[0].type, classType);
}
function createConcatenateType(

View File

@ -2,7 +2,9 @@
# introduced in Python 3.8.
import typing
from typing import Any, Final, Protocol
from typing import Any, Final, Protocol, TypeVar
T = TypeVar("T")
foo1: typing.Final = 3
@ -181,3 +183,11 @@ class ClassB:
class ClassC(Protocol):
x: Final[int]
def func3(x: type[T]) -> T:
return x()
# This should generate two errors because Final isn't compatible with type.
func3(Final[int])

View File

@ -367,7 +367,7 @@ test('Final2', () => {
test('Final3', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['final3.py']);
TestUtils.validateResults(analysisResults, 28);
TestUtils.validateResults(analysisResults, 30);
});
test('Final4', () => {