|
10 | 10 | if TYPE_CHECKING: |
11 | 11 | from ._config import Config |
12 | 12 |
|
13 | | -COLOR_PATTERN = '(\x1b\\[\\d*m?|\x0f)*' |
14 | | -REX_COLOR = re.compile(COLOR_PATTERN) |
15 | | -REX_LINE = re.compile(rf""" |
| 13 | +REX_COLOR = re.compile('(\x1b\\[\\d*m?|\x0f)*') |
| 14 | +REX_COLOR_NBQA = re.compile(r'\[\d*m|\x1b|\(B') |
| 15 | +REX_LINE = re.compile(r""" |
16 | 16 | (?P<path>.+\.py): |
17 | 17 | (?P<lineno>[0-9]+):\s |
18 | | - {COLOR_PATTERN}(?P<severity>[a-z]+):{COLOR_PATTERN}\s |
| 18 | + (?P<severity>[a-z]+):\s |
19 | 19 | (?P<message>.+?) |
20 | | - (?:\s\s{COLOR_PATTERN}\[(?P<category>[a-z-]+)\]{COLOR_PATTERN})? |
| 20 | + (?:\s\s\[(?P<category>[a-z-]+)\])? |
| 21 | + \s* |
| 22 | +""", re.VERBOSE | re.MULTILINE) |
| 23 | +REX_LINE_NBQA = re.compile(r""" |
| 24 | + (?P<path>.+\.ipynb:cell_[0-9]+): |
| 25 | + (?P<lineno>[0-9]+):\s |
| 26 | + (?P<severity>[a-z]+):\s |
| 27 | + (?P<message>.+?) |
| 28 | + (?:\s\s\[(?P<category>[a-z-]+)\])? |
21 | 29 | \s* |
22 | 30 | """, re.VERBOSE | re.MULTILINE) |
23 | 31 | REX_LINE_IN_MSG = re.compile(r'defined on line \d+') |
24 | 32 |
|
25 | 33 |
|
| 34 | +def _remove_color_codes(line: str) -> str: |
| 35 | + line = REX_COLOR.sub('', line) |
| 36 | + return REX_COLOR_NBQA.sub('', line) |
| 37 | + |
| 38 | + |
26 | 39 | @dataclass |
27 | 40 | class Error: |
28 | 41 | raw_line: str |
29 | 42 | _match: re.Match[str] |
30 | 43 |
|
31 | 44 | @classmethod |
32 | 45 | def new(self, line: str) -> Error | None: |
33 | | - match = REX_LINE.fullmatch(line) |
| 46 | + line = _remove_color_codes(line) |
| 47 | + match = REX_LINE.fullmatch(line) or REX_LINE_NBQA.fullmatch(line) |
34 | 48 | if match is None: |
35 | 49 | return None |
36 | 50 | return Error(line, match) |
@@ -61,6 +75,7 @@ def get_clean_line(self, config: Config) -> str: |
61 | 75 | path = Path(*self.path.parts[:config.depth]) |
62 | 76 | pos = self.line_number if config.preserve_position else 0 |
63 | 77 | msg = REX_COLOR.sub('', self.message).strip() |
| 78 | + msg = REX_COLOR_NBQA.sub('', self.message).strip() |
64 | 79 | msg = REX_LINE_IN_MSG.sub('defined on line 0', msg) |
65 | 80 | line = f'{path}:{pos}: {self.severity}: {msg}' |
66 | 81 | if self.category != 'note': |
|
0 commit comments