|
3 | 3 | """ |
4 | 4 | Runs custom linting on our code. |
5 | 5 |
|
6 | | -Adding "NOLINT" to any line makes the linter ignore that line. |
| 6 | +Adding "NOLINT" to any line makes the linter ignore that line. Adding a pair of "NOLINT_START" and "NOLINT_END" makes |
| 7 | +the linter ignore these lines, as well as all lines in between. |
7 | 8 | """ |
8 | 9 |
|
9 | 10 | from __future__ import annotations |
@@ -973,7 +974,19 @@ def _update_content(self) -> None: |
973 | 974 | self.content = "".join(self.lines) |
974 | 975 |
|
975 | 976 | # gather lines with a `NOLINT` marker |
976 | | - self.no_lints = {i for i, line in enumerate(self.lines) if "NOLINT" in line} |
| 977 | + self.no_lints = set() |
| 978 | + is_in_no_lint_block = False |
| 979 | + for i, line in enumerate(self.lines): |
| 980 | + if "NOLINT" in line: |
| 981 | + self.no_lints.add(i) |
| 982 | + |
| 983 | + if "NOLINT_START" in line: |
| 984 | + is_in_no_lint_block = True |
| 985 | + |
| 986 | + if is_in_no_lint_block: |
| 987 | + self.no_lints.add(i) |
| 988 | + if "NOLINT_END" in line: |
| 989 | + is_in_no_lint_block = False |
977 | 990 |
|
978 | 991 | def rewrite(self, new_lines: list[str]) -> None: |
979 | 992 | """Rewrite the contents of the file.""" |
@@ -1020,8 +1033,18 @@ def lint_file(filepath: str, args: Any) -> int: |
1020 | 1033 |
|
1021 | 1034 | is_in_docstring = False |
1022 | 1035 |
|
| 1036 | + is_in_no_lint_block = False |
| 1037 | + |
1023 | 1038 | prev_line = None |
1024 | 1039 | for line_nr, line in enumerate(source.lines): |
| 1040 | + if "NOLINT_START" in line: |
| 1041 | + is_in_no_lint_block = True |
| 1042 | + |
| 1043 | + if is_in_no_lint_block: |
| 1044 | + if "NOLINT_END" in line: |
| 1045 | + is_in_no_lint_block = False |
| 1046 | + continue |
| 1047 | + |
1025 | 1048 | if line == "" or line[-1] != "\n": |
1026 | 1049 | error = "Missing newline at end of file" |
1027 | 1050 | else: |
|
0 commit comments