diff --git a/packages/pyright-internal/src/parser/tokenizer.ts b/packages/pyright-internal/src/parser/tokenizer.ts index 6614e5335fa4..d113a8e77946 100644 --- a/packages/pyright-internal/src/parser/tokenizer.ts +++ b/packages/pyright-internal/src/parser/tokenizer.ts @@ -1133,12 +1133,16 @@ export class Tokenizer { const value = this._cs.getText().substring(start, start + length); const comment = Comment.create(start, length, value); - const typeIgnoreRegexMatch = value.match(/^\s*type:\s*ignore(\s*\[([\s*\w-,]*)\]|\s|$)/); + const typeIgnoreRegexMatch = value.match(/((^|#)\s*)type:\s*ignore(\s*\[([\s*\w-,]*)\]|\s|$)/); if (typeIgnoreRegexMatch) { - const textRange: TextRange = { start, length: typeIgnoreRegexMatch[0].length }; + const commentStart = start + (typeIgnoreRegexMatch.index ?? 0); + const textRange: TextRange = { + start: commentStart + typeIgnoreRegexMatch[1].length, + length: typeIgnoreRegexMatch[0].length - typeIgnoreRegexMatch[1].length, + }; const ignoreComment: IgnoreComment = { range: textRange, - rulesList: this._getIgnoreCommentRulesList(start, typeIgnoreRegexMatch), + rulesList: this._getIgnoreCommentRulesList(commentStart, typeIgnoreRegexMatch), }; if (this._tokens.findIndex((t) => t.type !== TokenType.NewLine && t && t.type !== TokenType.Indent) < 0) { @@ -1148,12 +1152,16 @@ export class Tokenizer { } } - const pyrightIgnoreRegexMatch = value.match(/^\s*pyright:\s*ignore(\s*\[([\s*\w-,]*)\]|\s|$)/); + const pyrightIgnoreRegexMatch = value.match(/((^|#)\s*)pyright:\s*ignore(\s*\[([\s*\w-,]*)\]|\s|$)/); if (pyrightIgnoreRegexMatch) { - const textRange: TextRange = { start, length: pyrightIgnoreRegexMatch[0].length }; + const commentStart = start + (pyrightIgnoreRegexMatch.index ?? 0); + const textRange: TextRange = { + start: commentStart + pyrightIgnoreRegexMatch[1].length, + length: pyrightIgnoreRegexMatch[0].length - pyrightIgnoreRegexMatch[1].length, + }; const ignoreComment: IgnoreComment = { range: textRange, - rulesList: this._getIgnoreCommentRulesList(start, pyrightIgnoreRegexMatch), + rulesList: this._getIgnoreCommentRulesList(commentStart, pyrightIgnoreRegexMatch), }; this._pyrightIgnoreLines.set(this._lineRanges.length, ignoreComment); } @@ -1163,11 +1171,11 @@ export class Tokenizer { // Extracts the individual rules within a "type: ignore [x, y, z]" comment. private _getIgnoreCommentRulesList(start: number, match: RegExpMatchArray): IgnoreCommentRule[] | undefined { - if (match.length < 3 || match[2] === undefined) { + if (match.length < 5 || match[4] === undefined) { return undefined; } - const splitElements = match[2].split(','); + const splitElements = match[4].split(','); const commentRules: IgnoreCommentRule[] = []; let currentOffset = start + match[0].indexOf('[') + 1; diff --git a/packages/pyright-internal/src/tests/samples/pyrightIgnore2.py b/packages/pyright-internal/src/tests/samples/pyrightIgnore2.py index d2ff245075f9..0ea0ced52e30 100644 --- a/packages/pyright-internal/src/tests/samples/pyrightIgnore2.py +++ b/packages/pyright-internal/src/tests/samples/pyrightIgnore2.py @@ -1,4 +1,4 @@ -# This sample tests the use of a # pyright: ignore comment in conjunction +# This sample tests the use of a pyright ignore comment in conjunction # with the reportUnnecessaryTypeIgnoreComment mechanism. from typing import Optional @@ -19,6 +19,6 @@ def foo(self, x: Optional[int]) -> str: v4 = x + x # pyright: ignore [] # One of these is unnecessary - v5 = x + "hi" # pyright: ignore [reportGeneralTypeIssues, foo] + v5 = x + "hi" # test # pyright: ignore [reportGeneralTypeIssues, foo] return 3 # pyright: ignore [reportGeneralTypeIssues]