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 the sphinx-alt-text-validator tool to VCS install it in each API repo #2445

Merged
merged 8 commits into from
Dec 9, 2024

Conversation

arnaucasau
Copy link
Collaborator

This PR adds a tool that all the APIs should use to avoid having images without alt text. By having the script in a central repository, we could use the VCS method to pip install it everywhere instead of duplicating the same script 9 times.

The tool is currently being used in qiskit, runtime, and transpiler. Code -> https://github.com/Qiskit/qiskit/blob/main/tools/verify_images.py

The code added in this PR is the same but split into two files (__init__.py and verify_images.py) and with the possibility of using two new arguments to specify the folder where the linter will check their files and an argument to enumerate all the files the user wants to skip, replacing the ALLOWLIST_MISSING_ALT_TEXT list that was defined at the beginning of the script.

To install and test the tool we use

pip install -e "git+https://github.com/arnaucasau/documentation.git@AC/sphinx-alt-text-validator#egg=sphinx-alt-text-validator&subdirectory=scripts/image-tester"

and run sphinx-alt-text-validator -f [FolderToCheck]

Note

This PR only copies the script to the repo and adds the necessary files to make it installable from the API repos. In a follow-up, I'm planning to add some tests and maybe do a little refactoring to the verify_image.py

return alt_exists or nofigs_exists


def validate_image(file_path: str) -> tuple[str, list[str]]:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same function but removing this conditional:

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

Now the function gets the files filtered from __init__.py so no need to verify any allowlist anymore

Comment on lines 22 to 29
parser = argparse.ArgumentParser(prog="verify_images.py")
parser.add_argument("-f", "--folder", required=True)
parser.add_argument("-s", "--skip", nargs="+")
args = parser.parse_args()

skip_list = args.skip if args.skip is not None else []
files = glob.glob(f"{args.folder}/**/*.py", recursive=True)
filtered_files = [file for file in files if file not in skip_list]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only new code added in this PR

@arnaucasau arnaucasau marked this pull request as ready for review December 9, 2024 10:18
Copy link
Member

@frankharkins frankharkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! I did a quick test and seems to work well :)

When this is merged to main, make sure to pin to the commit when installing in other repos. This means we can make changes to the script without worrying about accidentally breaking their CI.

pip install -e "git+https://github.com/Qiskit/documentation.git@<COMMIT-HASH>#egg=sphinx-alt-text-validator&subdirectory=scripts/image-tester"

Co-authored-by: Frank Harkins <frankharkins@hotmail.co.uk>
@arnaucasau arnaucasau added this pull request to the merge queue Dec 9, 2024
Merged via the queue into Qiskit:main with commit bc34108 Dec 9, 2024
2 checks passed
@arnaucasau arnaucasau deleted the AC/sphinx-alt-text-validator branch December 9, 2024 10:59
Copy link
Collaborator

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Can you please add in a follow up a simple Pytest test for validate_image? Now that it's a public API, it would be good to ensure we don't break it in the future.

  • You can reuse the Pytest infrastructure we have for nb-tester. I'd probably rename the Tox env to be something like tests
  • You'll probably want to change validate_image to be a pure function and have the text be passed in directly rather than using Pathlib

parser.add_argument("-s", "--skip", nargs="+")
args = parser.parse_args()

skip_list = args.skip or []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: using a set will be faster when the skip list is larger

github-merge-queue bot pushed a commit that referenced this pull request Dec 18, 2024
This PR creates tests for the `sphinx-alt-text-validator` and recovers
the tests of the `nb-tester` in CI. See
#2445 (review)
and
#2425 (comment)

Example of the test running:
https://github.com/Qiskit/documentation/actions/runs/12390997779/job/34587197281?pr=2502#step:22:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants