Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NOLINT block to lint.py #7720

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -849,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 = []
Expand All @@ -863,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("```"):
Expand All @@ -881,7 +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 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):
Expand Down Expand Up @@ -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
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
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."""
Expand Down Expand Up @@ -1022,6 +1035,9 @@ def lint_file(filepath: str, args: Any) -> int:

prev_line = None
for line_nr, line in enumerate(source.lines):
if source.should_ignore(line_nr):
continue

if line == "" or line[-1] != "\n":
error = "Missing newline at end of file"
else:
Expand Down Expand Up @@ -1049,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))
Expand Down
Loading