Developer Guid¶
Environment¶
While installing LoQS, developers may want to use the all:
pip install -e ".[all]"
Additionally, developers who want an editable version of a backend may want to install those in editable mode separately.
For example, getting an editable version of pyGSTi 0.9.12.3 via:
pip install -e git+https://github.com/sandialabs/pyGSTi.git@v0.9.12.3#egg=pyGSTi
The editable package will then be installed under src/pygsti.
The tag can be replaced with any tag, branch, or commit hash to check out the exact version of the package needed.
Testing¶
This project uses tox to perform code formatting/linting, type checking,
unit tests, and coverage reporting.
These features are available to anyone who has installed loqs with at least
the [tests] optional dependency.
Developers can run all tests using:
tox run
The first time you run tox, it will create different environments for each
testing stage in .tox.
Subsequent runs will be much faster, although these environments will need to
be rebuilt if the configuration in tox.ini changes (specifically if the
requirements of each environment change).
Code Formatting and Linting¶
This project uses black for autoformatting code and flake8 for linting.
This can be run with tox using
tox run -e lint
Developers can also set this up to be done locally before committing by performing the following steps:
pip install -e ."[dev]"
pre-commit install
If you have staged files for a commit, you can then do pre-commit run
to run the script that will be ran during the commit process.
Alternatively, you can run pre-commit run -a to just format and lint
the entire codebase.
Type Checking¶
This project uses lots of type annotations and mypy for type checking.
This can be run using tox via
tox run -e type
Unit Tests¶
This project uses pytest to run unit tests and doctests.
With tox, the unit tests can be run in a specific environment.
For example, the py311 environment tests Python 3.11, and tests can be
run with this version of Python via:
tox run -e py311
Developers can run unit tests pytest outside of tox also by simply doing:
pytest tests
This can be helpful to test specific files, or perhaps run pytest with
nonstandard flags.
Doctests are when tests are directly built into the docstring of a file/class/function. These can be a useful way to document examples directly in the source code, which makes them appear right next to the autogenerated reference in the documentation.
Doctests can be run by invoking pytest on the source code instead:
pytest --doctest-modules loqs
Coverage¶
The tox pipeline also generates coverage reports by using the pytest-cov
and coverage packages.
Coverage information is generated during the unit tests, and the coverage
report can be regenerated via:
tox run -e report
It's worth noting that the coverage information is also cleaned at the beginning
of each run to ensure there's no residual coverage inforomation (especially important
if using the --cov-append flag for pytest.)
This can be done at any time with:
tox run -e clean
Documentation¶
This project uses JupyterBook for its documentation, which should be
installed if using the [docs] optional dependencies.
I have also noticed that PYTHONPATH needs to be set to the root directory
in order for sphinx.ext.autosummary to find the package properly.
For a developer with a local install, this should build the docs:
pip install -e .'[docs]'
jupyter-book build docs
Open docs/_build/html/index.html and view your docs!
Something close to the following should work for Sphinx-only builds:
jupyter-book config sphinx docs
sphinx-build docs docs/_build/html -b html
Note: Depending on how your environment is set up, you may need to include PYTHONPATH=. to the beginning of any jupyter-book or sphinx build instructions.
If rebuilding the docs, it may be worth cleaning the build directory first:
jb clean docs
jb build docs
where jb is a built-in alias for jupyter-book.
If significant code changes has occurred, it may also be worth removing the _autosummary directory
to force a full regeneration of the automated documentation.
Jupytext Notebooks¶
Several of the MyST Markdown files are actually Jupyter notebooks. We are storing them as Markdown and converting to Jupyter notebooks using Jupytext.
Jupytext needs to be told about the kernel to use when running these as Jupyter notebooks (see the metadata block at the top of a -notebook.md file).
I am using the virtual environment installed during the User Guide above, which I have installed as a kernel on my system using:
python -m ipykernel install --user --display-name="Python 3.11 (loqs)" --name=loqs
A .md file can be initialized with this metadata using
jupyter-book myst init --kernel loqs <name>.md
or by simply copying the metadata block to the new file.
Syncing MyST Markdown and Jupyter Notebooks¶
The MyST Markdown is great for generating documentation for Sphinx, but less interactive than a Jupyter notebook. For users with a local copy of the codebase or developers creating tutorials, it can be convenient to work with the Jupyter notebook instead or switch back and forth between the two formats.
For the tutorial notebooks under docs/notebooks/, use loqs-nb-sync to convert between the two formats -- this command is installed automatically alongside LoQS when you install the docs extra:
loqs-nb-sync to-ipynb <file>.md # Convert from MyST Markdown to a working Jupyter notebook
loqs-nb-sync to-md <file>.ipynb # Sync edits back to MyST Markdown
Note that only the MyST Markdown will be commited to the repo, so make sure to sync back to the MyST Markdown version of the file to save your work. Always use loqs-nb-sync for both directions -- converting or syncing these notebooks any other way can leave broken links in the result.