Skip to content

Commit

Permalink
Added support for # pyright: ignore and # type: ignore comments t…
Browse files Browse the repository at this point in the history
…hat are not at the start of a comment. This addresses #4259.
  • Loading branch information
msfterictraut committed Dec 1, 2022
1 parent 621d886 commit 1fa84c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 16 additions & 8 deletions packages/pyright-internal/src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/pyrightIgnore2.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]

0 comments on commit 1fa84c0

Please sign in to comment.