Skip to content

Commit

Permalink
refactor linter and add it to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaucasau committed Nov 29, 2024
1 parent 8f4c287 commit b30929e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
56 changes: 24 additions & 32 deletions tools/verify_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
import sys
import glob

# Dictionary to allowlist lines of code that the checker will not error
# Format: {"file_path": [list_of_line_numbers]}
ALLOWLIST_MISSING_ALT_TEXT = {
"qiskit_addon_utils/problem_generators/generate_time_evolution_circuit.py": [31]
}
# List of allowlist files that the checker will not verify
ALLOWLIST_MISSING_ALT_TEXT = [
"qiskit_addon_utils/problem_generators/generate_time_evolution_circuit.py"
]


def is_image(line: str) -> bool:
Expand All @@ -32,51 +31,44 @@ def is_image(line: str) -> bool:
def is_option(line: str) -> bool:
return line.strip().startswith(":")

def in_allowlist(filename: str, line_num: int) -> bool:
return line_num in ALLOWLIST_MISSING_ALT_TEXT.get(filename, [])

def is_valid_image(options: list[str]) -> bool:
alt_exists = any(option.strip().startswith(":alt:") for option in options)
nofigs_exists = any(option.strip().startswith(":nofigs:") for option in options)

# Only `.. plot::`` directives without the `:nofigs:` option are required to have alt text.
# Meanwhile, all `.. image::` directives need alt text and they don't have a `:nofigs:` option.
return alt_exists or nofigs_exists


def validate_image(file_path: str) -> tuple[str, list[str]]:
"""Validate all the images of a single file"""

if file_path in ALLOWLIST_MISSING_ALT_TEXT:
return [file_path, []]

invalid_images: list[str] = []

lines = Path(file_path).read_text().splitlines()

line_index = 0
image_found = False
image_line = -1
options: list[str] = []

while line_index < len(lines):
line = lines[line_index].strip()

if image_found and not is_option(line) and not is_valid_image(options):
invalid_images.append(f"- Error in line {image_line}: {lines[image_line-1].strip()}")
image_found = False
options = []
continue

for line_index, line in enumerate(lines):
if image_found and is_option(line):
options.append(line)
continue

if is_image(line) and not in_allowlist(file_path, line_index + 1):
image_found = True
image_line = line_index + 1
options = []
if image_found and not is_valid_image(options):
image_line = line_index - len(options)
invalid_images.append(f"- Error in line {image_line}: {lines[image_line-1].strip()}")

line_index += 1
image_found = is_image(line)
options = []

return (file_path, invalid_images)


def is_valid_image(options: list[str]) -> bool:
alt_exists = any(option.startswith(":alt:") for option in options)
nofigs_exists = any(option.startswith(":nofigs:") for option in options)

# Only `.. plot::`` directives without the `:nofigs:` option are required to have alt text.
# Meanwhile, all `.. image::` directives need alt text and they don't have a `:nofigs:` option.
return alt_exists or nofigs_exists

def main() -> None:
files = glob.glob("qiskit_addon_utils/**/*.py", recursive=True)

Expand Down Expand Up @@ -106,4 +98,4 @@ def main() -> None:


if __name__ == "__main__":
main()
main()
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ commands =
nbqa ruff docs/
mypy qiskit_addon_utils/
pylint -rn qiskit_addon_utils/ test/
python tools/verify_images.py
nbqa pylint -rn docs/
typos
reno lint
Expand Down

0 comments on commit b30929e

Please sign in to comment.