Fixed bug that results in a false positive error when a with statement has a single parenthesized context manager. This addresses #11020. (#11024)
Some checks failed
Run mypy_primer on push / Run mypy_primer on push (push) Has been cancelled
Validation / Typecheck (push) Has been cancelled
Validation / Style (push) Has been cancelled
Validation / Test macos-latest (push) Has been cancelled
Validation / Test ubuntu-latest (push) Has been cancelled
Validation / Test windows-latest (push) Has been cancelled
Validation / Build (push) Has been cancelled
Validation / Required (push) Has been cancelled

This commit is contained in:
Eric Traut 2025-10-11 12:13:48 -07:00 committed by GitHub
parent 346f74cb92
commit b1c9ae2bb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2317,6 +2317,8 @@ export class Parser {
// "dry run" to determine whether the entire list of "with items"
// is enclosed in parentheses.
let isParenthesizedWithItemList = false;
let isParenthesizedDisallowed = false;
if (possibleParen.type === TokenType.OpenParenthesis) {
const openParenTokenIndex = this._tokenIndex;
@ -2337,7 +2339,11 @@ export class Parser {
this._peekToken().type === TokenType.CloseParenthesis &&
this._peekToken(1).type === TokenType.Colon
) {
isParenthesizedWithItemList = withItemList.length !== 1 || withItemList[0].d.target !== undefined;
isParenthesizedWithItemList = true;
// Some forms of parenthesized context with statements were not
// allowed prior to Python 3.9. Is this such a form?
isParenthesizedDisallowed = withItemList.length !== 1 || withItemList[0].d.target !== undefined;
}
this._tokenIndex = openParenTokenIndex;
@ -2347,7 +2353,7 @@ export class Parser {
if (isParenthesizedWithItemList) {
this._consumeTokenIfType(TokenType.OpenParenthesis);
if (PythonVersion.isLessThan(this._getLanguageVersion(), pythonVersion3_9)) {
if (isParenthesizedDisallowed && PythonVersion.isLessThan(this._getLanguageVersion(), pythonVersion3_9)) {
this._addSyntaxError(LocMessage.parenthesizedContextManagerIllegal(), possibleParen);
}
}