Fixed a bug that resulted in a false positive error when using an unpack operator (*) in a quoted type annotation with versions of Python prior to 3.11.

This commit is contained in:
Eric Traut 2022-07-31 09:53:06 -07:00
parent 74d43236f6
commit 0971dc3f12
2 changed files with 14 additions and 2 deletions

View File

@ -217,6 +217,7 @@ export class Parser {
private _isInFinally = false;
private _isParsingTypeAnnotation = false;
private _isParsingIndexTrailer = false;
private _isParsingQuotedText = false;
private _futureImportMap = new Map<string, boolean>();
private _importedModules: ModuleImport[] = [];
private _containsWildcardImport = false;
@ -288,8 +289,10 @@ export class Parser {
let parseTree: ExpressionNode | FunctionAnnotationNode | undefined;
if (parseTextMode === ParseTextMode.VariableAnnotation) {
this._isParsingQuotedText = true;
parseTree = this._parseTypeAnnotation();
} else if (parseTextMode === ParseTextMode.FunctionAnnotation) {
this._isParsingQuotedText = true;
parseTree = this._parseFunctionTypeAnnotation();
} else {
const exprListResult = this._parseTestOrStarExpressionList(
@ -3524,7 +3527,9 @@ export class Parser {
if (argType !== ArgumentCategory.Simple) {
const unpackAllowed =
this._parseOptions.isStubFile || this._getLanguageVersion() >= PythonVersion.V3_11;
this._parseOptions.isStubFile ||
this._isParsingQuotedText ||
this._getLanguageVersion() >= PythonVersion.V3_11;
if (argType === ArgumentCategory.UnpackedDictionary || !unpackAllowed) {
this._addError(Localizer.Diagnostic.unpackedSubscriptIllegal(), argNode);
@ -4357,7 +4362,11 @@ export class Parser {
if (isUnpack) {
if (!allowUnpack) {
this._addError(Localizer.Diagnostic.unpackInAnnotation(), startToken);
} else if (!this._parseOptions.isStubFile && this._getLanguageVersion() < PythonVersion.V3_11) {
} else if (
!this._parseOptions.isStubFile &&
!this._isParsingQuotedText &&
this._getLanguageVersion() < PythonVersion.V3_11
) {
this._addError(Localizer.Diagnostic.unpackedSubscriptIllegal(), startToken);
}
}

View File

@ -54,3 +54,6 @@ def func11(*v11: *tuple[int, ...]):
def func12(*v11: *tuple[int, int]):
pass
def func13(v12: "tuple[str, *tuple[int, ...], str]"):
pass