From 085c1d82066561d2715c49eb15fd745629969801 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler Date: Mon, 14 Oct 2024 14:18:42 +0200 Subject: [PATCH 1/4] Add NOLINT_START/NOLINT_END to lint.py --- scripts/lint.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/lint.py b/scripts/lint.py index ce9a83ec8392..f2ea3f631c6d 100755 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -3,7 +3,8 @@ """ Runs custom linting on our code. -Adding "NOLINT" to any line makes the linter ignore that line. +Adding "NOLINT" to any line makes the linter ignore that line. Adding a pair of "NOLINT_START" and "NOLINT_END" makes +the linter ignore these lines, as well as all lines in between. """ from __future__ import annotations @@ -973,7 +974,19 @@ def _update_content(self) -> None: self.content = "".join(self.lines) # gather lines with a `NOLINT` marker - self.no_lints = {i for i, line in enumerate(self.lines) if "NOLINT" in line} + self.no_lints = set() + is_in_no_lint_block = False + for i, line in enumerate(self.lines): + if "NOLINT" in line: + self.no_lints.add(i) + + if "NOLINT_START" in line: + is_in_no_lint_block = True + + if is_in_no_lint_block: + self.no_lints.add(i) + if "NOLINT_END" in line: + is_in_no_lint_block = False def rewrite(self, new_lines: list[str]) -> None: """Rewrite the contents of the file.""" @@ -1020,8 +1033,18 @@ def lint_file(filepath: str, args: Any) -> int: is_in_docstring = False + is_in_no_lint_block = False + prev_line = None for line_nr, line in enumerate(source.lines): + if "NOLINT_START" in line: + is_in_no_lint_block = True + + if is_in_no_lint_block: + if "NOLINT_END" in line: + is_in_no_lint_block = False + continue + if line == "" or line[-1] != "\n": error = "Missing newline at end of file" else: From f0de8731a703bfcb629d76c6dc64e278d3df7733 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler Date: Mon, 14 Oct 2024 19:32:09 +0200 Subject: [PATCH 2/4] Handle NOLINT in lint_markdown --- scripts/lint.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/lint.py b/scripts/lint.py index f2ea3f631c6d..52b6f46aaad8 100755 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -882,6 +882,10 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s if in_metadata and line.startswith("-->"): in_metadata = False + if "NOLINT" in line: + lines_out.append(line) + continue + if not in_code_block: if not in_metadata: # Check the casing on markdown headers From 90ea0d102d79680ecac7d5b84eae9b1d136d3362 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler Date: Mon, 14 Oct 2024 20:34:27 +0200 Subject: [PATCH 3/4] Improved code --- scripts/lint.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/scripts/lint.py b/scripts/lint.py index 52b6f46aaad8..cbb706a5a238 100755 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -850,7 +850,7 @@ def fix_enforced_upper_case(s: str) -> str: return "".join(new_words) -def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[str]]: +def lint_markdown(filepath: str, source: SourceFile) -> tuple[list[str], list[str]]: """Only for .md files.""" errors = [] @@ -864,12 +864,12 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s in_code_of_conduct = filepath.endswith("CODE_OF_CONDUCT.md") if in_code_of_conduct: - return errors, lines_in + return errors, source.lines in_code_block = False in_frontmatter = False in_metadata = False - for line_nr, line in enumerate(lines_in): + for line_nr, line in enumerate(source.lines): line_nr = line_nr + 1 if line.strip().startswith("```"): @@ -882,11 +882,7 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s if in_metadata and line.startswith("-->"): in_metadata = False - if "NOLINT" in line: - lines_out.append(line) - continue - - if not in_code_block: + if not in_code_block and not source.should_ignore(line_nr): if not in_metadata: # Check the casing on markdown headers if m := re.match(r"(\#+ )(.*)", line): @@ -1037,16 +1033,9 @@ def lint_file(filepath: str, args: Any) -> int: is_in_docstring = False - is_in_no_lint_block = False - prev_line = None for line_nr, line in enumerate(source.lines): - if "NOLINT_START" in line: - is_in_no_lint_block = True - - if is_in_no_lint_block: - if "NOLINT_END" in line: - is_in_no_lint_block = False + if source.should_ignore(line_nr): continue if line == "" or line[-1] != "\n": @@ -1076,7 +1065,7 @@ def lint_file(filepath: str, args: Any) -> int: source.rewrite(lines_out) if filepath.endswith(".md"): - errors, lines_out = lint_markdown(filepath, source.lines) + errors, lines_out = lint_markdown(filepath, source) for error in errors: print(source.error(error)) From f3031a65ec6ba6bb6614db078df32b01bc63df27 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 15 Oct 2024 13:26:12 +0200 Subject: [PATCH 4/4] naming fix --- scripts/lint.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/lint.py b/scripts/lint.py index cbb706a5a238..980b0f1caa61 100755 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -974,19 +974,19 @@ def _update_content(self) -> None: self.content = "".join(self.lines) # gather lines with a `NOLINT` marker - self.no_lints = set() - is_in_no_lint_block = False + self.nolints = set() + is_in_nolint_block = False for i, line in enumerate(self.lines): if "NOLINT" in line: - self.no_lints.add(i) + self.nolints.add(i) if "NOLINT_START" in line: - is_in_no_lint_block = True + is_in_nolint_block = True - if is_in_no_lint_block: - self.no_lints.add(i) + if is_in_nolint_block: + self.nolints.add(i) if "NOLINT_END" in line: - is_in_no_lint_block = False + is_in_nolint_block = False def rewrite(self, new_lines: list[str]) -> None: """Rewrite the contents of the file.""" @@ -1006,7 +1006,7 @@ def should_ignore(self, from_line: int, to_line: int | None = None) -> bool: if to_line is None: to_line = from_line - return any(i in self.no_lints for i in range(from_line - 1, to_line + 1)) + return any(i in self.nolints for i in range(from_line - 1, to_line + 1)) def should_ignore_index(self, start_idx: int, end_idx: int | None = None) -> bool: """Same as `should_ignore` but takes 0-based indices instead of line numbers."""