1from collections import defaultdict
2import re
3
4NOQA_REGEXP = re.compile(
5    # Use the same regex as flake8 does.
6    # https://gitlab.com/pycqa/flake8/-/tree/master/src/flake8/defaults.py
7    # We're looking for items that look like this:
8    # `# noqa`
9    # `# noqa: E123`
10    # `# noqa: E123,W451,F921`
11    # `# NoQA: E123,W451,F921`
12    r"# noqa(?::[\s]?(?P<codes>([A-Z]+[0-9]+(?:[,\s]+)?)+))?",
13    re.IGNORECASE,
14)
15
16NOQA_CODE_MAP = {
17    # flake8 F401: module imported but unused.
18    "F401": "V104",
19    # flake8 F841: local variable is assigned to but never used.
20    "F841": "V107",
21}
22
23
24def _parse_error_codes(match):
25    # If no error code is specified, add the line to the "all" category.
26    return [
27        c.strip() for c in (match.groupdict()["codes"] or "all").split(",")
28    ]
29
30
31def parse_noqa(code):
32    noqa_lines = defaultdict(set)
33    for lineno, line in enumerate(code, start=1):
34        match = NOQA_REGEXP.search(line)
35        if match:
36            for error_code in _parse_error_codes(match):
37                error_code = NOQA_CODE_MAP.get(error_code, error_code)
38                noqa_lines[error_code].add(lineno)
39    return noqa_lines
40
41
42def ignore_line(noqa_lines, lineno, error_code):
43    """Check if the reported line is annotated with "# noqa"."""
44    return lineno in noqa_lines[error_code] or lineno in noqa_lines["all"]
45